Java Reference
In-Depth Information
Finally, every time an event happens, you want to change the font accordingly. This can
be achieved by invoking the paint method. To call the paint method, you need a
Graphics object. So, you invoke the repaint method, which in turn invokes the
paint method. Therefore, you need to call the repaint method before leaving the
event handler method.
The definition of the method itemStateChanged is:
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == boldCB)
{
if (e.getStateChange() == ItemEvent.SELECTED)
intBold = Font.BOLD;
if (e.getStateChange() == ItemEvent.DESELECTED)
intBold = Font.PLAIN;
}
if (e.getSource() == italicCB)
{
if (e.getStateChange() == ItemEvent.SELECTED)
intItalic = Font.ITALIC;
if (e.getStateChange() == ItemEvent.DESELECTED)
intItalic = Font.PLAIN;
}
repaint();
}
Now that the necessary components are written, we can write the complete program.
//Welcome Applet with check boxes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GrandWelcomeCheckBox extends JApplet implements
ItemListener
1
2
{
private int intBold = Font.PLAIN;
private int intItalic = Font.PLAIN;
private JCheckBox boldCB, italicCB;
public void init()
{
Container c = getContentPane();
c.setLayout( null );
boldCB = new JCheckBox("Bold");
italicCB = new JCheckBox("Italic");
Search WWH ::




Custom Search