Showing posts with label add. Show all posts
Showing posts with label add. Show all posts

Add component on JFrame during runtime

Source code below will show you how to add a component into a JFrame during program runtime. Component that will be use in this program, is JButton.

************************************************************************
COMPLETE SOURCE CODE FOR : AddComponentOnJFrameAtRuntime.java
************************************************************************


import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.FlowLayout;
import java.awt.BorderLayout;

public class AddComponentOnJFrameAtRuntime extends JFrame implements ActionListener
{
JPanel panel;

public AddComponentOnJFrameAtRuntime()
{
super("Add component on JFrame at runtime");
setLayout(new BorderLayout());
panel=new JPanel();
panel.setLayout(new FlowLayout());
add(panel,BorderLayout.CENTER);
JButton button=new JButton("CLICK HERE");
add(button,BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
panel.add(new JButton("Button"));
panel.revalidate();
validate();
}

public static void main(String[]args)
{
AddComponentOnJFrameAtRuntime acojfar=new AddComponentOnJFrameAtRuntime();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Add JSlider into JFrame

Source code below will show you, how to create horizontal slider in java using JSlider class, and after that add the slider into a JFrame.

***********************************************************************
COMPLETE SOURCE CODE FOR : AddJSliderIntoJFrame.java
***********************************************************************


import javax.swing.JSlider;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJSliderIntoJFrame
{
public static void main(String[]args)
{
//Create a slider from JSlider class
JSlider slider=new JSlider();

//spacing between major tick
slider.setMajorTickSpacing(10);

//spacing between minor tick
slider.setMinorTickSpacing(1);

//make slider integer value visible
slider.setPaintLabels(true);

//make slider tick visible
slider.setPaintTicks(true);

//Create a JFrame with title ( Put slider into JFrame )
JFrame frame=new JFrame("Put slider into JFrame");

//set layout manager for JFrame
frame.setLayout(new FlowLayout());

//Add JSlider into JFrame
frame.add(slider);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,100);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Add scrollbar to JFrame

Source code below will show you, how to add scrollbar to a JFrame.

******************************************************************************
COMPLETE SOURCE CODE FOR : AddScrollBarToJFrame.java
******************************************************************************


import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AddScrollBarToJFrame
{
public static void main(String[]args)
{
//Create a JPanel
JPanel panel=new JPanel();

//Create a scrollbar using JScrollPane and add panel into it's viewport
//Set vertical and horizontal scrollbar always show
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Create a JFrame with title ( AddScrollBarToJFrame )
JFrame frame=new JFrame("AddScrollBarToJFrame");

//Add JScrollPane into JFrame
frame.add(scrollBar);

//Set close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);

//So, if you want to add other component like JTextArea, just add them into JPanel.After that add
//the JPanel into JScrollPane before add the JScrollPane into JFrame.
}
}


******************************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************************

Add menu bar into JFrame

Source code below will show you, how to create menu bar for a JFrame using JMenuBar. It also show you, how to use JMenu that will be add into JMenuBar and how to use JMenuItem that will be list of menu item in a menu. JMenuItem will be add into JMenu. We also can add other JMenu into a JMenu.

*******************************************************************
COMPLETE SOURCE CODE FOR : AddMenuBarIntoJFrame.java
*******************************************************************


import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import javax.swing.JFrame;

import javax.swing.SwingConstants;

public class AddMenuBarIntoJFrame
{
public static void main(String[]args)
{
//Create a menu bar
JMenuBar frameMenuBar=new JMenuBar();

//Create first menu in menubar
JMenu menu1=new JMenu("Menu1");

//Create second menu in menubar
JMenu menu2=new JMenu("Menu2");

//Create third menu in menubar
JMenu menu3=new JMenu("Menu3");

//Create menu that will be add into third menu
JMenu menu3_internalMenu1=new JMenu("Menu3_Menu1");

//Create first menu item for first menu
JMenuItem menuItem1=new JMenuItem("Menu1_MenuItem1");

//Create first menu item for second menu
JMenuItem menuItem2=new JMenuItem("Menu2_MenuItem1");

//Create second menu item for second menu
JMenuItem menuItem3=new JMenuItem("Menu2_MenuItem2");

//Create first menu item for third menu
JMenuItem menuItem4=new JMenuItem("Menu3_MenuItem1");

//Create menu item that will be add into internal menu in third menu
JMenuItem menuItem5=new JMenuItem("Menu3_Menu1_MenuItem1");

//add first menu item into first menu
menu1.add(menuItem1);

//add first menu item into second menu
menu2.add(menuItem2);

//add second menu item into second menu
menu2.add(menuItem3);

//add first menu item into third menu
menu3.add(menuItem4);

//add internal menu into third menu
menu3.add(menu3_internalMenu1);

//add menu item into internal menu
menu3_internalMenu1.add(menuItem5);

//add first menu into menu bar
frameMenuBar.add(menu1);

//add second menu into menu bar
frameMenuBar.add(menu2);

//add third menu into menu bar
frameMenuBar.add(menu3);

//Create a JFrame with title ( JFrame with menu bar )
JFrame frame=new JFrame("JFrame with menu bar");

//Set menu bar for JFrame
frame.setJMenuBar(frameMenuBar);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,100);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Add JTextArea into JFrame

Source code below will show you how to create text area in java, and add it into JFrame. There are two example of text area in source code below. First, without initial text and second with initial text.

************************************************************************
COMPLETE SOURCE CODE FOR : AddJTextAreaIntoJFrame.java
************************************************************************


import javax.swing.JFrame;
import javax.swing.JTextArea;

import java.awt.GridLayout;

public class AddJTextAreaIntoJFrame
{
public static void main(String[]args)
{
//Create JTextArea without initial text
JTextArea textArea1=new JTextArea();

//Create JTextArea with initial text ( Text area with initial text )
JTextArea textArea2=new JTextArea("Text area with initial text");

//Create a JFrame with title ( Add JTextArea into JFrame )
JFrame frame=new JFrame("Add JTextArea into JFrame");

//Set JFrame layout
frame.setLayout(new GridLayout(2,1,1,5));

//Add first JTextArea into JFrame
frame.add(textArea1);

//Add second JTextArea into JFrame
frame.add(textArea2);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,500);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Add JTextField into JFrame

Source code below will show you how to add text field in JFrame. There are two example of text field. First is without initial text. Second is text field with initial text.

***********************************************************************
COMPLETE SOURCE CODE FOR : AddJTextFieldIntoJFrame.java
***********************************************************************


import javax.swing.JTextField;
import javax.swing.JFrame;

import java.awt.GridLayout;

public class AddJTextFieldIntoJFrame
{
public static void main(String[]args)
{
//Create JTextField with no initial text
JTextField textField1=new JTextField();

//Create JTextField with initial text ( Put your text here )
JTextField textField2=new JTextField("Put your text here");

//Create a JFrame with title ( Put JTextField into JFrame )
JFrame frame=new JFrame("Put JTextField into JFrame");

//Set layout for JFrame
frame.setLayout(new GridLayout(2,1,1,3));

//Add first JTextField into JFrame
frame.add(textField1);

//Add second JTextField into JFrame
frame.add(textField2);

//Set close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set size for JFrame
frame.setSize(500,100);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Add JList into JFrame

Source code below will show you, how to create a list in java using JList. After that we will add it, into a jframe.

**************************************************************************
COMPLETE SOURCE CODE FOR : AddJListIntoJFrame.java
**************************************************************************


import javax.swing.JList;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJListIntoJFrame
{
public static void main(String[]args)
{
//Create contents for JList
String[]listContents={"Honda","Toyota","Ford","Ferrari","Cooper"};

//Create a JList with it contents
JList myList=new JList(listContents);

//Create a JFrame with title ( Add JList into JFrame )
JFrame frame=new JFrame("Add JList into JFrame");

//Set layout for JFrame
frame.setLayout(new FlowLayout());

//Add JList into JFrame
frame.add(myList);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

Add JComboBox into JFrame

Source code below will show you how create contents for JComboBox. After that we will add JComboBox into a JFrame.

***********************************************************************
COMPLETE SOURCE CODE FOR : AddJComboBoxIntoJFrame.java
***********************************************************************


import javax.swing.JComboBox;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJComboBoxIntoJFrame
{
public static void main(String[]args)
{
//Create combo box contents from type String
String[]comboBoxContents={"Chicken","Duck","Cow","Sheep"};

//Create combo box using JComboBox
JComboBox myComboBox=new JComboBox(comboBoxContents);

//Create a JFrame with title ( Add JComboBox into JFrame )
JFrame frame=new JFrame("Add JComboBox into JFrame");

//Set layout for JFrame
frame.setLayout(new FlowLayout());

//Add JComboBox into JFrame
frame.add(myComboBox);

//Set close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Add JRadioButton into JFrame

Source code below will show you, how to create radio button in java using JRadioButton. It also show you how to use ButtonGroup that enable only one selection can make at radio button in one time.

*******************************************************************
COMPLETE SOURCE CODE FOR : AddJRadioButtonIntoJFrame.java
*******************************************************************


import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;

import java.awt.FlowLayout;
import java.awt.GridLayout;

public class AddJRadioButtonIntoJFrame
{
public static void main(String[]args)
{
//Create JRadioButton with text(Chicken)
JRadioButton radioButton1=new JRadioButton("Chicken");

//Create JRadioButton with text(Duck)
JRadioButton radioButton2=new JRadioButton("Duck");

//Create JRadioButton with text(Cow)
JRadioButton radioButton3=new JRadioButton("Cow");

//Create JRadioButton with text(Horse)
JRadioButton radioButton4=new JRadioButton("Horse");

//Create JRadioButton with text(Sheep)
JRadioButton radioButton5=new JRadioButton("Sheep");

//Create JRadioButton with text(Chicken)
JRadioButton radioButton6=new JRadioButton("Chicken");

//Create JRadioButton with text(Duck)
JRadioButton radioButton7=new JRadioButton("Duck");

//Create JRadioButton with text(Cow)
JRadioButton radioButton8=new JRadioButton("Cow");

//Create JRadioButton with text(Horse)
JRadioButton radioButton9=new JRadioButton("Horse");

//Create JRadioButton with text(Sheep)
JRadioButton radioButton10=new JRadioButton("Sheep");

//Create button group will make at one time
//only one JRadioButton can select in that group.
//Create first JRadioButton group
ButtonGroup groupFirst=new ButtonGroup();
groupFirst.add(radioButton1);
groupFirst.add(radioButton2);
groupFirst.add(radioButton3);
groupFirst.add(radioButton4);
groupFirst.add(radioButton5);

//Create second JRadioButton group
ButtonGroup groupSecond=new ButtonGroup();
groupSecond.add(radioButton6);
groupSecond.add(radioButton7);
groupSecond.add(radioButton8);
groupSecond.add(radioButton9);
groupSecond.add(radioButton10);

//Create a JFrame with title ( Add JRadioButton into JFrame )
JFrame frame=new JFrame("Add JRadioButton into JFrame");

//Create a JPanel that we use to add JRadioButton into it
JPanel panel=new JPanel();

//Create a JPanel that we use to add JRadioButton into it
JPanel panel2=new JPanel();

//Set layout for JFrame
frame.setLayout(new FlowLayout());

//Set layout for first JPanel
panel.setLayout(new FlowLayout());

//Set layout for second JPanel
panel2.setLayout(new GridLayout(5,1));

//Add radioButton1 - radioButton5 into first JPanel
panel.add(radioButton1);
panel.add(radioButton2);
panel.add(radioButton3);
panel.add(radioButton4);
panel.add(radioButton5);

//Add radioButton6 - radioButton10 into second JPanel
panel2.add(radioButton6);
panel2.add(radioButton7);
panel2.add(radioButton8);
panel2.add(radioButton9);
panel2.add(radioButton10);

//Add first JPanel into JFrame
frame.add(panel);

//Add second JPanel into JFrame
frame.add(panel2);

//Set close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set size for JFrame
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Add JCheckBox into JFrame

Source code below will show you how to add JCheckBox into JPanel. After that we will add the JPanel into JFrame.

***************************************************************************
COMPLETE SOURCE CODE FOR : AddJCheckBoxIntoJFrame.java
***************************************************************************


import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.FlowLayout;
import java.awt.GridLayout;

public class AddJCheckBoxIntoJFrame
{
public static void main(String[]args)
{
//Create JCheckBox with text(Chicken)
JCheckBox checkBox1=new JCheckBox("Chicken");

//Create JCheckBox with text(Duck)
JCheckBox checkBox2=new JCheckBox("Duck");

//Create JCheckBox with text(Cow)
JCheckBox checkBox3=new JCheckBox("Cow");

//Create JCheckBox with text(Horse)
JCheckBox checkBox4=new JCheckBox("Horse");

//Create JCheckBox with text(Sheep)
JCheckBox checkBox5=new JCheckBox("Sheep");

//Create JCheckBox with text(Chicken)
JCheckBox checkBox6=new JCheckBox("Chicken");

//Create JCheckBox with text(Duck)
JCheckBox checkBox7=new JCheckBox("Duck");

//Create JCheckBox with text(Cow)
JCheckBox checkBox8=new JCheckBox("Cow");

//Create JCheckBox with text(Horse)
JCheckBox checkBox9=new JCheckBox("Horse");

//Create JCheckBox with text(Sheep)
JCheckBox checkBox10=new JCheckBox("Sheep");

//Create a JFrame with title ( Add JCheckBox into JFrame )
JFrame frame=new JFrame("Add JCheckBox into JFrame");

//Create a JPanel that we use to add JCheckBox into it
JPanel panel=new JPanel();

//Create a JPanel that we use to add JCheckBox into it
JPanel panel2=new JPanel();

//Set layout for JFrame
frame.setLayout(new FlowLayout());

//Set layout for first JPanel
panel.setLayout(new FlowLayout());

//Set layout for second JPanel
panel2.setLayout(new GridLayout(5,1));

//Add checkBox1 - checkBox5 into first JPanel
panel.add(checkBox1);
panel.add(checkBox2);
panel.add(checkBox3);
panel.add(checkBox4);
panel.add(checkBox5);

//Add checkBox6 - checkBox10 into second JPanel
panel2.add(checkBox6);
panel2.add(checkBox7);
panel2.add(checkBox8);
panel2.add(checkBox9);
panel2.add(checkBox10);

//Add first JPanel into JFrame
frame.add(panel);

//Add second JPanel into JFrame
frame.add(panel2);

//Set close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set size for JFrame
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

Add JToggleButton into JFrame

Source code below will show how to add JToggleButton into JFrame.

************************************************************************
COMPLETE SOURCE CODE FOR : AddJToggleButtonIntoJFrame.java
************************************************************************


import javax.swing.JToggleButton;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJToggleButtonIntoJFrame
{
public static void main(String[]args)
{
//Create a JToggleButton with text(Java)
JToggleButton toggleButton=new JToggleButton("Java");

//Create a JToggleButton with text(Java_2) and selection state
//If selection state true, JToggleButton will be pressed
JToggleButton toggleButton2=new JToggleButton("Java_2",true);

//Create a JFrame with title(Add toggle button)
JFrame frame=new JFrame("Add toggle button");

//Set layout for JFrame
frame.setLayout(new FlowLayout());

//Add first JToggleButton into JFrame
frame.add(toggleButton);

//Add second JToggleButton into JFrame
frame.add(toggleButton2);

//Set close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Add element in array

Note : Source code below will show you how to add new element in a created array. It will add "monkey" word in an array that contains "lion","tiger","elephant","crocodile" word.

******************************************************
COMPLETE SOURCE CODE FOR : AddElementInArray.java
******************************************************


public class AddElementInArray
{
public static void main(String[]args)
{
//Create array with it's component
String[]animal={"lion","tiger","elephant","crocodile"};

//Add an element - "Monkey"
String a="monkey";

//Step 1:Create temp array (size=(arrayThatWeWantToAddElement)+1)
String[]temp=new String[(animal.length)+1];
System.arraycopy(animal,0,temp,0,animal.length);
temp[temp.length-1]=a;

//Make animal array point to temp array
animal=temp;

//Print all element in animal
int temp2=0;
while(temp2!=animal.length)
{
System.out.println(animal[temp2]);
temp2=temp2+1;
}
}
}


******************************************************
JUST COMPILE AND EXECUTE IT
******************************************************

RELAXING NATURE VIDEO