jfc-swing

recent posts related to swing and java gui development

Java image example - simple ImageIcon and JLabel technique

Here is some sample Java source code that shows a simple technique to display a GIF, JPG, or PNG image using a Java ImageIcon and JLabel.

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).

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 mouse cursor shape example

Here's a link to the Java API doc for how to set a mouse pointer/cursor shape to one of the predefined shapes. It's amazing how bad the memory gets ... here's a link to an article I wrote a long time ago about "How to turn the Java mouse cursor (pointer) into an hourglass".

 

How to add a JPopupMenu to a JTable

Here's an example of how to add a JPopupMenu to a JTable. The purpose of the popup menu is to let the user right-click on contents in the table and work directly with those contents.

In the code below I've taken a real class and trimmed it down considerably for these demo purposes. I hope the code remaining is useful enough to help you implement your own JPopupMenu on a JTable, or perhaps in other components as well.

JTabbedPane tab selection - How to detect when a user selects a tab

Summary: A JTabbedPane tab selection example.

Here's an example of some code where I'm detecting when a user selects a tab in a JTabbedPane. There are pretty much just three important pieces of code, and I've labeled those with comments and the numbers 1, 2, and 3.

JTable column width example - setting the column width

Here is a quick example of how to set JTable column widths. I don't know if this is a perfect solution, but it is one possible solution:

Java hyperlinks and the HyperLinkListener class

Using hypertext and hyperlinks in Java Swing applications makes them look more like web applications, and that's often a very good thing. Here's a quick example of how to add a Java HyperlinkListener to a component in Java. In this case the component I'm going to use is a JEditorPane.

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:

myTextField.setHorizontalAlignment(SwingConstants.TRAILING);

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.

Initial JDialog - JTable size

(From a recent email.) Help, I have a 13-column JTable, inside of a jscroll pane, inside of a borderlayout, inside a JDialog. Ideally, I would like the dialog to initially open up to the correct width needed to display the 13 columns without horizontal scrolling.

Setting the preferred width of each column (only) does not work very well. Setting the minWidth gets the columns to open up to the correct width, but then the last 7 or so columns fall off the right hand side of the dialog.

Syndicate content