• No results found

ListDataListeners—the controllers

In document Digit Fast Track to Java (Page 122-128)

Java GUI components10 Java

10.12.1 ListDataListeners—the controllers

Adding or Removing An Item from the JList

Whenever you add or remove an item from your list, that is another type of event. To track that event, you use the object

ListDataListener

• If you want to change an item in a JList, you have to get its model, and go through the interface

Java GUI components

Java

10

Methods the implementing classes must override:

• public abstract void addListDataListener( ListDataListener ) • public abstract void removeListDataListener( ListDataListener )

Classes that implement this interface are notified when the contents of the list changes.

One of the three methods defined by the ListDataListener interface class is invoked whenever data associated with a list is modified. Methods the implementing classes must override:

• public abstract void contentsChanged( ListDataEvent ) • public abstract void intervalAdded( ListDataEvent ) • public abstract void intervalRemoved( ListDataEvent )

If just one item is changed, the first method is called. When an “interval” or several contiguous objects in the list are added, either of the last two methods are called.

This event object has three constants that define the change that occurred. • public static final int CONTENTS_CHANGED

• public static final int INTERVAL_ADDED • public static final int INTERVAL_REMOVED

Constructors:

public ListDataEvent( Object source, int type, int index(), int index1 ) This method will tell you the number of rows or items in the list. public abstract int getSize()

This method will give you a reference to a specific Object in the list. public abstract Object getElementAt()

10.13 JComboBox

A JComboBox gets its name because it is a combination of an editable area and a drop-down list of selectable items. In other words, a JComboBox is a more complex JList. Because both display a list of items, they both have models that extend the ListModel interface. Likewise, both are components that that render list cells by implementing the ListCellRenderer interface.

Instances of the JComboBox are—by default—not editable, so you just have to make a simple call to:

JComboBox.setEditable( true ) and you can edit to your heart’s content.

This example is not editable, as you can see by the gray background in the JComboBox. The behavior of these two variants—editable and not editable— is significantly different.

Java GUI components

10

Java

Code Necessary for a JComboBox :

private JComboBox comboBox = new JComboBox(); comboBox.addItem( “Top” ); comboBox.addItem( “Center” ); comboBox.addItem( “Bottom” ); comboBox.setEditable( true ); comboBox.getEditor().addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) { System.out.println("here" + comboBox.getSelectedItem() ); } } );

This example uses a default constructor. You could also have used: • public JComboBox( ComboBoxModel )

• public JComboBox( Object[] ) • public JComboBox( Vector )

The most common way you will use a JComboBox is for making another object react to the choices expressed by the user. For this purpose, you implement the

ItemListener interface.

String[] numbers = { ‘one’, ‘two’, ‘three’, ‘four’ }; JComboBox cb = new JComboBox( numbers );

cb.setMaximumRowCount( 3 );

cb.addItemListener( new Itemlistener() {

public void itemStateChanged( ItemEvent e ) {

someJLabel.setText( numbers[ cb.getSelectedIndex() ] ); }

} );

cb.getSelectedIndex()

Java GUI components

Java

10

which returns a String, which becomes the text.

Event handling in a JComboBox is tricky for several reasons. When their selected item changes—either by being selected, or by having them be edited— every JComboBox fires two events.

1.For deselecting the previous item. 2.For selecting the current item.

Also, whenever an item event is fired, an action event is also fired right away.

Because Combo Boxes can be edited, they can fire ActionEvents that you can track with an ActionListener. In fact, whenever the currently displayed value is edited, the combo box fires an action event.

public class Test extends JApplet {

private JComboBox comboBox = new JComboBox();

private ComboBoxEditor editor = comboBox.getEditor(); public void init()

{ Container c = getContentPane(); comboBox.setEditable(true); comboBox.addItem("Top"); comboBox.addItem("Center"); comboBox.addItem("Bottom"); c.setLayout(new FlowLayout()); c.add(comboBox);

editor.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

String s = (String) editor.getItem(); showStatus("Item Edited: " + s); }

}); } }

This is an interface, it has method getItem(), which returns an Object. Once we get an Object out of the editor, we cast it into type String, and discover what the edit was that triggered the event. As you might imagine, this is a highly customizable component.

Java GUI components

10

Java

10.14 JTextArea

This familiar component can display multiple lines. It is basic in its function. For complex abilities, choose the JEditorPane and JTextPane. It also does not implement scrolling by itself. You can specify its word wrapping behavior.

JTextArea txt = new JTextArea(); txt.setLineWrap( true );

Properties maintained by the JTextArea: • columns: int—the number of columns displayed

• line count: int—the number of lines of text contained in the text area. • line wrap: boolean—determines whether or not lines of text are wrapped at the right edge of a text area.

• rows: int—the number of lines of text displayed in a text area.

• tabSize: int—number of characters inserted when the tab key is pressed. • wrapStyleWord: boolean—when true, causes words to be wrapped on words. If false, wrap is on characters.

10.15 JEditorPane

Just like JTextAreas, these are capable of displaying multiple lines of editable text. Unlike text areas, JEditorPanes can display HTML and RTF formats.

JEditorPane editor = new JEditorPane(); editor.setPage( url );

Hyperlink events are fired when a mouse pressed event occurs over a hyperlink in an editor pane that is displaying an HTML document. Naturally, Swing defines a HyperLinkListener interface and HyperLinkEvent class that permits hyperlink events to be handled.

public abstract void

hyperlinkUpdate( HyperlinkEvent h )

The HyperlinkEvent object offers the following methods: HyperlinkEvent:

• public String getDescription() • public HyperlinkEvent.EventType getEventType()

• public URL getURL() Example:

JEditorPane editor = new JEditorPane(); …

editor.addHyperlinkListener( new HyperlinkListener()

Java GUI components

Java

10

{

public void hyperlinkUpadate( HyperLinkEvent e ) {

editor.setPane( e.getURL() ); }

} );

In document Digit Fast Track to Java (Page 122-128)

Related documents