Java Tips Weblog

  • Blog Stats

    • 2,530,131 hits
  • Categories

  • Archives

Archive for the ‘Tips’ Category

Swing and Java 8

Posted by Darryl Burke on April 5, 2015

Java 7 brought us JLayer (which I haven’t played with), Generics in JComboBox and JList, and a lonely collections-oriented JList#getSelectedValuesList().  Java 8, on the other hand, did not introduce any new Swing API.  However, enhanced language features can certainly make Swing code less verbose and more maintainable, and the new Date/Time API makes it easier than ever before to create interactive Swing components for date and/or time selection.

Read the rest of this entry »

Advertisement

Posted in Swing, Tips | 4 Comments »

Motion Using the Keyboard

Posted by Rob Camick on June 9, 2013

A simple form of animation is to have a component move across the screen. This is generally done by using one of the four arrow keys to control the direction of movement. There are two common approaches to listen for the pressing of an arrow key (or any key for that matter). One is to use a KeyListener and the other is to use Key Bindings. Lets take a look at these two approaches.
Read the rest of this entry »

Posted in Swing, Tips | 5 Comments »

Keeping Menus Open

Posted by Darryl Burke on September 12, 2010

The default behavior of menus on all platforms, not just in Java, is for the menu to disappear when any item is selected. But once in a way, one comes across a situation where keeping the menu open could improve the user experience. For example, in the “Quick Preferences” submenu in Opera browser, a user who wants to set more than one option is forced to navigate the menu repeatedly.

Fortunately, the flexibility afforded in terms of extending the JDK classes makes this possible in Java.

Read the rest of this entry »

Posted in Classes, Extensions, Swing, Tips | 17 Comments »

Dialog Focus

Posted by Rob Camick on March 14, 2010

When a JDialog (or JFrame for that matter) is made visible, focus is placed on the first focusable component by default. There may be times when you want to change this behaviour. The obvious solution would be to invoke the requestFocusInWindow() method on the component you wish to receive focus. The problem is that this doesn’t work all the time.
Read the rest of this entry »

Posted in Application, Swing, Tips | 42 Comments »

Formatted Text Field Tips

Posted by Rob Camick on February 21, 2010

A JFormattedTextField provides the ability for text to be formatted depending on the formatter specified. This formatting causes the formatted text field to behave differently than other text components in some cases. It also makes it a little more difficult to customize the behaviour in other situations.
Read the rest of this entry »

Posted in Classes, Swing, Tips | 5 Comments »

Table Row Rendering

Posted by Rob Camick on January 24, 2010

There are times when you might want to do some custom rendering for an entire row of a table. Based on answers I see in the forums, for most people the first thought is to create a custom renderer. This can be a simple solution in some cases, but are there other alternatives to consider as well?
Read the rest of this entry »

Posted in Swing, Tips | 20 Comments »

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 | 1 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 | 13 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 | 27 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 | 3 Comments »