Java Tips Weblog

  • Blog Stats

    • 2,613,981 hits
  • Categories

  • Archives

Default Table Header Cell Renderer

Posted by Darryl Burke on February 27, 2009

The javax.swing package and its subpackages provide a fairly comprehensive set of default renderer implementations, suitable for customization via inheritance. A notable omission is the lack of a default renderer for a JTableHeader in the public API. The renderer used by default is a Sun proprietary class, sun.swing.table.DefaultTableCellHeaderRenderer, which cannot be extended.

DefaultTableHeaderCellRenderer seeks to fill this void, by providing a rendering designed to be identical with that of the proprietary class, with one difference: the vertical alignment of the header text has been set to BOTTOM, to provide a better match between DefaultTableHeaderCellRenderer and other custom renderers.

Read the rest of this entry »

Posted in Extensions, Swing, Table | 16 Comments »

Vertical Label UI

Posted by Darryl Burke on February 21, 2009

Once in a way, one comes across a situation where the ability to render text vertically – rotated 90° – could both conserve screen real estate as well as make for a more aesthetic GUI.

VerticalLabelUI, which extends BasicLabelUI, provides a comprehensive solution to such need.

Read the rest of this entry »

Posted in Extensions, Swing | 13 Comments »

Combo Box With Separators

Posted by Rob Camick on February 15, 2009

A combo box displays a drop down list of items. Usually all the items in the list are related to one another. Occassionally, you may have a need to show a list of unrelated items. In this case it would be nice to separate the items in the list visually. However, unlike a popup menu, a combo box popup does not support the use of separators.
Read the rest of this entry »

Posted in Extensions, Swing | 3 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 | 5 Comments »

Menu Scroller

Posted by Darryl Burke on February 1, 2009

New: keepVisible(JMenuItem) and keepVisible(int) methods scroll the menu to the specified item or index.

A drawback of the JMenu API is that it does not provide a method to limit the number of items displayed at a time, something on the lines of the JComboBox method setMaximumRowCount. This means that adding a large number of items to a menu can and does result in a menu too tall for the screen. The usual workaround is to nest the items in submenus, which can make it tedious for the user to find and select the desired item, and lead to quite some extra code for the programmer, especially if the menu items are added dynamically at run time.

MenuScroller addresses this shortcoming by deterministically adding up and down arrows to the top and bottom of the menu. A number of items can optionally be frozen at the top and/or bottom of a scrolling subset of menu items.

Read the rest of this entry »

Posted in Classes, Swing | 83 Comments »

Single Root File Chooser

Posted by Rob Camick on January 28, 2009

The JFileChooser component allows a user to select a file from anywhere in the File System. The “Look In” combo box allows you to move around the File System easily to find the file you are looking for. However, there may be times when you want to restrict the user to searching a specific directory tree. Therefore, we need to control the entries displayed in the “Look In” combo box.
Read the rest of this entry »

Posted in Extensions, Swing | 18 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 Column Reordering

Posted by Rob Camick on January 9, 2009

By default table columns are created and displayed in the order in which the column data is created in the TableModel. This will satisfy your requirements the majority of the time as in most case you are in control of how the data in the TableModel is created. However, there may be some situations when you wish to change the default ordering of the table columns.
Read the rest of this entry »

Posted in Classes, Swing | Leave a Comment »

Center Line in Scroll Pane

Posted by Rob Camick on January 4, 2009

Generally, a multi-line text component is added to a scroll pane so that all the lines contained in the text component can be viewed by scrolling through the scroll pane. The caret indicates the current line. When the setCaretPosition() method of the text component is invoked the scroll pane will scroll to make sure the current line is always visible. Therefore, one of three possible scrolling outcomes will occur:

  • no scrolling happens when the current line is within the current viewport of the scroll pane
  • scroll such that the current line is displayed at the top of the scroll pane
  • scroll such that the current line is displayed at the bottom of the scroll pane

There may be times when you wish the current line to be positioned in the center of the scroll pane.
Read the rest of this entry »

Posted in Extensions, Swing | Leave a Comment »

 
Design a site like this with WordPress.com
Get started