Java Tips Weblog

Archive for the ‘Tips’ Category

Global Event Dispatching

Posted by Rob Camick on September 6, 2009

As a user interacts with a GUI, by using the mouse or keyboard, Swing handles the interaction by dispatching events to the appropriate component for processing. In turn the component will notify any listeners that the event has been received and processed. There may be times when you want to intercept or alter the event before it is dispatched to the component. Swing provides a few different approaches that will allow you to control the dispatching of events.
Read the rest of this entry »

Posted in Awt, Swing, Tips | Leave a Comment »

Global Event Listeners

Posted by Rob Camick on August 30, 2009

Listeners are used to listen for specific events on a given component. This makes it easy to listen for a MouseEvent on a text component for example. With this approach you need to add a separate listener to the component for every event you want to listen for. However there may be times when you want to listen for events at a more global level. That is, you may want to listen for events:

  • of multiple types on a specific component – in this case you would need to create multiple listeners to add to the component.
  • of a single type on all components – in this case you would need to create a single listener and then recursively add it to all components.

Wouldn’t it be nice if the above requirements could be handled by a single listener?
Read the rest of this entry »

Posted in Awt, Swing, Tips | 8 Comments »

Backgrounds With Transparency

Posted by Rob Camick on May 31, 2009

Swing components allow you to change the background color of a component simply by using the setBackground() method. It is also possible to use a Color created with an “alpha” value. The alpha value defines the transparency of a Color. Unfortunately, once you start using alpha values in your background color you may encounter some undesireable painting artifacts.
Read the rest of this entry »

Posted in Classes, Swing, Tips | 2 Comments »

Combo Box No Action

Posted by Rob Camick on May 17, 2009

A common GUI design principle is that the user should be able to use the keyboard or the mouse to achieve the same functionality. Because of this principle I’ve alway been a little puzzled about the default behaviour of a JComboBox. In particular the behaviour is different when using the mouse versus the keyboard when the popup is visible.
Read the rest of this entry »

Posted in Swing, Tips | Leave a Comment »

Custom Painting Approaches

Posted by Rob Camick on May 8, 2009

The basics of custom painting are explained in the section from the Swing tutorial on Custom Painting. The main idea is that you can customize a component by overriding its paintComponent method. Typically, JComponent or JPanel will be overridden to do custom painting. A concern of many people is adding too much painting code to the paintComponent method which might result in excessive CPU usage or slow painting. Is this a valid concern and if so, then is there anything that can be done to minimize these problems?
Read the rest of this entry »

Posted in Swing, Tips | 2 Comments »

Text and New Lines

Posted by Rob Camick on February 7, 2009

Files on different platforms represent a new line with different strings. For example, if I remember correctly, the new line strings are:

  • “\r\n” – for Windows
  • “\n” – for Unix
  • “\r” – Macs

When you read a file into a Swing component the text is stored in a Document. To simplify Document processing the Swing developers decided that all new line strings would be represented by “\n” in the Document. Therefore, in Windows, the internal and external representation of a newline string is different. As long as you use the text component read() method to load the Document and the write() method to save the Document, this is not a problem. However, what happens when your program starts to query or use the text in the Document?
Read the rest of this entry »

Posted in Swing, Tips | Leave a Comment »

No Wrap Text Pane

Posted by Rob Camick on January 25, 2009

By default a JTextPane wraps text when displayed in a scroll pane. Unlike a JTextArea there is no property to turn off wrapping. I have seen many different suggestions on how to turn off wrapping in a text pane. However, all the solutions I’ve seen have a common problem in that the caret is no longer visible at the right edge of the viewport as text is being entered.
Read the rest of this entry »

Posted in Classes, Swing, Tips | 4 Comments »

Single Line Text Area

Posted by Rob Camick on January 15, 2009

We all know that a JTextField is used to display a single line of text and a JTextArea is used to display multiple lines of text. A text area also has the ability to wrap an individual line of text. Maybe you have a need for a text field that has the ability to wrap text.
Read the rest of this entry »

Posted in Swing, Tips | Leave a Comment »

Table Editing

Posted by Rob Camick on December 26, 2008

There are 3 different ways to edit a cell containing text in a JTable:

  • use F2 to invoke the editor
  • use a mouse double click to invoke the editor
  • just start typing on the cell with focus.

Read the rest of this entry »

Posted in Swing, Tips | 5 Comments »

Text Validation

Posted by Rob Camick on December 15, 2008

A common requirement is to validate text as it is being typed into a text component. Unfortunately a common solution suggested in the forums is to use a KeyListener
and to override the keyTyped() event. Using this approach the event is consumed when an invalid character is entered to prevent the character from being added to the Document. This approach can work in certain situations, however, JDK1.4 introduced a couple of new features that provide better, more complete, solutions than are available when using a KeyListener.
Read the rest of this entry »

Posted in Swing, Tips | 4 Comments »