JTOGGLE BUTTON:
```````````````````````````
Here ItemListener interface is used instead of ActionListener
Here we use ItemEvent\ instead of ActionEvent.
Here we use itemStateChanged is used instead of ActionListener method.
STATES OF TOGGLE BUTTON
`````````````````````````````````````````````
1) Pressed state
2)Release state
PROGRAM:
````````````````
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class toggle extends JApplet implements ItemListener
{
JToggleButton jtb=new JToggleButton("ON/OFF");
JLabel jl= new JLabel("PRESS THE BUTTON");
public void init()
{
setLayout(new FlowLayout());
add(jl);
add(jtb);
showStatus("PRESS A BUTTON");//to display the given msg in status bar
jtb.addItemListener(this);
}
public void itemStateChanged(ItemEvent arg0)
{
if(jtb.isSelected())
{
jl.setText("BUTTON PRESSED");//replace the label by this message using setText() after the button is toggled
showStatus("BUTTON PRESSED");//displays the message in statusbar after the button is toggled
}
}
}
OUTPUT:
```````````````````````````
Here ItemListener interface is used instead of ActionListener
Here we use ItemEvent\ instead of ActionEvent.
Here we use itemStateChanged is used instead of ActionListener method.
STATES OF TOGGLE BUTTON
`````````````````````````````````````````````
1) Pressed state
2)Release state
PROGRAM:
````````````````
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class toggle extends JApplet implements ItemListener
{
JToggleButton jtb=new JToggleButton("ON/OFF");
JLabel jl= new JLabel("PRESS THE BUTTON");
public void init()
{
setLayout(new FlowLayout());
add(jl);
add(jtb);
showStatus("PRESS A BUTTON");//to display the given msg in status bar
jtb.addItemListener(this);
}
public void itemStateChanged(ItemEvent arg0)
{
if(jtb.isSelected())
{
jl.setText("BUTTON PRESSED");//replace the label by this message using setText() after the button is toggled
showStatus("BUTTON PRESSED");//displays the message in statusbar after the button is toggled
}
}
}
OUTPUT:
SCREEN: BEFORE BUTTON PRESSED
SCREEN: AFTER BUTTON PRESSED
No comments:
Post a Comment