0% found this document useful (0 votes)
29 views9 pages

Swing Lect4

The JPasswordField class allows for password entry in a single line text field. It inherits from JTextField and can be constructed with or without an initial text string and number of columns. Code examples show how to create a JPasswordField, add it to a frame along with other components like labels and buttons, and use an action listener to retrieve the password text when a button is clicked.

Uploaded by

rajeshchoudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views9 pages

Swing Lect4

The JPasswordField class allows for password entry in a single line text field. It inherits from JTextField and can be constructed with or without an initial text string and number of columns. Code examples show how to create a JPasswordField, add it to a frame along with other components like labels and buttons, and use an action listener to retrieve the password text when a button is clicked.

Uploaded by

rajeshchoudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Jpassword Field

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.
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 Construct a new JPasswordField


columns) initialized with the specified text
and columns.
Using JPasswordField
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);
}
}
Using Action Listener
import javax.swing.*;
import java.awt.event.*;
public class PasswordFieldExample { b.addActionListener(new ActionListener() {
public static void main(String[] args) { public void actionPerformed(ActionEvent e)
JFrame f=new JFrame("Password Field Example"); {
final JLabel label = new JLabel();
String data = "Username " + text.getText();
label.setBounds(20,150, 200,50);
data += ", Password: "
final JPasswordField value = new JPasswordField();
value.setBounds(100,75,100,30);
+ new String(value.getPassword());
JLabel l1=new JLabel("Username:"); label.setText(data);
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);
Java 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.
public class JCheckBox extends JToggleButton implements Accessible
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.
CHECK BOX
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();
}}
Java JCheckBox Example with ItemListener
checkbox2.addItemListener(new ItemListener()
import javax.swing.*;
import java.awt.event.*;
{
public class CheckBoxExample public void itemStateChanged(ItemEvent e)
{ {
CheckBoxExample(){ label.setText("Java Checkbox: "
JFrame f= new JFrame("CheckBox Example"); + (e.getStateChange()==1?"checked":"un
final JLabel label = new JLabel(); checked"));
label.setHorizontalAlignment(JLabel.CENTER); }
label.setSize(400,100); });
JCheckBox checkbox1 = new JCheckBox("C++"); f.setSize(400,400);
checkbox1.setBounds(150,100, 50,50);
f.setLayout(null);
JCheckBox checkbox2 = new JCheckBox("Java");
f.setVisible(true);
checkbox2.setBounds(150,150, 50,50);
}
f.add(checkbox1); f.add(checkbox2); f.add(label);
public static void main(String args[])
checkbox1.addItemListener(new ItemListener() { {
new CheckBoxExample();
public void itemStateChanged(ItemEvent e) }
{ }
label.setText("C++ Checkbox: "
+ (e.getStateChange()==1?"checked":"unchecked"));
}
});
import javax.swing.*;
Example
import java.awt.event.*;
public class CheckBoxExample extends JFrame implement
s ActionListener{ public void actionPerformed(ActionEvent e){
JLabel l; float amount=0;
JCheckBox cb1,cb2,cb3;
String msg="";
if(cb1.isSelected())
JButton b;
{
CheckBoxExample(){
amount+=100;
l=new JLabel("Food Ordering System"); msg="Pizza: 100\n";
l.setBounds(50,50,300,20); }
cb1=new JCheckBox("Pizza @ 100"); if(cb2.isSelected())
cb1.setBounds(100,100,150,20); {
cb2=new JCheckBox("Burger @ 30"); amount+=30;
cb2.setBounds(100,150,150,20); msg+="Burger: 30\n";
cb3=new JCheckBox("Tea @ 10"); }
cb3.setBounds(100,200,150,20); if(cb3.isSelected()){
amount+=10;
b=new JButton("Order");
msg+="Tea: 10\n";
b.setBounds(100,250,80,30);
}
b.addActionListener(this); msg+="-----------------\n";
add(l);add(cb1);add(cb2);add(cb3);add(b); JOptionPane.showMessageDialog(this,msg+"Total: "+a
setSize(400,400); mount);
setLayout(null); }
setVisible(true); public static void main(String[] args) {
setDefaultCloseOperation(EXIT_ON_CLOSE); new CheckBoxExample();
} }
}
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.
Methods Description
Constructor Description
void setText(String s) It is used to set specified text on
JRadioButton() Creates an unselected button.
radio button with no text.
String getText() It is used to return the text of the
button.

JRadioButton(String s) Creates an unselected void setEnabled(boolean b) It is used to enable or disable the


radio button with button.
specified text. 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
JRadioButton(String s, Creates a radio button button.
boolean selected) with the specified text and
selected status. void setMnemonic(int a) It is used to set the mnemonic on
the button.
void It is used to add the action listener
addActionListener(ActionListener to this object.
a)
JRadioButton
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male"); public void actionPerformed(ActionEvent e){
JRadioButton r2=new JRadioButton("B) Female");
if(rb1.isSelected()){
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
JOptionPane.showMessageDialog(this,"You are
ButtonGroup bg=new ButtonGroup(); Male.");
bg.add(r1);bg.add(r2); }
f.add(r1);f.add(r2); if(rb2.isSelected()){
f.setSize(300,300); JOptionPane.showMessageDialog(this,"You are
f.setLayout(null);
Female.");
f.setVisible(true);
}
}
public static void main(String[] args) { }
new RadioButtonExample(); public static void main(String args[]){
} new RadioButtonExample();
} b.addActionListener(this); }}

You might also like