Skip to main content

Java Swing: JPasswordField, JCheckBox, JRadioButton, JComboBox

Java JPasswordField

The object of a JPasswordField class is a text component specialized for password entry. It allows the editing of a single line of text. It inherits JTextField class.


JPasswordField class declaration

Let's see the declaration for javax.swing.JPasswordField class.

  1. public class JPasswordField extends JTextField  

Commonly used Constructors:

ConstructorDescription
JPasswordField()Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.
JPasswordField(int columns)Constructs a new empty JPasswordField with the specified number of columns.
JPasswordField(String text)Constructs a new JPasswordField initialized with the specified text.
JPasswordField(String text, int columns)Construct a new JPasswordField initialized with the specified text and columns.

Java JPasswordField Example

  1. import javax.swing.*;    
  2. public class PasswordFieldExample {  
  3.     public static void main(String[] args) {    
  4.     JFrame f=new JFrame("Password Field Example");    
  5.      JPasswordField value = new JPasswordField();   
  6.      JLabel l1=new JLabel("Password:");    
  7.         l1.setBounds(20,10080,30);    
  8.          value.setBounds(100,100,100,30);    
  9.             f.add(value);  f.add(l1);  
  10.             f.setSize(300,300);    
  11.             f.setLayout(null);    
  12.             f.setVisible(true);     
  13. }  
  14. }  

Output:

Java Jpasswardfield 1

Java JPasswordField Example with ActionListener

  1. import javax.swing.*;    
  2. import java.awt.event.*;  
  3. public class PasswordFieldExample {  
  4.     public static void main(String[] args) {    
  5.     JFrame f=new JFrame("Password Field Example");    
  6.      final JLabel label = new JLabel();            
  7.      label.setBounds(20,150200,50);  
  8.      final JPasswordField value = new JPasswordField();   
  9.      value.setBounds(100,75,100,30);   
  10.      JLabel l1=new JLabel("Username:");    
  11.         l1.setBounds(20,2080,30);    
  12.         JLabel l2=new JLabel("Password:");    
  13.         l2.setBounds(20,7580,30);    
  14.         JButton b = new JButton("Login");  
  15.         b.setBounds(100,12080,30);    
  16.         final JTextField text = new JTextField();  
  17.         text.setBounds(100,20100,30);    
  18.                 f.add(value); f.add(l1); f.add(label); f.add(l2); f.add(b); f.add(text);  
  19.                 f.setSize(300,300);    
  20.                 f.setLayout(null);    
  21.                 f.setVisible(true);     
  22.                 b.addActionListener(new ActionListener() {  
  23.                 public void actionPerformed(ActionEvent e) {       
  24.                    String data = "Username " + text.getText();  
  25.                    data += ", Password: "   
  26.                    + new String(value.getPassword());   
  27.                    label.setText(data);          
  28.                 }  
  29.              });   
  30. }  
  31. }  

Output:

Java Jpasswardfield 2

ava JCheckBox

The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on ".It inherits JToggleButton class.

JCheckBox class declaration

Let's see the declaration for javax.swing.JCheckBox class.

  1. public class JCheckBox extends JToggleButton implements Accessible  

Commonly used Constructors:

ConstructorDescription
JJCheckBox()Creates an initially unselected check box button with no text, no icon.
JChechBox(String s)Creates an initially unselected check box with text.
JCheckBox(String text, boolean selected)Creates a check box with text and specifies whether or not it is initially selected.
JCheckBox(Action a)Creates a check box where properties are taken from the Action supplied.

Commonly used Methods:

MethodsDescription
AccessibleContext getAccessibleContext()It is used to get the AccessibleContext associated with this JCheckBox.
protected String paramString()It returns a string representation of this JCheckBox.

Java JCheckBox Example

  1. import javax.swing.*;  
  2. public class CheckBoxExample  
  3. {  
  4.      CheckBoxExample(){  
  5.         JFrame f= new JFrame("CheckBox Example");  
  6.         JCheckBox checkBox1 = new JCheckBox("C++");  
  7.         checkBox1.setBounds(100,10050,50);  
  8.         JCheckBox checkBox2 = new JCheckBox("Java"true);  
  9.         checkBox2.setBounds(100,15050,50);  
  10.         f.add(checkBox1);  
  11.         f.add(checkBox2);  
  12.         f.setSize(400,400);  
  13.         f.setLayout(null);  
  14.         f.setVisible(true);  
  15.      }  
  16. public static void main(String args[])  
  17.     {  
  18.     new CheckBoxExample();  
  19.     }}  

Output:

JAVA Jcheckbox 1

