Posts in the “jfc-swing” category

Java JTextField FAQ - How do I right-align a JTextField?

Java JTextField question: How do I right-align the text in a JTextField?

Short answer:


Long answer: I need to look at this some more to understand the difference between the alignment constants in SwingConstant and the alignment constants in JTextField.

JTable popup menu example

JTable popup menu introduction

Here's a copy of a class file I created to demonstrate how to put a popup menu (JPopupMenu) on a JTable in a Java application. Actually, the intent of this particular class was to see if I could get the popup menu to work on the header cell of a JTable, but that ended up being a no-brainer.

Java hyperlink - a JLabel hyperlink example

This Java class still needs some work, but it's my first attempt at creating a Java Swing component that simulates a hyperlink. Basically, if you use this label (a JHyperlinkLabel to be precise) instead of a JLabel you should see hyperlink behavior on your Swing labels (JLabel).

JOptionPane showMessageDialog - a Java class to show long messages

Some times when exceptions occur when users are running a Java JFC/Swing application, it's nice to be able to show them a long message about what happened, i.e., something more than a one-line message. To that end I created a really simple Error Message Panel ( that you can use with the JOptionPane to display long messages.

JGoodies FormLayout - put the FormLayout into debug mode

To put the JGoodies FormLayout into debug mode, when you're creating a builder, instead of doing this:

PanelBuilder builder = new PanelBuilder(layout);

add in a FormDebugPanel, like this:

// debug mode
PanelBuilder builder = new PanelBuilder(layout, new FormDebugPanel());

This enables what I think is a terrific debug feature, showing red lines around whatever grid structure you've created. (Sorry I don't have an image out here for this tutorial, but it really is a terrific GUI debugging feature.)


Java JTree example - select a JTree node

Recently I ran into a programming situation where I needed to select a node in a Java JTree from my code. As opposed to the usual situation of responding to a user's click on a JTree node, I needed to set the active JTree node based on something that was happening in another area of the application.

Java look and feel - How to use the "metal" look and feel

Java Swing FAQ: How do I set my Java look and feel to the "metal" look and feel?

To set your Java / Swing application to use the metal look and feel, first import the necessary classes:

import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;

Then put this in your code:

UIManager.setLookAndFeel( new MetalLookAndFeel() );

JDialog icon - how to change the icon on a Java dialog (JDialog)

A couple of us were just working on a Java application that uses a JDialog, and were wondering how to change the icon image of a JDialog (a Java dialog).

Changing the JDialog icon image doesn't seem to be too hard, though it's not quite as simple as you might think. It turns out that the JDialog inherits the icon image from it's parent, which in my case is a JFrame. So in the code sample below I'm showing two different ways to do this.

JFormDesigner - how to set the column width of a JTable

I just noticed that you can set the preferred size of a column in a JTable using JFormDesigner. If you're looking at a JTable in the normal JFormDesigner view, click the ellipsis button for the model property in the Properties list. This brings up the JTable model editor.

Next, click a cell in the column you want to modify. Then on the bottom-right of this dialog you'll see a few fields labeled "Pref. Width", "Min. Width", and "Max. Width". Just type in a value you want for the preferred width of your column.

JFormDesigner - How to create a ButtonGroup

I just learned how to create a ButtonGroup using JFormDesigner, and it's pretty easy and pretty cool.

First, add all the related buttons you want on your JFormDesigner form. Next, click all the buttons while holding down the [Control] or [Apple] key, depending on your platform, so the all the buttons are selected at one time. Then right-click one of the buttons, then select the "Group Buttons" option from the popup menu.

How to create a JComboBox in a JTable column with JFormDesigner

In a heavy day of JFormDesigner use, I just learned how to create a JComboBox in a column in a JTable, using only JFormDesigner.

First, create your JTable in JFormDesigner. I'll skip the details here, hoping that process is easy enough. Then, click the ellipsis button for the model property in the Properties list. This brings up the JTable model editor.

Java JButton ActionListener example

Here's another one of those postings that are just here to help my bad memory. I currently travel a fair amount, work on different systems and projects, and can't remember the Java syntax for adding a Java ActionListener to a JButton, so here's a little sample code to help me remember:

JOptionPane showConfirmDialog example

Here's some sample code that demonstrates how to use the Java JOptionPane showConfirmDialog method. I've included the method call in the context of a real containing method (doExitAction()) so you can see how it might be used in real-world code.

Java look and feel - how to use the native system look and feel

Question: How do I set my Java/Swing application to use the native look and feel of the platform it is running on?

Answer: Use the Java UIManager class to set the look and feel properly, like this:


Note that the setLookAndFeel method can throw an UnsupportedLookAndFeelException exception that you should handle.


I like my platform freedom

As I've started to work on designing an HTML editor I'd like for the Mac platform, a little irony has set in: I find that I don't want to write it in Objective C. Given my history with Java, I find that I don't want to be tied to one platform, even Mac OS X. What if I install Ubuntu later this week (as scheduled) and fall madly in love with it? I want my application to work there also.