Friday, June 29, 2007

HOWTO: add stripes to JList

Overview
Swing has one pretty cool thing. You can customize everything to look anyway you want. The following will show you a simple but useful trick for adding strips to a swing JList component.

Technique
The following class can be put right into a project. You will want to change the package name though. This is all you need to do the striping. Towards the bottom, you will see two setBackground(...) calls. The first is if a row is even and unselected and the other is if the row is even but is selected. Play with the colors until you find ones you like. I am not sure I like the ones below actually.
package jliststriping;

import javax.swing.*;
import java.awt.*;

public class StripeRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(
list,
value,
index,
isSelected,
cellHasFocus
);

if(index%2 == 0) {
if(! list.isSelectedIndex(index)) {
label.setBackground(new Color(230,255,230));
} else {
label.setBackground(new Color(255,255,200));
} // end-if
} // end-if

return label;
}
} // end-class
Once you have this code in your project, the next step is to plug it into your JList. In Netbeans, you simply:
  1. Select your JList in Matisse
  2. In Properties, look for cellRenderer. Click on the "..."
  3. Using "Select Mode" "Form Connector" select "User Code"
  4. type new StripeRenderer() in the text box.
  5. Click Ok and test it out.
If you don't have Netbeans, you can add jList.setCellRenderer(new StripeRenderer()); in your jList creation code block.

Final Thoughts
Adding stripes can make the component easier to read. This code is cut/paste - able into a Java file for use in any program. Give it a try.

HOWTO: remove multiple items from a JAVA Swing JList

Overview
When using the JList, there are times when you want to edit the list of items. Adding or removing a single item is pretty straight forward, however, there is a trick to removing groups of items from a JList.

For the following example, there is a simple JList that uses the DefaultListModel. The JList is setup with a MULTIPLE_INTERVAL selection model.

Technique
DefaultListModel dlm = (DefaultListModel) this.jList.getModel();

if(this.jList.getSelectedIndices().length > 0) {
int[] tmp = this.jList.getSelectedIndices();
int[] selectedIndices = this.jList.getSelectedIndices();

for (int i = tmp.length-1; i >=0; i--) {
selectedIndices = this.jList.getSelectedIndices();
dlm.removeElementAt(selectedIndices[i]);
} // end-for
} // end-if


Last Thoughts
This approach will let you grab any interval from a JList and remove them. Hopefully SUN will update this component to better handle this in the future. This will remove the need for a trick.

HOWTO: properly start a Java Swing Application from main

Overview
When starting a JAVA Swing application, is is not always clear on the proper way to fire it up. If you use Netbeans, it provides you with a nice stub in main for doing this.

Technique
To start a JAVA Swing application...

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
new JListStripingForm().setVisible(true);
}
});
}
The UIManager.put(...) line turns off the BOLD for all menus and such. I think it makes the program look bad.

The rest ensures that the application starts on the correct thread and makes the application visible.

Final Thoughts
This can be used to ensure a smooth startup of a Swing application.

HOWTO: set the title bar icon for a JAVA Swing Application

Overview
To help polish a JAVA application, it is always nice to have a custom icon that shows up in the left corner of the title bar and in the task list. To add this is simple.

Technique
Say you have a project that is using a package of jliststriping and in your jliststriping directory, you have an icon called a3.JPG. Here is how you would do it..

java.net.URL imgURL = JListStripingForm.class.getResource(
"/jliststriping/a3.JPG"
);
if (imgURL != null) {
this.setIconImage(new ImageIcon(imgURL,"Icon").getImage());
} // end-if
It is done this way so that you can load the icon from a JAR file.

Final Thoughts
This will help add a little polish to your program.

HOWTO: ensure correct JAVA Look and Feel at Program Startup

Overview
Lets face it, the default Java Look and Feel kind of sucks. It looks ok but when you have it next to a native app, it sticks out. Fortunately, the guys at SUN have made it easy to setup your JAVA program to automatically use the system look and feel.

Technique
During init of your GUI, simply add the following code. I like putting it in my constructor for the FORM.


try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);

} catch(Exception ex) {
ex.printStackTrace();
} // end-try-catch

What will happen is that the UIManager will set the look and feel using the System Look and Feel.

Last Thoughts
Using this simple snippet, you can make your JAVA program look more like a native application.