Java Tips Weblog

  • Blog Stats

    • 2,613,989 hits
  • Categories

  • Archives

Tabbed Pane Focus

Posted by Rob Camick on October 30, 2008

It seems to me that the focus policy of JTabbedPane has changed from JDK1.4.2 to the current JDK6 version.

The current version remembers which component last had focus on a tab and will restore focus to that component again when the tab is reselected. In earlier versions the component with focus was not remembered and focus was placed on the first component on the tab each time a tab was reselected.

Both focus policies are valid. It would be nice to have control over which focus policy you want to use independent of the JDK version you are using.
Read the rest of this entry »

Posted in Classes, Swing | 2 Comments »

Line Painter

Posted by Rob Camick on October 29, 2008

You have a text component that dislays multiple lines of text. Now you want to highlight the current line at the position of the caret. One possible way to do this might be to use a highlighter (as discussed in the last blog) and then adjust the highlight offsets whenever the caret changes location. The problem is that the default highlight painter will only highlight where text is painted. That is, the empty space to the right of the text is never highlighted. We want to highlight the entire line whether text goes to the end or not.
Read the rest of this entry »

Posted in Classes, Swing | 13 Comments »

Rectangle Painter

Posted by Rob Camick on October 28, 2008

All text components allow you to highlight certain portions of text. This is done by adding a highlight to the Highlighter. The highlight will then use a HighlightPainter to paint the highlight. The default painter  simply paints a coloured background. Effective but rather plain. Now is your chance to create a custom painter and jazz up the highlighting.
Read the rest of this entry »

Posted in Classes, Swing | 2 Comments »

Compound Undo Manager

Posted by Rob Camick on October 27, 2008

Swing text components support undo/redo of text entered in a Document. Every time a change is made to the Document an UndoableEditEvent is generated. The UndoManager keeps track of these events and allows you to invoke undo/redo on them. The default implemetation of the UndoManager is to undo/redo individual events. This means each character you type is undone/redone separately. I don’t know about you, but I would rather undo/redo releated events together at the same time.

Read the rest of this entry »

Posted in Classes, Swing | 20 Comments »

Text Utilities

Posted by Rob Camick on October 26, 2008

The JDK already supports a class dedicated to Text Utilities. Although you may have missed it because the class is called Utilities. The class is full of static methods to help you work with text components. Of course a few more methods would never hurt.
Read the rest of this entry »

Posted in Classes, Swing | 7 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:

getRootPane().setDefaultButton(...);

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 »

Application Inactivity

Posted by Rob Camick on October 24, 2008

There are times when you may want to monitor your application for inactivity. Maybe you want to log out a user after a certain time interval. This requirement can be broken down into two main steps. We will need to:

  • listen for events at the application level
  • track the time interval between these events

Whenever the time interval between events exceeds our inactivity threshhold we will then invoke the inactivity Action.

Read the rest of this entry »

Posted in Classes, Swing | 15 Comments »

Bean Comparator

Posted by Rob Camick on October 23, 2008

Java provides basic support for sorting Beans stored in Lists and Arrays by using the Collections.sort(…) and Arrays.sort(…) methods respectively. If you want your Bean to be sortable, then your Bean must implement the Comparable interface. This will provide your Bean with a “Natural Order” sort. What do you do when you want to sort your Bean on a different property? Well, in this case you will need to create a custom Comparator to be used by the sort methods. But, do we really need to create custom Comparators for each sortable property, or is there an easier way?
Read the rest of this entry »

Posted in Classes, Util | 9 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 »

Table Select All Renderer

Posted by Rob Camick on October 21, 2008

My previous entry was about using a Select All Editor in a JTable. When you double click a cell or use the F2 key to start editing a cell the editor will be displayed with the text in the cell already highlighted ready to be replaced. This is fine because the user has visual feedback that the text will be replaced if they start typing without removing the selection.

However, when you type a character when focus is still on the rendered cell the editor is invoked with the text already replaced by the character just typed. The user had no visual indication that this was going to happen.
Read the rest of this entry »

Posted in Classes, Swing | 2 Comments »

 
Design a site like this with WordPress.com
Get started