


JToggleButton(String
str)
Here,
a toggle button is created that contains the string str.




boolean
isSelected()
It
returns true if the button is selected otherwise false.

import
java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;
public
class MyToggleButton extends JApplet
{
JLabel l1;
JToggleButton jb;
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
makeGUI();
}
});
} catch(Exception e){
System.out.println("Sorry some
error occured "+e);
}
}
private void makeGUI()
{
setLayout(new FlowLayout());
l1=new JLabel("Off");
jb=new JToggleButton("On/Off");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent
ae){
if(jb.isSelected())
l1.setText("On");
else
l1.setText("Off");
}
});
add(jb);
add(l1);
}
Output:
No comments:
Write comments