Posts in the “jfc-swing” category
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.
In Part 1 of this tutorial I shared the Java source code you need to handle drag and drop events on Mac OS X. But as I mentioned in that tutorial, the recipe to handle files that are dropped onto your Dock application icon requires a little more work, in particular an Ant build process that uses the JarBundler task. So here in Part 2 of this tutorial, I'm sharing my Ant build script that completes this recipe.
I'm always looking for ways to make my Java Swing applications on Mac OS X look more and more like native Mac applications, and when a co-worker mentioned the name Quaqua, I decided to take a look at that library/framework one more time. I had looked at it before, but this time, looking at it with a new mindset, I noticed it offers a
The running program
JSheet effect is very nice, it works as advertised. My only problem with it currently is that it does something to distort the image when the
JSheet is displayed. You can see this problem in the image shown below, where the coffee cup image that sits on top of the caution image does not look very good:
Java JLabel FAQ: How do I set the help text (i.e., help text, balloon text, tooltip text) on a JLabel?
Just call the
setToolTipText method on the
JLabel. Here's a quick JLabel tooltip display example:
// create a label with tooltip help text JLabel label = new JLabel("Username"); label.setToolTipText("Enter your username");
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.");
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.");
Summary: How to make a Java JFrame transparent (translucent) on Mac OS X.
A lot of people complain about a lot of things in regards to Java on Apple's Mac OS X platform, and okay, occasionally I'm one of them, but a very cool thing you can do on OS X is to create translucent (transparent) frames and windows with Java.
Here's a link to a new tutorial I've published titled "How to get Swing components to grow and fill using the JGoodies FormLayout".
While I'm in the neighborhood, here are a couple of quick links to go with that tutorial:
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.
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:
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() );
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:
Then use this code:
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:
One of my co-workers pointed me to this site by Jeanette Winzenburg that discusses some issues regarding the Swing framework in the land of Java, including problems with the JTable.
(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.
Java JTextField question: How do I right-align the text in a JTextField?
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.
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: