Java Tips Weblog

  • Blog Stats

    • 2,571,705 hits
  • Categories

  • Archives

Archive for 2008

Recent Updates

Posted by Darryl Burke on December 29, 2008

Thanks for the various comments. We’ll try to keep you updated here when changes are made to the code so you can get a new copy.

June 21, 2016 – Single Root File Chooser

  • The “Up” button was not being disabled correctly when a relative File was used as a parameter for the class.

If you are interested in receiving an RSS feed whenever this posting is updated click on the “Read the rest of this entry” link below for more information on how to do this.

Read the rest of this entry »

Posted in Uncategorized | 42 Comments »

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 »

Protected Text Component

Posted by Rob Camick on December 21, 2008

Text components allow you to freely change any of the text displayed in the component. There may be times when you wish to prevent the user from changing specific pieces of text. This is a relatively complex task as you will need to prevent the Document from being updated. As well it would probably be a good idea to highlight the protected text so the user has some visual feedback.
Read the rest of this entry »

Posted in Classes, Swing | 1 Comment »

Icon Table Cell Renderer

Posted by Darryl Burke on December 18, 2008

The suplied default renderer for JTable columns of class Icon and ImageIcon, JTable.IconRenderer, is known to give rise to a ClassCastException when attempting to render an Icon whose implementation of paintIcon(…) requires a cast of its Component parameter to a specific subclass of Component. Several icons obtained from standard JDK classes trigger this ClassCastException.

IconTableCellRenderer addresses this issue by attempting to paint the Icon in 3 different ways before falling back on a default icon which can be customized via a parameter to the constructor.

Read the rest of this entry »

Posted in Extensions, Swing, Table | 7 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 | 46 Comments »

File Class Loader

Posted by Darryl Burke on December 9, 2008

Loading a Java class identified at runtime can be a daunting task. When faced with the challenge, one naturally turns first to the standard ClassLoader and its subclasses. However, many methods that look interesting are protected and/or final, and cannot be used directly.

FileClassLoader extends java.net.URLClassLoader to provide useful functionality for loading a class from its compiled class file.

Read the rest of this entry »

Posted in Extensions, Net | 2 Comments »

Action Map Action

Posted by Rob Camick on December 6, 2008

The ActionMap contains predefined Actions for a given Swing component. Many, but not all, of these Actions are mapped to a KeyStroke so they can be invoked by the user. Regardless of whether an Action is mapped to a KeyStroke or not, there may be times when you want to use a button or a menu item to invoke one of these existing actions. These components support the setAction() method, so all we need to do is retrieve the Action from the ActionMap.
Read the rest of this entry »

Posted in Classes, Swing | 2 Comments »

Popup Menu Alignment

Posted by Rob Camick on December 3, 2008

A JPopupMenu displays JMenuItems with the text left aligned. In fact the popup menu goes to a lot of trouble to make sure the text of each menu item is properly aligned. For example, if you try using the setIconTextGap() method on a single menu item, that gap is used for all menu items. This makes it difficult to change the text alignment to be centered or right aligned if you have a reason to do so.
Read the rest of this entry »

Posted in Classes, Swing | Leave a Comment »

Visual Font Designer

Posted by Darryl Burke on November 30, 2008

New in version 2: VisualFontDesigner can now be used as a Font chooser, much on the lines of JColorChooser

When trying to create an attractive Font, the first challenge is usually to research the difference that applicable attributes make to its appearance. Applying and reviewing numerous combinations of attributes can be a tedious exercise.

VisualFontDesigner makes it a breeze to quickly visualize the effects of most of the available attributes, with the added bonus of generating a code block that can be directly copied to recreate the font elsewhere.

Read the rest of this entry »

Posted in Application, Awt | 4 Comments »