Java Tips Weblog

  • Blog Stats

    • 2,613,990 hits
  • Categories

  • Archives

Table Select All Editor

Posted by Rob Camick on October 20, 2008

Sometimes when editing text fields it is desireable to have the text selected so that when you start typing the original text is replaced with the new text. This is easily done by adding a FocusListener to the text component and invoking the selectAll() method. Since a JTable uses a JTextField as an editor we should be able to add this text selecting feature to a table. However, it is a little more complicated then simply using a FocusListener.
Read the rest of this entry »

Posted in Extensions, Swing | 26 Comments »

List Editor

Posted by Rob Camick on October 19, 2008

Wouldn’t it be nice to be able to edit the values in a JList? Well a list was designed to be display only so it would involve a major rewrite of the component to support this requirement. For this reason, the most common suggestion found in the forums is to create a single column JTable and use it as a list. This is a very powerfull solution as you inherit all the default rendering and editing capabilities of a table. But what if you just want simple editing of the values, is there another way?
Read the rest of this entry »

Posted in Classes, Swing | 6 Comments »

Combo Box Prompt

Posted by Rob Camick on October 18, 2008

You have a JComboBox on a crowded form and there is no room to display a label describing the usage of the combo box. Or, maybe you just don’t want to use a label. In either case, you need to display a prompt message in the combo box indicating its usage. One way to do this is to simply use the first item of the combo box as the prompt message and then make this item the selected item. But what if you don’t want the prompt message to be part of the combo box drop down?
Read the rest of this entry »

Posted in Classes, Swing | 7 Comments »

Group Comparator

Posted by Rob Camick on October 17, 2008

Many times a sort may need to be performed on multiple fields to get the desired result. For example, in a phone book “First Name” is sorted within “Last Name”. Using the existing Java sort utilities, Collections.sort(…) and Arrays.sort(…), this would be done by first doing a sort on the First Name and then a sort on the Last Name. Obviously it is not very efficient to perform a double sort. A better approach would be to sort on both fields in a single pass.
Read the rest of this entry »

Posted in Classes, Util | 2 Comments »

Column Comparator

Posted by Rob Camick on October 16, 2008

Java provides basic support for sorting data in Lists and Arrays by using the Collections.sort(…) and Arrays.sort(…) methods respectively. This works great when you have a single column of data in the List, an Array of Strings or a List of Integers, for example. But what happens when the data to be sorted is a List or an Array itself? In this case you would want to sort on a specific column and the data type to be sorted may be unknown. Java does not support this so what can you do?
Read the rest of this entry »

Posted in Classes, Util | 7 Comments »

Limit Lines in Document

Posted by Rob Camick on October 15, 2008

You may want to use a JTextArea or JTextPane as a simple console type component. That is, messages from your application will be added to the console as they are generated. Now you will probably need the ability to limit the total number of messages in the console so that you don’t run out of memory. How can we do this?
Read the rest of this entry »

Posted in Classes, Swing | 7 Comments »

List Action

Posted by Rob Camick on October 14, 2008

Many Swing components support the concept of invoking an Action when a certain event occurs. For example, clicking on a button will invoke its ActionListener, or using the Enter key on a text field will do the same. However, there is no standard support for invoking an Action on a JList. I find this surprising, as most applications I’ve worked with support invoking an Action when either the Enter key is pressed or when the mouse is double clicked.
Read the rest of this entry »

Posted in Classes, Swing | 3 Comments »

Screen Image

Posted by Rob Camick on October 13, 2008

Do you need the ability to create an image of your GUI or a component on your GUI? The easiest way to do this would be to use the Robot class and its createScreenCapture(…) method. This will work whether you have an AWT or Swing application. However, this method is relatively slow as it gets the pixels directly from the screen. There must be a better way.
Read the rest of this entry »

Posted in Classes, Swing | 17 Comments »

Background Panel

Posted by Rob Camick on October 12, 2008

Many people want to know how to add a backgound image to a frame. The easiest way to do this is to add an image to a JLabel and then add the label to the frame. Is there a better way? Well, the answer depends on your requirements.

Read the rest of this entry »

Posted in Classes, Swing | 61 Comments »

Table Format Renderers

Posted by Rob Camick on October 11, 2008

JTable uses renderers to display data in each cell of the table. This allows for data to easily be displayed in different formats in any given cell.  However, the table only provides support for a few basic renderers:

  • Boolean – rendered with a check box.
  • Number – rendered by a right-aligned label .
  • Date – rendered by a label.
  • Icon – rendered by a centered label.
  • Object – rendered by a label that displays the object’s string value.

Not much to choose from when you want to jazz up the display of your data. Fortunately we can easily create a renderer which will allow us to format the data in various ways.

Read the rest of this entry »

Posted in Classes, Swing | 32 Comments »

 
Design a site like this with WordPress.com
Get started