JAVA-MANI.BLOGSPOT.COM
Saturday, June 28, 2008

Labels, Icons and Buttons

Labels are non-interactive text objects most commonly used as prompts. They are created using the JLabel() constructor with the required text as the first parameter. Another parameter can be added using a SwingConstant value to set horizontal alignment. Vertical alignment is through the setVerticalAlignment() method. The contents of a label can be changed with the setText() method.

Icons can be easily added to labels or other controls either to brand, dress up, or aid accessibility. Icons are constructed from the ImageIcon class and then added as a parameter to the label (or other) control. An extra parameter can be used to control the position of the text relative to the icon. It must use one of the SwingConstants values.

ImageIcon icon = new ImageIcon("smile.gif");
JLabel label = new JLabel("hello",icon,SwingConstants.RIGHT);
pane.add(label);

Simple buttons are used to start operations. They are created with the JButton() constructor. They can be deactivated with the setEnabled(false) method and tested with the isEnabled() method. One useful button method is setMnemonic(char) which allows a hot key to be associated with the button. Simple buttons require an ActionEvent event listener that reacts to the button click.

Toggle buttons are a visual push on - push off mechanism. They are created with the JToggleButton() constructor. The isSelected() method returns the state of the button. And in addition to ActionEvent, the ChangeEvent is triggered.

Basic Event Listeners

GUIs are event-based. That is they respond to buttons, keyboard input or mouse activities. Java uses event listeners to monitor activity on specified objects and react to specific conditions. For a listing of useful event listeners . For techniques on organizing many different events in larger projects,

The first step in adding a basic button push event handler to the above example is to import awt.event.* which contains all of the event classes. Next add the phrase implements ActionListener to the class header. Register event listeners for each button widget using the addActionListener(this) method. The reserved word this indicates that the required (by implements ActionListener) handler method called actionPerformed() will be included in the current class. For example:

Bounded-Range Components

Bounded-Range components are components that have a single integer value within fixed integer boundaries. Examples of bounded range control classes are JScrollBar, JSlider and JProgressBar. Each bounded-range component has the following methods: setExtent(), setMaximum(), setMinimum(), setValue(), getValueIsAdjusting() and setOrientation(). Bounded-range components use ChangeEvent to update.

Sliders can be dressed up with ticks and labels. The methods setPaintTicks(true) and setPaintLabels(true) turn them on. setMajorTickSpacing(int) and setMinorTickSpacing(int) set the intervals used for marker ticks. setSnapToTicks(true) forces the slider to the closest tick. setInverted(true) reverses the low and high marks. labelTable is a dictionary of slider values and label objects for painting.

Progress bars indicate status for time consuming jobs. The basic JProgressBar class offers a subtle control but if you want dialog for canceling an operation the classes ProgressMonitor and ProgressMonitorInputStream are better choices. ProgressMonitorInputStream is a stream filter in addition to a progress monitor.

0 comments:

SUBSCRIBE VIA eMAIL

Enter your email address:

Delivered by FeedBurner

Recent Posts

Firefox 3

Counter

internet companies

Live Traffic Map

Subscribe Now