Category: Java Swing

Java Swing

Grid Bag Layout

Grid Bag Layout

GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple...

The Timer Class

The Timer Class

The Timer class provides a mechanism to generate timed events. It has properties and events, and thus can be used in application builders that understand JavaBeans. It fires an ActionEvent at a given time....

Highlighters

Highlighters

Highlighters determine how text is marked to make it stand out. The order in which we discuss the highlighter interfaces may seem counterintuitive. The basic Highlighter interface is so straightforward that you’ll rarely need...

The JTextPane Class

The JTextPane Class

JTextPane is a multiline text component that can display text with multiple fonts, colors, and even embedded images. It supports named hierarchical text styles and has other features that can help implement a word...

Formatted Text Fields

Formatted Text Fields

Swing provides extended functionality for text fields through the JFormattedTextField class introduced in SDK 1.4. A JFormattedTextField can display its value in a friendly (and locale-specific) way, enforce restrictions on its value, be used...

The JTextArea Class

The JTextArea Class

The JTextArea class displays multiple lines of text in a single font and style. Its default behavior is not to wrap lines of text, but line-wrapping can be enabled on word or character boundaries....

A Simple Swing Form

A Simple Swing Form

One of the most common user-interface constructs is the basic form. Typically, forms are made up of labels and fields, with the label describing the text to be entered in the field. Here’s a...

The JTextField Class

The JTextField Class

JTextField allows the user to enter a single line of text, scrolling the text if its size exceeds the physical size of the field. A JTextField fires an ActionEvent to any registered ActionListeners (including...

The Swing Text Components

The Swing Text Components

Swing provides an extensive collection of classes for working with text in user interfaces. In fact, because there’s so much provided for working with text, Swing’s creators placed most of it into its own...

The JTree Class

The JTree Class

Now that you’ve seen all the tree models and some of the default implementations, let’s look at the visual representation we can give them. The JTree class can build up trees out of several...

A Simple Tree

A Simple Tree

This example works by building up a series of unconnected nodes (using the DefaultMutableTreeNode class) and then connecting them. As long as we stick to the default classes provided with the tree package, we...

Editing Cells

Editing Cells

The data in the table is entirely contrived, but look at how simple it was to set up the combo box editor. In our data model we say that one column has a particular...

Selecting Table Entries

Selecting Table Entries

All this, and all we can do is render and edit data in a table. “What about selecting data?” you ask. Yes, we can do that, too. And, as you might expect, the ListSelectionModel,...

Implementing a Column Model

Implementing a Column Model

Here’s a custom column model that keeps all of its columns in alphabetical order as they are added: Implementing the model is simple. We override addColumn( ) to add the column to the superclass...