Java JCheckBox Example with ItemListener

  1. import javax.swing.*;  
  2. import java.awt.event.*;    
  3. public class CheckBoxExample    
  4. {    
  5.      CheckBoxExample(){    
  6.         JFrame f= new JFrame("CheckBox Example");    
  7.         final JLabel label = new JLabel();            
  8.         label.setHorizontalAlignment(JLabel.CENTER);    
  9.         label.setSize(400,100);    
  10.         JCheckBox checkbox1 = new JCheckBox("C++");    
  11.         checkbox1.setBounds(150,10050,50);    
  12.         JCheckBox checkbox2 = new JCheckBox("Java");    
  13.         checkbox2.setBounds(150,15050,50);    
  14.         f.add(checkbox1); f.add(checkbox2); f.add(label);    
  15.         checkbox1.addItemListener(new ItemListener() {    
  16.              public void itemStateChanged(ItemEvent e) {                 
  17.                 label.setText("C++ Checkbox: "     
  18.                 + (e.getStateChange()==1?"checked":"unchecked"));    
  19.              }    
  20.           });    
  21.         checkbox2.addItemListener(new ItemListener() {    
  22.              public void itemStateChanged(ItemEvent e) {                 
  23.                 label.setText("Java Checkbox: "     
  24.                 + (e.getStateChange()==1?"checked":"unchecked"));    
  25.              }    
  26.           });    
  27.         f.setSize(400,400);    
  28.         f.setLayout(null);    
  29.         f.setVisible(true);    
  30.      }    
  31. public static void main(String args[])    
  32. {    
  33.     new CheckBoxExample();    
  34. }    
  35. }    

Output:

JAVA Jcheckbox 2

Java JCheckBox Example: Food Order

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class CheckBoxExample extends JFrame implements ActionListener{  
  4.     JLabel l;  
  5.     JCheckBox cb1,cb2,cb3;  
  6.     JButton b;  
  7.     CheckBoxExample(){  
  8.         l=new JLabel("Food Ordering System");  
  9.         l.setBounds(50,50,300,20);  
  10.         cb1=new JCheckBox("Pizza @ 100");  
  11.         cb1.setBounds(100,100,150,20);  
  12.         cb2=new JCheckBox("Burger @ 30");  
  13.         cb2.setBounds(100,150,150,20);  
  14.         cb3=new JCheckBox("Tea @ 10");  
  15.         cb3.setBounds(100,200,150,20);  
  16.         b=new JButton("Order");  
  17.         b.setBounds(100,250,80,30);  
  18.         b.addActionListener(this);  
  19.         add(l);add(cb1);add(cb2);add(cb3);add(b);  
  20.         setSize(400,400);  
  21.         setLayout(null);  
  22.         setVisible(true);  
  23.         setDefaultCloseOperation(EXIT_ON_CLOSE);  
  24.     }  
  25.     public void actionPerformed(ActionEvent e){  
  26.         float amount=0;  
  27.         String msg="";  
  28.         if(cb1.isSelected()){  
  29.             amount+=100;  
  30.             msg="Pizza: 100\n";  
  31.         }  
  32.         if(cb2.isSelected()){  
  33.             amount+=30;  
  34.             msg+="Burger: 30\n";  
  35.         }  
  36.         if(cb3.isSelected()){  
  37.             amount+=10;  
  38.             msg+="Tea: 10\n";  
  39.         }  
  40.         msg+="-----------------\n";  
  41.         JOptionPane.showMessageDialog(this,msg+"Total: "+amount);  
  42.     }  
  43.     public static void main(String[] args) {  
  44.         new CheckBoxExample();  
  45.     }  
  46. }  

Output:

JAVA Jcheckbox 21 JAVA Jcheckbox 22

Java JRadioButton

The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz.

It should be added in ButtonGroup to select one radio button only.

JRadioButton class declaration

Let's see the declaration for javax.swing.JRadioButton class.

  1. public class JRadioButton extends JToggleButton implements Accessible  

Commonly used Constructors:

ConstructorDescription
JRadioButton()Creates an unselected radio button with no text.
JRadioButton(String s)Creates an unselected radio button with specified text.
JRadioButton(String s, boolean selected)Creates a radio button with the specified text and selected status.

Commonly used Methods:

MethodsDescription
void setText(String s)It is used to set specified text on button.
String getText()It is used to return the text of the button.
void setEnabled(boolean b)It is used to enable or disable the button.
void setIcon(Icon b)It is used to set the specified Icon on the button.
Icon getIcon()It is used to get the Icon of the button.
void setMnemonic(int a)It is used to set the mnemonic on the button.
void addActionListener(ActionListener a)It is used to add the action listener to this object.

