Java Tips Weblog

  • Blog Stats

    • 2,189,668 hits
  • Categories

  • Archives

  • Advertisements

Archive for the ‘Tips’ Category

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 (see link below). 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 | 7 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 (OS 9 and earlier)
  • “\n” – Macs (OS X)

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 | 4 Comments »

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 | 21 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 | 6 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 »

Table Stop Editing

Posted by Rob Camick on December 12, 2008

When editing a cell in a JTable the table doesn’t know when a user is finished editing the cell. Therefore it is the users responsibility to tell the table when to stop editing. This is normally done by:

  • using the enter key
  • tabbing to the next cell
  • clicking on another cell with the mouse

In all the above cases the table will stop the cell editing and save the value to the table model for you. However, there is one case that is not handled automatically by the table. That is, what should happen when a user clicks on a component other than the table?
Read the rest of this entry »

Posted in Swing, Tips | 45 Comments »

Enter Key and Button

Posted by Rob Camick on October 25, 2008

A JButton that has focus can be activated by using the space bar. However, on any given Window a single button can be designated as the “Default Button” for the Window. The default button is noticeable by the darker border around the button. The default button can be activated by using the Enter key, even when it doesn’t have focus.

To assign an initial default button to a JFrame or JDialog you can use:


In Windows Look and Feel, when you tab to a button it temporarily becomes the default button. In this case, you can then activate the button either by using the space bar or the Enter key. When you tab off the button, the original default button is reset. However, in the Metal Look and Feel, the default button never changes. Therefore, even when a button has focus you can still only activate the button using the space bar.

Many users are more familiar with the Windows approach and would like the Metal LAF to work that way as well. That is, they would like to be able to activate a button that has focus with the Enter key. The solution to this problem will depend on the version of Java you are using.
Read the rest of this entry »

Posted in Classes, Swing, Tips | 6 Comments »

Text Area Scrolling

Posted by Rob Camick on October 22, 2008

A JTextArea, added to a scroll pane, is commonly used to display messages generated by an application as it is easy to add messages to the text area using the append(…) method. When using a text area in this manner there is usually also a requirement that the text area scroll as each new message is added. Sometimes the text area scrolls automatically and sometimes it doesn’t. Why is this?
Read the rest of this entry »

Posted in Swing, Tips | 15 Comments »