Listing 8.1 JButton Used with Different Look-and-Feels (TJButton.java)
// Demonstrates the Swing Buttons...
/*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TJButton extends JApplet {
//1. Declare the Swing buttons.
JButton button1, button2, button3;
//2. Icons for the buttons 1, 2, and 3.
Icon metalIcon = new ImageIcon("metal.gif");
Icon motifIcon = new ImageIcon("motif.gif");
Icon windowsIcon = new ImageIcon("windows.gif");
//3. String forms of different look-and-feel.
String metallicLF =
"javax.swing.plaf.metal.MetalLookAndFeel";
String motifLF =
"com.sun.java.swing.plaf.motif.MotifLookAndFeel";
String windowsLF =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
public void init() {
//4. Creating grid layout and setting it for the applet.
GridLayout grid = new GridLayout(1,3,5,5);
this.getContentPane().setLayout(grid);
this.getContentPane().setBackground(Color.lightGray);
//5. Creating and adding Swing button1.
button1 = new JButton("Java L&F",metalIcon);
button1.setVerticalTextPosition(SwingConstants.BOTTOM);
button1.setHorizontalTextPosition(SwingConstants.CENTER);
button1.setMnemonic('J');
button1.setEnabled(false);
button1.setActionCommand(metallicLF);
CustomListener cl = new CustomListener(this);
button1.addActionListener(cl);
this.getContentPane().add(button1);
//6. Creating and adding Swing button2.
button2 = new JButton("Motif L&F",motifIcon);
button2.setVerticalTextPosition(SwingConstants.BOTTOM);
button2.setHorizontalTextPosition(SwingConstants.CENTER);
button2.setMnemonic('M');
button2.setActionCommand(motifLF);
button2.addActionListener(cl);
this.getContentPane().add(button2);
//7. Creating and adding Swing button3.
button3 = new JButton("Windows L&F",windowsIcon);
button3.setVerticalTextPosition(SwingConstants.BOTTOM);
button3.setHorizontalTextPosition(SwingConstants.CENTER);
button3.setMnemonic('W');
button3.setActionCommand(windowsLF);
button3.addActionListener(cl);
this.getContentPane().add(button3);
}
//8. Inner class that supports event handling.
class CustomListener implements ActionListener {
TJButton testApplet = null;
public CustomListener(TJButton testApplet) {
this.testApplet = testApplet;
}
public void actionPerformed(ActionEvent evt) {
//9. For pluggable look-and-feel.
String LF = evt.getActionCommand();
try {
UIManager.setLookAndFeel(LF);
SwingUtilities.updateComponentTreeUI(testApplet);
JButton button = (JButton) evt.getSource();
button.setEnabled(false);
updateStatus(button);
} catch (Exception e) {
System.err.println("Look&Feel not set for "
+LF+" !");
}
}
//10. Updates the status of each button (Enabled/Disabled).
public void updateStatus(JButton button) {
if (button == button1) {
button2.setEnabled(true);
button3.setEnabled(true);
}
else if (button == button2) {
button1.setEnabled(true);
button3.setEnabled(true);
}
else if (button == button3) {
button1.setEnabled(true);
button2.setEnabled(true);
}
}
}
}
Listing 8.2 JToggleButton Used to Create a Simple Audio Player (TJToggleButton.java)
// Demonstrates the Toggle Buttons...
/*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class TJToggleButton extends JApplet {
// 1. Declare a couple of toggle button references.
JToggleButton playButton,stopButton;
// 2. Audio support.
AudioClip audioClip = null;
// 3.Icons to be used for the Stop button.
Icon enabledIcon = new TestIcon(Color.red);
Icon disabledIcon = new TestIcon(Color.gray);
String LF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
public void init() {
// 4. Sets the Motif look-and-feel.
try {
UIManager.setLookAndFeel(LF);
} catch (Exception e) {
System.err.println("Look and Feel not Initialized!");
}
// 5. Create and set the layout.
GridLayout grid = new GridLayout(1,2);
this.getContentPane().setLayout(grid);
this.getContentPane().setBackground(Color.gray);
// 6. Create and add a Play button.
ImageIcon playIcon = new ImageIcon("play.gif");
playButton = new JToggleButton("Play",playIcon);
playButton.setBackground(Color.lightGray);
playButton.setForeground(Color.blue);
playButton.setMnemonic('p');
CustomListener cl = new CustomListener();
playButton.addActionListener(cl);
this.getContentPane().add(playButton);
// 7. Create and add a Stop button.
stopButton = new JToggleButton("Stop",enabledIcon);
stopButton.setVerticalTextPosition(SwingConstants.BOTTOM);
stopButton.setHorizontalTextPosition(SwingConstants.CENTER);
stopButton.setBackground(Color.lightGray);
stopButton.setForeground(Color.blue);
stopButton.setMnemonic('s');
stopButton.addActionListener(cl);
this.getContentPane().add(stopButton);
// 8. Initialize the audio clip.
if (audioClip == null) {
audioClip = this.getAudioClip(getCodeBase(),
"music.au");
}
}
// 9. Listener class for the button objects.
class CustomListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JToggleButton button = (JToggleButton) e.getSource();
// 10. If the play button is clicked...
if (button == playButton) {
playButton.setEnabled(false);
if (!stopButton.isEnabled())
stopButton.setEnabled(true);
stopButton.setIcon(enabledIcon);
audioClip.loop();
}
// 11. If the stop button is clicked...
else if (button == stopButton) {
stopButton.setEnabled(false);
stopButton.setIcon(disabledIcon);
if (!playButton.isEnabled())
playButton.setEnabled(true);
audioClip.stop();
}
}
}
}
/*
* 12. A class that implements the Icon interface;
* a slight modification (to display the specified
* colors) of the TestIcon shown in chapter-7.
*/
class TestIcon implements Icon {
int width = 20; // default icon-width
int height = 20; // default icon-height
Color fillColor; // icon color to be specified
// 13. Constructor that specifies the icon color.
public TestIcon(Color fillColor) {
this.fillColor = fillColor;
}
// 14. Interface method to paint the icon.
// using the methods in graphics context object
public void paintIcon(Component c,
Graphics g,
int x,
int y) {
g.setColor(fillColor);
g.fillOval(x, y, width, height);
g.setColor(Color.black);
g.drawOval(x, y, width, height);
}
// 15. Interface method to retrieve the icon width.
public int getIconWidth() {
return width;
}
// 16. Interface method to retrieve the icon height.
public int getIconHeight() {
return height;
}
}
Listing 8.3 JCheckBox (TJCheckBox.java)
/*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TJCheckBox extends JApplet {
JLabel label2 = null;
public void init() {
//1. Obtain a handle on the JApplet container.
Container container = this.getContentPane();
//2. Set the layout.
container.setLayout(new GridLayout(5, 1));
//3. Create the radio buttons.
JCheckBox cBox1, cBox2, cBox3;
cBox1 = new JCheckBox("Light-weight!");
cBox2 = new JCheckBox("Ready-made!");
cBox3 = new JCheckBox("Easy-to-use!");
//4. Add the item listener to the check boxes.
CheckBoxListener listener = new CheckBoxListener();
cBox1.addItemListener(listener);
cBox2.addItemListener(listener);
cBox3.addItemListener(listener);
//5. Set the text position of each button.
cBox1.setHorizontalTextPosition(SwingConstants.LEFT);
cBox1.setVerticalTextPosition(SwingConstants.BOTTOM);
cBox2.setHorizontalTextPosition(SwingConstants.CENTER);
cBox2.setVerticalTextPosition(SwingConstants.BOTTOM);
cBox3.setHorizontalTextPosition(SwingConstants.RIGHT);
cBox3.setVerticalTextPosition(SwingConstants.BOTTOM);
//6. Set the position of each button in its display area.
cBox1.setHorizontalAlignment(SwingConstants.LEFT);
cBox2.setHorizontalAlignment(SwingConstants.CENTER);
cBox3.setHorizontalAlignment(SwingConstants.RIGHT);
//7. Create a couple of label to notify what to do and
// what happened.
Font font = new Font("Monospaced", Font.ITALIC, 16);
JLabel label1 = new JLabel("The JFC/Swing Widgets are:");
label1.setFont(font);
label2 = new JLabel("Select the Check Boxes...");
label2.setFont(font);
//8. Finally, addeach component to the Swing applet.
container.add(label1);
container.add(cBox1);
container.add(cBox2);
container.add(cBox3);
container.add(label2);
}
//9. Listener class for check boxes.
class CheckBoxListener implements ItemListener {
private double n = 0.0;
public void itemStateChanged(ItemEvent evt) {
n = n + 100.0/3.0;
label2.setText("Your Answer is "
+ new Double(n).toString()
+ "% Correct!");
JCheckBox cBox = (JCheckBox) evt.getSource();
cBox.setEnabled(false);
}
}
}
Listing 8.4 JRadioButton Used to Create Two Sets of Radio Buttons (TJRadioButton.java)
/*
*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TJRadioButton extends JApplet {
// 1. Declare the object references.
JRadioButton rButton1, rButton2, rButton3;
JRadioButton rButton4, rButton5, rButton6;
JLabel label = null;
String message = null;
Font font = null;
Container container;
String LF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
public void init() {
// 2. Set the look-and-feel of the applet to the Motif type.
try {
UIManager.setLookAndFeel(LF);
} catch (Exception e) {
System.err.println("Look and Feel not Initialized!");
}
// 3. Get a handle on the JApplet container.
container = this.getContentPane();
// 4. Initialize the font object.
font = new Font("Serif", Font.PLAIN, 16);
// 5. Initialize the message.
message = "Check or Uncheck any Radio Button?";
// 6. Create grid layout and set it for the JApplet.
container.setLayout(new GridLayout(2, 1));
// 7. Create the JRadioButton objects.
rButton1 = new JRadioButton("Serif", true);
CustomListener cl = new CustomListener();
rButton1.addActionListener(cl);
rButton2 = new JRadioButton("SansSerif");
rButton2.addActionListener(cl);
rButton3 = new JRadioButton("Monospaced");
rButton3.addActionListener(cl);
// 8. Form a group with these radio buttons.
ButtonGroup group1 = new ButtonGroup();
group1.add(rButton1);
group1.add(rButton2);
group1.add(rButton3);
// 9. Create the remaining JRadioButton objects.
rButton4 = new JRadioButton("Plain", true);
rButton4.addActionListener(cl);
rButton5 = new JRadioButton("Bold");
rButton5.addActionListener(cl);
rButton6 = new JRadioButton("Italic");
rButton6.addActionListener(cl);
// 10. Form a group with these radio buttons.
ButtonGroup group2 = new ButtonGroup();
group2.add(rButton4);
group2.add(rButton5);
group2.add(rButton6);
// 11. Create a JPanel and main label objects.
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
Icon picture = new ImageIcon("pointer.gif");
label = new JLabel(message, picture, SwingConstants.CENTER);
// 12. Add the radio buttons to JPanel.
panel.add(new JLabel("Font"));
panel.add(new JLabel("Font Type"));
panel.add(rButton1); panel.add(rButton4);
panel.add(rButton2); panel.add(rButton5);
panel.add(rButton3); panel.add(rButton6);
// 13. Add JPanel and canvas to the JApplet.
container.add(panel);
container.add(label);
}
// 14. The item listener class.
class CustomListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JRadioButton rButton = (JRadioButton) evt.getSource();
if (rButton == rButton1) {
font = new Font("Serif", font.getStyle(), 16);
label.setFont(font);
label.setText("You selected the SERIF font!");
container.validate();
}
else if (rButton == rButton2) {
font = new Font("SansSerif", font.getStyle(), 16);
label.setFont(font);
label.setText("You selected the SANSSERIF font!");
container.validate();
}
else if (rButton == rButton3) {
font = new Font("Monospaced", font.getStyle(), 16);
label.setFont(font);
label.setText("You selected the MONOSPACED font!");
container.validate();
}
else if (rButton == rButton4) {
font = new Font(font.getName(), Font.PLAIN, 16);
label.setFont(font);
label.setText("You selected the PLAIN style!");
container.validate();
}
else if (rButton == rButton5) {
font = new Font(font.getName(), Font.BOLD, 16);
label.setFont(font);
label.setText("You selected the BOLD style!");
label.validate();
}
else if (rButton == rButton6) {
font = new Font(font.getName(), Font.ITALIC, 16);
label.setFont(font);
label.setText("You selected the ITALIC style!");
container.validate();
}
}
}
}