• No results found

New Feature in JDK 1.2.2

In document JAVA (Page 103-106)

Most AWT listener interfaces contain more than one method

UNIT 9: JFC AND SWING COMPONENT

9.3 Overview of JComponent

9.4.6 New Feature in JDK 1.2.2

When you press this button, the event handler will print out the number of times it has been passed.

New Features: Icons, Alignment, and Mnemonics

The most obvious new feature is the ability to associate images with buttons. Swing introduced a utility class called ImageIcon that lets you very easily specify an image file (jpeg or GIF, including animated GIFs). Many Swing controls allow the inclusion of icons. The simplest way to associate an image with a JButton is to pass the ImageIcon to the constructor, either in place of the text or in addition to it. However, a JButton actually allows seven associated images:

1. the main image (use setIcon to specify it if not supplied in the constructor), 2. the image to use when the button is pressed (setPressedIcon),

3. the image to use when the mouse is over it (setRolloverIcon, but you need to call setRolloverEnabled(true) first),

4. the image to use when the button is selected and enabled (setSelectedIcon), 5. the image to use when the button is disabled (setDisabledIcon),

6. the image to use when it is selected but disabled (setDisabledSelectedIcon), and 7. the image to use when the mouse is over it while it is selected

(setRolloverSelectedIcon).

You can also change the alignment of the text or icon in the button (setHorizontalAlignment and setVerticalAlignment; only valid if button is larger than preferred size), and change where the text is relative to the icon (setHorizontalTextPosition, setVerticalTextPosition).

You can easily create buttons with images as well, like this:

Icon spIcon = new ImageIcon (“spam.jpg”);

JButton jb = new JButton (“press here for spam”, splcon);

You can add a keyboard accelerator to a button, and you can give it a symbolic for the text string that it displays. This helps with internationalizing the code.

You can also easily set keyboard mnemonics via setMnemonic. This results in the specified character being underlined on the button, and also results in ALT-char activating the button.

9.4.6 New Feature in JDK 1.2.2

In JDK 1.2.2 and Swing 1.1.1 (and later), Sun added the ability to use HTML to describe the text in

JButtons and JLabels. This lets you easily have multi-line labels, mixed fonts and colors, and other

fancy features.

JButton Example: Source Code

Here is an example of four similar buttons: one with plain text, one with just an image, one with both and the default layout (image to the left, text to the right), and one with the image and text positions reversed.

JButtons.java import java.awt.*;

import javax.swing.*;

public class JButtons extends JFrame { public static void main(String[] args) { new JButtons();

}

public JButtons() {

super("Using JButton");

WindowUtilities.setNativeLookAndFeel();

addWindowListener(new ExitListener());

Container content = getContentPane();

content.setBackground(Color.white);

content.setLayout(new FlowLayout());

JButton button1 = new JButton("Java");

content.add(button1);

ImageIcon cup = new ImageIcon("images/cup.gif");

JButton button2 = new JButton(cup);

content.add(button2);

JButton button3 = new JButton("Java", cup);

content.add(button3);

JButton button4 = new JButton("Java", cup);

button4.setHorizontalTextPosition(SwingConstants.LEFT);

content.add(button4);

pack();

setVisible(true);

} }

Note: also requires WindowUtilities.java and ExitListener.java, shown earlier, plus cup.gif.

JButton Example: Result

9.4.7 JToolTip

This is a text string that acts as a hint or further explanation. You can set it for any JComponent. It appears automatically when the mouse lingers on that component and it disappears when you roll mouse away.

ToolTips don’t generate any events, so there is nothing to handle.

We’ll add a ToolTip to the JLabel that we showed earlier.

The code to create it:

JLabel j1 = new JLabel (“You are a star”, icon, JLabel.Center);

jl.setToolTipText (“you must practice to be a star!”);

Notice that you don’t directly create a JToolTip object. That is done for you behind the scenes. You invoke the set ToolTipText () method of JComponent.

9.4.8 JTextField

This is an area of the screen where you can enter a line of text. There are a couple of subclasses : JTextArea (several lines in size) and JPasswordField (which doesn’t echo what you type in). you can set it to be editable or not-editable.

The Code to create it:

JLabel jl = new JLabel (“Enter your name : ”);

JTextField jtf = new JTextField (25); //field is 25 chars wide The code to retrieve user input from it:

TextFields generate key events on each keystroke and an ActionEvent when the user presses a carriage return. This makes it convenient to validate individual keystrokes as they are typed (as in, ensuring that a field is wholly numeric) and to retrieve all the text when the user has finished typing. The code to get the text look like this :

jft.addActionListener ( new ActionListener ()) { public void actionPerformed (ActionEvent e)

{ System.out.println ( “you entered:” +e.getActionCommand () );}

});

Container c = jframe.getContentPane();

c.add(jl);

c.add(jtf);

In this example, running the program, typing a name, and hitting carriage return will cause the name to be echoed on the Sytem.out. You should write some code to try implementing a listener for each keystroke.

9.4.9 JCheckBox

A CheckBox screen object that represents a Boolean choice: pressed, not pressed, on, or off.

Usually some text explains the choice. For example, a “press for fries” JLabel would have a JCheckBox “Button” allowing yes or no. You can also add an icon to the JCheckBox, just the way you can with JButton.

The code to create it

JCheckBox jck1 = new JCheckBox (“Kannada”);

JCheckBox jck2 = new JCheckBox (“Tamil”);

JCheckBox jck3 = new JCheckBox (“Telugu”);

JCheckBox jck4 = new JCheckBox (“Malayalam”);

Container c = jframe.getContentPane ();

c.add (jck1); c.add(jck2); //etc.

….

The code to retrieve user input from it

CheckBox generates bot ActionEvent and ItemEvent every time you change it. This seems to be for backword compatibility with AWT. We already saw the code to handle ActionEvents with button. The code to register an ItemListener looks like this.

Jck2.addItemListener (new ItemListener () { // anonymous class

public void itemStateChanged (itemEvent e) { if(e.getStateChange () == e.SELECTED)

In this example, running the program and clicking the “Kannada” checkbox will cause the output of selected Kannada in the system console.

Handlers in real programs will do more useful actions as necessary like assigning values and creating objects. The ItemEvent contains fields and methods that specify which objects generated the event and whether it was selected or deselected.

9.4.10 JPanel

In document JAVA (Page 103-106)