0% found this document useful (0 votes)
5 views

2.2 Swing components part-3

Uploaded by

nihalpatil0021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2.2 Swing components part-3

Uploaded by

nihalpatil0021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

JPasswordField

-JPasswordField class is a text component specialized for password entry.


-It inherits JTextField class.
Constructor-
1.JPasswordField(): constructor that creates a new PasswordField
2. JPasswordField(int columns) : constructor that creates a new empty PasswordField with specified
number of columns.
3. JPasswordField(String Password) : constructor that creates a new empty Password field initialized
with the given string.
4. JPasswordField(String Password, int columns) : constructor that creates a new empty PasswordField
with the given string and a specified number of columns
Methods-
1.char getEchoChar() : returns the character used for echoing in JPasswordField.
2.setEchoChar(char c) : set the echo character for JPasswordField.
3.String getPassword() : returns the text contained in JPasswordField.
4.String getText() : returns the text contained in JPasswordField.
Develop a program to add passwordfield on applet using swing
import javax.swing.*;
import java.awt.*;
public class pass extends JApplet
{
JLabel j1=new JLabel("Enter Password");
JPasswordField v = new JPasswordField(10);
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(j1); cp.add(v);
}
}

/* <applet code=pass.class width=200 height=300></applet> */


JComboBox
-JComboBox shows a popup menu that shows a list and the user can select a option from that specified
list .
-JComboBox can be editable or read- only depending on the choice of the programmer .
Constructor-
1. JComboBox()- Creates a JComboBox with a default data model.
2. JComboBox(Object[] items)- Creates a JComboBox that contains the elements in the specified array
3. JComboBox(Vector<?> items)- Creates a JComboBox that contains the elements in the
specified vector
Methods-
1.void addItem(Object anObject)- used to add an item to the item list.
2. void removeItem(Object anObject)- used to delete an item to the item list.
3. void removeAllItems()-used to remove all the items from the list.
4. void setEditable(boolean b)- used to set the JComboBox is editable.
5. int getItemCount()- Returns the number of items in the list.
6. int getSelectedIndex()- Returns the first item in the list that matches the given item.
7. boolean isEditable()- Returns true if the JComboBox is editable.
8. Object getSelectedItem( )- obtain the item selected in the list.
Develop a program to add combobox on applet using swing
import javax.swing.*;
import java.awt.*;
public class jcombo extends JApplet
{

String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);
JLabel j2=new JLabel("Select");
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(j2);cp.add(cb);
}
}

/* <applet code=jcombo.class width=200 height=300></applet> */


JCheckBox
-JCheckBox class provides the functionality of a check box.
-work as two-state buttons
-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.
Constructor-
1. JCheckBox()- Creates an initially unselected check box button with no text, no icon.
2. JChechBox(String s)- Creates an initially unselected check box with text.
3. JCheckBox(String text, boolean selected)-Creates a check box with text and specifies whether or not
it is initially selected.
Methods-
1. void setSelected(boolean b) : sets the checkbox to selected if boolean value passed is true or vice
versa
2. setText(String s) :sets the text of the checkbox to the given text
3.String getText() : returns the text of the checkbox
Develop a program to add checkbox on applet using swing
import javax.swing.*;
import java.awt.*;
public class jcheck extends JApplet
{

JCheckBox c1 = new JCheckBox("Airtel",true);


JCheckBox c2 = new JCheckBox("Idea");
JLabel j2=new JLabel("Select");
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(j2);cp.add(c1);cp.add(c2);
}
}

/* <applet code=jcheck.class width=200 height=300></applet> */


JRadioButton
-The JRadioButton class is used to create a radio button.
- It should be added in ButtonGroup to select one radio button only.
Constructor-
1. JRadioButton()- Creates an unselected radio button with no text.
2. JRadioButton(String s)- Creates an unselected radio button with specified text.
3. JRadioButton(String s, boolean selected) - Creates a radio button with the specified text and
selected status.
4. 5. JRadioButton(String s,boolean selected)
5. JRadioButton(String s, ImageIcon obj,boolean selected)
Methods-
1. void setText(String s)- used to set specified text on button.
2. String getText() - used to return the text of the button.
3. void setEnabled(boolean b)- used to enable or disable the button.
4. void setIcon(Icon b)- used to set the specified Icon on the button.
5. Icon getIcon()- used to get the Icon of the button.
6 boolean isSelected()- check current radio button is selected or not
Develop a program to add radiobutton on applet using swing
import javax.swing.*;
import java.awt.*;
public class radio extends JApplet
{
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
ButtonGroup bg=new ButtonGroup();
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
bg.add(r1); bg.add(r2);
cp.add(r1); cp.add(r2);
}
}
/* <applet code=radio.class width=200 height=300></applet> */
JList
- JList class represents a list of text items.
Constructor-
1. JList(): creates an empty blank list
2. JList(ary[] listData)- creates an new list with the elements of the array.
3. JList(Vector l) : creates a new list with the elements of the vector

Methods-
1.int getSelectedIndex()- returns the index of selected item of the list
2. String getSelectedValue()- returns the selected value of the element of the list
3.void setSelectedIndex(int i)- sets the selected index of the list to I
4.void addElement(Object obj)-add the object into list.
5.void setVisibleRowCount(int v)- set how many visible rows you want
Develop a program to add radiobutton on applet using swing
import javax.swing.*;
import java.awt.*;
public class list extends JApplet
{
String week[]= { "Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday"};
JList b;
public void init()
{
b= new JList(week);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b);
}
}

/* <applet code=list.class width=200 height=300></applet> */

You might also like