Posts in the “jfc-swing” category

Java - get the number of mouse buttons

Java mouse buttons FAQ: How do I determine the number of mouse buttons in a Java application?

I'm working on a Java Swing application, and I'd like to be able to determine the number of mouse buttons on the current system (Mac, Linux, Windows).

Mac Java keystroke - How to handle the Apple (command) key with Java on Mac OS X

Java Mac keystroke FAQ: How do I write Java KeyStroke code for Mac OS X systems?

When I switched from "Java programming on Windows" (or Linux) to "Java programming on a Mac", I quickly learned that I was going to have to change the way I bound my keystrokes for handling key-driven events (things like keystroke-driven popup menus, mnemonics, or accelerator keys). On Windows systems I used to write key-binding code like this:

A JButton tooltip example

Java/Swing FAQ: How do I set the help text (i.e., help text, balloon text, tooltip text) on a JButton?

Just call the setToolTipText method on the JButton. Here's a quick JButton tooltip display example:

// create a button with tooltip help text
JButton button = new JButton("Click Me");
button.setToolTipText("Click this button to make something happen.");

Mac Java - Image drag and drop

Java Swing drag and drop FAQ: How do I get Java/Swing image drag and drop working on Mac OS X?

I've been working on several new Swing applications for Mac OS X recently, and most of these applications include features like image processing, copy and paste clipboard interaction, and in today's example, dealing with drag and drop events on the Mac.

JTextField tooltip display - how to set a balloon tooltip on a JTextField

Java/Swing FAQ: How do I set the help text (i.e., help text, balloon text, tooltip text) on a JTextField?

Solution: Just call the setToolTipText method on the JTextField. Here's a quick JTextField tooltip display example:

// create a textfield with tooltip help text
JTextField textfield = new JTextField(10);
textfield.setToolTipText("Enter your username over here, that other thing is a label.");

JButton example - how to create a JButton rollover effect

Problem

You want to implement a nice mouse rollover effect on the buttons (JButton instances) in your Java Swing application. This Java button rollover effect makes your application feel more "alive" and interactive.

You can see what this button rollover effect looks like in the following two images. First, here's what a Java button (JButton) looks like normally:

A list of great Java Swing resources

I've been updating a Java Swing application that I wrote (and that I use to write these blog entries), and in doing so, I've been reading a great Swing book titled Filthy Rich Clients to mine for a few good ideas. This is a terrific book for Swing/GUI developers , and it's full of links to great Swing resources on the internet.

JButton focus - How to put initial input focus on a JButton

Question: In a Java Swing application I open a JFrame that displays some contents in a JTextArea, and has a Close button (a JButton) at the bottom of the JFrame. I've tried a lot of things, but I can't get initial focus on that JButton component. This is happening on a Mac OS X system, but I'll assume it has the same problem on Windows. Any suggestions?

Jar manifest example - how to add a manifest file to a jar file

Summary: How to add a manifest file to a Java jar file.

I just created an executable jar file for a Java client-side application. I hadn't done this in a while, and I always forget about the option to include a manifest file in the jar file. If you're good and remember to include a manifest file, like this:

jar cfm curtain.jar Manifest.txt com/

then users can run your client application very simply, like this:

Java Swing look and feel - How to use the default look and feel

Java Swing FAQ: How do I set my Java/Swing (GUI) application to use the default look and feel of the current system/platform?

In a Java / Swing application, to use the default look and feel of the current operating system (platform), first import the necessary class:

import javax.swing.UIManager;

Then use this code:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Java caret position - Getting the caret position in a text document

Java caret position FAQ: How do I get the Java caret position in a JTextComponent, such as a JTextArea or JEditorPane?

Wow, this was a bear to find anything about. Everyone always wants to show you how to use a JPopupMenu with a mouse click, such as a right-mouse click, but nobody ever shows you how to display a JPopupMenu when someone uses a keystroke, or keyboard accelerator.

So, using a little Java mojo, here is how I get the caret position in a JTextComponent (JTextArea, etc.) to display a JPopupMenu near the current caret position:

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.