Java JRadioButton Example

  1. import javax.swing.*;    
  2. public class RadioButtonExample {    
  3. JFrame f;    
  4. RadioButtonExample(){    
  5. f=new JFrame();     
  6. JRadioButton r1=new JRadioButton("A) Male");    
  7. JRadioButton r2=new JRadioButton("B) Female");    
  8. r1.setBounds(75,50,100,30);    
  9. r2.setBounds(75,100,100,30);    
  10. ButtonGroup bg=new ButtonGroup();    
  11. bg.add(r1);bg.add(r2);    
  12. f.add(r1);f.add(r2);      
  13. f.setSize(300,300);    
  14. f.setLayout(null);    
  15. f.setVisible(true);    
  16. }    
  17. public static void main(String[] args) {    
  18.     new RadioButtonExample();    
  19. }    
  20. }    

Output:

JAVA Jradiobutton 1

Java JRadioButton Example with ActionListener

  1. import javax.swing.*;    
  2. import java.awt.event.*;    
  3. class RadioButtonExample extends JFrame implements ActionListener{    
  4. JRadioButton rb1,rb2;    
  5. JButton b;    
  6. RadioButtonExample(){      
  7. rb1=new JRadioButton("Male");    
  8. rb1.setBounds(100,50,100,30);      
  9. rb2=new JRadioButton("Female");    
  10. rb2.setBounds(100,100,100,30);    
  11. ButtonGroup bg=new ButtonGroup();    
  12. bg.add(rb1);bg.add(rb2);    
  13. b=new JButton("click");    
  14. b.setBounds(100,150,80,30);    
  15. b.addActionListener(this);    
  16. add(rb1);add(rb2);add(b);    
  17. setSize(300,300);    
  18. setLayout(null);    
  19. setVisible(true);    
  20. }    
  21. public void actionPerformed(ActionEvent e){    
  22. if(rb1.isSelected()){    
  23. JOptionPane.showMessageDialog(this,"You are Male.");    
  24. }    
  25. if(rb2.isSelected()){    
  26. JOptionPane.showMessageDialog(this,"You are Female.");    
  27. }    
  28. }    
  29. public static void main(String args[]){    
  30. new RadioButtonExample();    
  31. }}   

Output:

JAVA Jradiobutton 2

Java JComboBox

The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent class.

JComboBox class declaration

Let's see the declaration for javax.swing.JComboBox class.

  1. public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible  

Commonly used Constructors:

ConstructorDescription
JComboBox()Creates a JComboBox with a default data model.
JComboBox(Object[] items)Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector<?> items)Creates a JComboBox that contains the elements in the specified Vector.

Commonly used Methods:

MethodsDescription
void addItem(Object anObject)It is used to add an item to the item list.
void removeItem(Object anObject)It is used to delete an item to the item list.
void removeAllItems()It is used to remove all the items from the list.
void setEditable(boolean b)It is used to determine whether the JComboBox is editable.
void addActionListener(ActionListener a)It is used to add the ActionListener.
void addItemListener(ItemListener i)It is used to add the ItemListener.

Java JComboBox Example

  1. import javax.swing.*;    
  2. public class ComboBoxExample {    
  3. JFrame f;    
  4. ComboBoxExample(){    
  5.     f=new JFrame("ComboBox Example");    
  6.     String country[]={"India","Aus","U.S.A","England","Newzealand"};        
  7.     JComboBox cb=new JComboBox(country);    
  8.     cb.setBounds(5050,90,20);    
  9.     f.add(cb);        
  10.     f.setLayout(null);    
  11.     f.setSize(400,500);    
  12.     f.setVisible(true);         
  13. }    
  14. public static void main(String[] args) {    
  15.     new ComboBoxExample();         
  16. }    
  17. }   

Output:

JAVA Jcombobox 1

Java JComboBox Example with ActionListener

  1. import javax.swing.*;    
  2. import java.awt.event.*;    
  3. public class ComboBoxExample {    
  4. JFrame f;    
  5. ComboBoxExample(){    
  6.     f=new JFrame("ComboBox Example");   
  7.     final JLabel label = new JLabel();          
  8.     label.setHorizontalAlignment(JLabel.CENTER);  
  9.     label.setSize(400,100);  
  10.     JButton b=new JButton("Show");  
  11.     b.setBounds(200,100,75,20);  
  12.     String languages[]={"C","C++","C#","Java","PHP"};        
  13.     final JComboBox cb=new JComboBox(languages);    
  14.     cb.setBounds(50100,90,20);    
  15.     f.add(cb); f.add(label); f.add(b);    
  16.     f.setLayout(null);    
  17.     f.setSize(350,350);    
  18.     f.setVisible(true);       
  19.     b.addActionListener(new ActionListener() {  
  20.         public void actionPerformed(ActionEvent e) {       
  21. String data = "Programming language Selected: "   
  22.    + cb.getItemAt(cb.getSelectedIndex());  
  23. label.setText(data);  
  24. }  
  25. });           
  26. }    
  27. public static void main(String[] args) {    
  28.     new ComboBoxExample();         
  29. }    
  30. }    

Output:

JAVA Jcombobox 2


Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&