JTabbedPane
JTabbedPane is simply a stack of components in selectable layers. Each layer can contain one component which is normally a container. Tab extensions are used to move a given layer to the front of the tabbed pane view. These tab extensions are similar to labels in that they can have assigned text, an icon as well as a disabled icon , background and foreground colors, and a tooltip. To add a component to a tabbed pane, you use one of its overloaded add methods. This creates a new selectable tab...
Example
import javax.swing. import javax.swing.event. import java.beans. import j ava.awt. import java.io. public class BakedBean extends JComponent implements Externalizable Property names only needed for bound or constrained properties public static final String BEAN_VALUE Value public static final String BEAN_COLOR Color private Font m_beanFont simple private Dimension m_beanDimension simple private int m_beanValue bound private Color m_beanColor constrained private String m_beanString change...
Custom JToolBar separators
Unfortunately, Swing does not include a toolbar-specific separator component that will display a vertical or horizontal line depending on current toolbar orientation. The following pseudocode shows how we can build such a component under the assumption that it will always have a JToolBar as a direct parent public class MyToolBarSeparator extends JComponent public void paintComponent Graphics g super.paintComponent g if getParent instanceof JToolBar if JToolBar getParent .getOrientation...
Understanding the code 53
Two instance variables have changed class type MutableHTMLDocument m_doc the main text component's Document is now an instance of our custom HTMLDocument subclass. CustomHTMLEditorKit m_kit the editor kit is now an instance of our custom HTMLEditorKit subclass. One new instance variable has been added to this example ColorMenu m_foreground used to choose the selected text foreground color. The createMenuBar method adds three new menus. An Insert menu is added with menu items Image and...
AbstractTableModel
abstract class AbstractTableModel is an abstract class that implements the TableModel interface. It provides default code for firing TableModelEvents with the fireTableRowsDeleted , fireTableCellUpdated , and fireTableChanged methods. It also manages all registered TableModelListeners in an EventListenerList see chapter 2 . The findColumn method searches for the index of a column with the given String name. This search is performed in a linear fashion this is referred to as naive in the...
The Printable interface
abstract interface java.awtprint.Printable This interface defines only one method print , which takes three parameters Graphics graphics The graphical context into which the page will be drawn. PageFormat pageFormat An object containing information about the size and orientation of the page being drawn see below . int pageIndex The zero-based index of the page to be drawn. The print method will be called to print a portion of the PrinterJob corresponding to a given pageIndex. An implementation...
Outline dragging mode
JDesktopPane supports an outline dragging mode to help with JInternalFrame dragging performance bottlenecks. To enable this mode on any JDesktopPane, we must set the JDesk-topPane. dragMode client property Instead of actually moving and painting the frame whenever it is dragged, an XOR'd rectangle is drawn in its place until the drag ends. Example 16.1 shows outline dragging mode in action.
Example 42
import j ava.awt. import j ava.awt.event. import java.io. import j avax.swing. import javax.swing.tree. import j avax.xml.parsers. import org.w3c.dom. public class XmlViewer extends JFrame public static final String APP_NAME XML Viewer protected JFileChooser m_chooser protected File m_currentFile public XmlViewer super APP_NAME setSize 4 00, 400 getContentPane .setLayout new BorderLayout JToolBar tb createToolbar getContentPane .add tb, BorderLayout.NORTH new DefaultMutableTreeNode No XML...
Running the code 27
Experiment with changing the editor's background and foreground colors using our custom menu component available in the Options menu. Notice that a color selection will not affect anything until the mouse is released, and a mouse release also triggers the collapse of all menu popups in the current path. Figure 12.6 shows ColorMenu in action. Usability and design alternatives A more traditional approach to this example would be to have an elipsis option in the Options menu that opens a color...
TableModelEvent
This event extends EventObject and is used to notify TableModelListeners registered with a TableModel about changes in that model. This class consists of four properties, which are each accessible with typical get methods int column Specifies the column affected by the change. TableModelEvent.ALL_ COLUMNS is used to indicate that more than one column is affected. int firstRow Specifies the first row affected. TableModelEvent.HEADER_ROW can be used here to indicate that the name, type, or order...
Running the code 46
Figure 18.8 shows the BeanContainer container and two editing frames displaying the properties of Clock and JButton components. This application provides a simple but powerful tool for investigating Swing and AWT components as well as custom JavaBeans. We can see all the exposed properties and modify many of them. If a component's properties change as a result of user interaction, our component properly notifies its listeners and we see an automatic editor table update. Try serializing a...
JFileChooser
This class represents the standard Swing directory navigation and file selection component which is normally used in a modal dialog. It consists of a JList and JTable and several button and input components all linked together to offer functionality similar to the file dialogs we are used to on our native platforms. The JList and JTable are used to display a list of files and subdirectories residing in the current directory being navigated. Figures 14.5 and 14.6 illustrate. Figure 14.5...
ColorChooserComponentFactory
class This is a very simple class that is responsible for creating and returning instances of the default color chooser panels and the preview pane used by JColorChooser. The three color chooser panels are instances of private classes DefaultSwatchChooserPanel, DefaultRGBChooser-Panel, and DefaultHSBChooserPanel. The preview pane is an instance of Default-PreviewPane. Other private classes used in the colorchooser package include two custom layout managers, CenterLayout and SmartGridLayout a...
JComboBox
This class represents a basic GUI component which consists of two parts A pop-up menu an implementation of javax.swing.plaf.basic.ComboPopup . By default, this is a JPopupMenu subclass that contains a JList in a JScrollPane. A button that acts as a container for an editor or renderer component, and an arrow button that is used to display the pop-up menu. The JList uses a ListSelectionModel see chapter 10 that allows SINGLE_SELECTION only. Apart from this, JComboBox directly uses only one model,...
JTable selection
JTable supports two selection models one for row selections and one for column selections. JTable also supports the selection of individual table cells. Column selections are managed by a ListSelectionModel which is maintained by a TableColumnModel implementation, and row selections are managed by a ListSelectionModel which is maintained by JTable itself both are DefaultListSelectionModels by default . As we learned in chapter 10, List-SelectionModels support three selection modes...
Example 35
import j ava.awt. import j ava.awt.event. import java.io. import java.net. import java.util. import j avax.swing. import j avax.swing.text. import j avax.swing.event. import javax.swing.border. import javax.swing.text.html. HtmlProcessor s table creation dialog-Table pane HtmlProcessor s table creation dialog-Table pane HtmlProcessor s table creation dialog-Size pane HtmlProcessor s table creation dialog-Size pane public class HtmlProcessor extends JFrame public static final String APP_NAME...
The TableColumnModelListener interface
abstract interface This interface describes an object that listens to changes in a TableColumnModel the adding, removing, and movement of columns, as well as changes in margin size and the current selection. TableColumnModel provides two methods for adding and removing these listeners addTableColumnModelListener and removeTableColumnModelListener . As is the case with TableModelListeners, TableColumnModelListeners are not directly added to JTable. Five methods are declared in this interface and...


