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.
- public class JPasswordField extends JTextField
Commonly used Constructors:
Constructor | Description |
---|
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
- import javax.swing.*;
- public class PasswordFieldExample {
- public static void main(String[] args) {
- JFrame f=new JFrame("Password Field Example");
- JPasswordField value = new JPasswordField();
- JLabel l1=new JLabel("Password:");
- l1.setBounds(20,100, 80,30);
- value.setBounds(100,100,100,30);
- f.add(value); f.add(l1);
- f.setSize(300,300);
- f.setLayout(null);
- f.setVisible(true);
- }
- }
Output:
Java JPasswordField Example with ActionListener
- import javax.swing.*;
- import java.awt.event.*;
- public class PasswordFieldExample {
- public static void main(String[] args) {
- JFrame f=new JFrame("Password Field Example");
- final JLabel label = new JLabel();
- label.setBounds(20,150, 200,50);
- final JPasswordField value = new JPasswordField();
- value.setBounds(100,75,100,30);
- JLabel l1=new JLabel("Username:");
- l1.setBounds(20,20, 80,30);
- JLabel l2=new JLabel("Password:");
- l2.setBounds(20,75, 80,30);
- JButton b = new JButton("Login");
- b.setBounds(100,120, 80,30);
- final JTextField text = new JTextField();
- text.setBounds(100,20, 100,30);
- f.add(value); f.add(l1); f.add(label); f.add(l2); f.add(b); f.add(text);
- f.setSize(300,300);
- f.setLayout(null);
- f.setVisible(true);
- b.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String data = "Username " + text.getText();
- data += ", Password: "
- + new String(value.getPassword());
- label.setText(data);
- }
- });
- }
- }
Output:
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.
- public class JCheckBox extends JToggleButton implements Accessible
Commonly used Constructors:
Constructor | Description |
---|
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:
Methods | Description |
---|
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
- import javax.swing.*;
- public class CheckBoxExample
- {
- CheckBoxExample(){
- JFrame f= new JFrame("CheckBox Example");
- JCheckBox checkBox1 = new JCheckBox("C++");
- checkBox1.setBounds(100,100, 50,50);
- JCheckBox checkBox2 = new JCheckBox("Java", true);
- checkBox2.setBounds(100,150, 50,50);
- f.add(checkBox1);
- f.add(checkBox2);
- f.setSize(400,400);
- f.setLayout(null);
- f.setVisible(true);
- }
- public static void main(String args[])
- {
- new CheckBoxExample();
- }}
Output:
Java JCheckBox Example with ItemListener
- import javax.swing.*;
- import java.awt.event.*;
- public class CheckBoxExample
- {
- CheckBoxExample(){
- JFrame f= new JFrame("CheckBox Example");
- final JLabel label = new JLabel();
- label.setHorizontalAlignment(JLabel.CENTER);
- label.setSize(400,100);
- JCheckBox checkbox1 = new JCheckBox("C++");
- checkbox1.setBounds(150,100, 50,50);
- JCheckBox checkbox2 = new JCheckBox("Java");
- checkbox2.setBounds(150,150, 50,50);
- f.add(checkbox1); f.add(checkbox2); f.add(label);
- checkbox1.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent e) {
- label.setText("C++ Checkbox: "
- + (e.getStateChange()==1?"checked":"unchecked"));
- }
- });
- checkbox2.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent e) {
- label.setText("Java Checkbox: "
- + (e.getStateChange()==1?"checked":"unchecked"));
- }
- });
- f.setSize(400,400);
- f.setLayout(null);
- f.setVisible(true);
- }
- public static void main(String args[])
- {
- new CheckBoxExample();
- }
- }
Output:
Java JCheckBox Example: Food Order
- import javax.swing.*;
- import java.awt.event.*;
- public class CheckBoxExample extends JFrame implements ActionListener{
- JLabel l;
- JCheckBox cb1,cb2,cb3;
- JButton b;
- CheckBoxExample(){
- l=new JLabel("Food Ordering System");
- l.setBounds(50,50,300,20);
- cb1=new JCheckBox("Pizza @ 100");
- cb1.setBounds(100,100,150,20);
- cb2=new JCheckBox("Burger @ 30");
- cb2.setBounds(100,150,150,20);
- cb3=new JCheckBox("Tea @ 10");
- cb3.setBounds(100,200,150,20);
- b=new JButton("Order");
- b.setBounds(100,250,80,30);
- b.addActionListener(this);
- add(l);add(cb1);add(cb2);add(cb3);add(b);
- setSize(400,400);
- setLayout(null);
- setVisible(true);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent e){
- float amount=0;
- String msg="";
- if(cb1.isSelected()){
- amount+=100;
- msg="Pizza: 100\n";
- }
- if(cb2.isSelected()){
- amount+=30;
- msg+="Burger: 30\n";
- }
- if(cb3.isSelected()){
- amount+=10;
- msg+="Tea: 10\n";
- }
- msg+="-----------------\n";
- JOptionPane.showMessageDialog(this,msg+"Total: "+amount);
- }
- public static void main(String[] args) {
- new CheckBoxExample();
- }
- }
Output:
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.
- public class JRadioButton extends JToggleButton implements Accessible
Commonly used Constructors:
Constructor | Description |
---|
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:
Methods | Description |
---|
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
- import javax.swing.*;
- public class RadioButtonExample {
- JFrame f;
- RadioButtonExample(){
- f=new JFrame();
- JRadioButton r1=new JRadioButton("A) Male");
- JRadioButton r2=new JRadioButton("B) Female");
- r1.setBounds(75,50,100,30);
- r2.setBounds(75,100,100,30);
- ButtonGroup bg=new ButtonGroup();
- bg.add(r1);bg.add(r2);
- f.add(r1);f.add(r2);
- f.setSize(300,300);
- f.setLayout(null);
- f.setVisible(true);
- }
- public static void main(String[] args) {
- new RadioButtonExample();
- }
- }
Output:
Java JRadioButton Example with ActionListener
- import javax.swing.*;
- import java.awt.event.*;
- class RadioButtonExample extends JFrame implements ActionListener{
- JRadioButton rb1,rb2;
- JButton b;
- RadioButtonExample(){
- rb1=new JRadioButton("Male");
- rb1.setBounds(100,50,100,30);
- rb2=new JRadioButton("Female");
- rb2.setBounds(100,100,100,30);
- ButtonGroup bg=new ButtonGroup();
- bg.add(rb1);bg.add(rb2);
- b=new JButton("click");
- b.setBounds(100,150,80,30);
- b.addActionListener(this);
- add(rb1);add(rb2);add(b);
- setSize(300,300);
- setLayout(null);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent e){
- if(rb1.isSelected()){
- JOptionPane.showMessageDialog(this,"You are Male.");
- }
- if(rb2.isSelected()){
- JOptionPane.showMessageDialog(this,"You are Female.");
- }
- }
- public static void main(String args[]){
- new RadioButtonExample();
- }}
Output:
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.
- public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible
Commonly used Constructors:
Constructor | Description |
---|
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:
Methods | Description |
---|
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
- import javax.swing.*;
- public class ComboBoxExample {
- JFrame f;
- ComboBoxExample(){
- f=new JFrame("ComboBox Example");
- String country[]={"India","Aus","U.S.A","England","Newzealand"};
- JComboBox cb=new JComboBox(country);
- cb.setBounds(50, 50,90,20);
- f.add(cb);
- f.setLayout(null);
- f.setSize(400,500);
- f.setVisible(true);
- }
- public static void main(String[] args) {
- new ComboBoxExample();
- }
- }
Output:
Java JComboBox Example with ActionListener
- import javax.swing.*;
- import java.awt.event.*;
- public class ComboBoxExample {
- JFrame f;
- ComboBoxExample(){
- f=new JFrame("ComboBox Example");
- final JLabel label = new JLabel();
- label.setHorizontalAlignment(JLabel.CENTER);
- label.setSize(400,100);
- JButton b=new JButton("Show");
- b.setBounds(200,100,75,20);
- String languages[]={"C","C++","C#","Java","PHP"};
- final JComboBox cb=new JComboBox(languages);
- cb.setBounds(50, 100,90,20);
- f.add(cb); f.add(label); f.add(b);
- f.setLayout(null);
- f.setSize(350,350);
- f.setVisible(true);
- b.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String data = "Programming language Selected: "
- + cb.getItemAt(cb.getSelectedIndex());
- label.setText(data);
- }
- });
- }
- public static void main(String[] args) {
- new ComboBoxExample();
- }
- }
Output:
Anurag Rana
Educator CSE/IT
Comments
Post a Comment