6.2 JTextField
JTextField is used for taking input of single line of text. It is most widely used text component. It has three constructors,
1 2 3 |
JTextField(int cols) JTextField(String str, int cols) JTextField(String str) |
xample using JTextField
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MyTextField extends JFrame { public MyTextField() { JTextField jtf = new JTextField(20); //creating JTextField. add(jtf); //adding JTextField to frame. setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setVisible(true); } public static void main(String[] args) { new MyTextField(); } } |
6.3 JButton
JButton class provides functionality of a button. JButton class has three constuctors,
1 2 3 |
JButton(Icon ic) JButton(String str) JButton(String str, Icon ic) |
It allows a button to be created using icon, a string or both. JButton supports ActionEvent. When a button is pressed an ActionEvent is generated.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class testswing extends JFrame { testswing() { JButton bt1 = new JButton("Yes"); //Creating a Yes Button. JButton bt2 = new JButton("No"); //Creating a No Button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) //setting close operation. setLayout(new FlowLayout()); //setting layout using FlowLayout object setSize(400, 400); //setting size of Jframe add(bt1); //adding Yes button to frame. add(bt2); //adding No button to frame. setVisible(true); } public static void main(String[] args) { new testswing(); } } |
1 |
6.5 JCheckBox
JCheckBox class is used to create checkboxes in frame. Following is constructor for JCheckBox,
1 |
JCheckBox(String str) |
Example using JCheckBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Test extends JFrame { public Test() { JCheckBox jcb = new JCheckBox("yes"); //creating JCheckBox. add(jcb); //adding JCheckBox to frame. jcb = new JCheckBox("no"); //creating JCheckBox. add(jcb); //adding JCheckBox to frame. jcb = new JCheckBox("maybe"); //creating JCheckBox. add(jcb); //adding JCheckBox to frame. setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setVisible(true); } public static void main(String[] args) { new Test(); } } |
6.5 JRadioButton
Radio button is a group of related button in which only one can be selected. JRadioButton class is used to create a radio button in Frames. Following is the constructor for JRadioButton,
1 |
JRadioButton(String str) |
Example using JRadioButton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Test extends JFrame { public Test() { JRadioButton jcb = new JRadioButton("A"); //creating JRadioButton. add(jcb); //adding JRadioButton to frame. jcb = new JRadioButton("B"); //creating JRadioButton. add(jcb); //adding JRadioButton to frame. jcb = new JRadioButton("C"); //creating JRadioButton. add(jcb); //adding JRadioButton to frame. jcb = new JRadioButton("none"); add(jcb); setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setVisible(true); } public static void main(String[] args) { new Test(); } } |
6.9 JComboBox
Combo box is a combination of text fields and drop-down list.JComboBox component is used to create a combo box in Swing. Following is the constructor for JComboBox,
1 |
JComboBox(String arr[]) |
Example using JComboBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Test extends JFrame { String name[] = {"Abhi","Adam","Alex","Ashkay"}; //list of name. public Test() { JComboBox jc = new JComboBox(name); //initialzing combo box with list of name. add(jc); //adding JComboBox to frame. setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setVisible(true); } public static void main(String[] args) { new Test(); } } |