jfc-swing

recent posts related to swing and java gui development

Mac Cocoa-Java API is deprecated

Wow, I missed this news from last year: the Mac OS X Cocoa-Java API is deprecated.

I know that when I was reading Cocoa Programming for Mac OS X earlier this year the author (Hillegass) said not to use it, but I didn't realize it was a total no-no. As the first link says, use JNI instead.

 

The Tame Swing examples

Digging through my Mac laptop, I just found the old Tame Swing files laying around. These examples are a little old now, but I think they are great examples of the power of the Java Swing framework. Since they are hard to find on the internet I've gathered them into a little collection here.

Currently I just have the Java source code files there, but give me a couple of days and I'll see what I can do to get some of the examples running, and maybe include some screen shots there as well.

 

Mac Java - Handling Quit, About, and Preferences menu/menubar actions

I'm just starting to get into making my Java/Swing application look and feel a lot more like a native Mac application. I've created two earlier posts on this topic already, including Making your Swing application look like a Mac application, and Putting your JMenuBar on the Mac menu bar.

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.

Mac Java - Make your Java Swing app look like a native Mac app

Just looking through some old code for a Java/Swing editor I started writing years ago, I saw this line of code that I added specifically for the Mac OS X platform:

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:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

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

 

Java JMenuBar Mac example - put the JMenuBar on the Mac OS X menubar

Mac Java menubar FAQ: I'm creating a "Mac Java" application for (a Java Swing application for Mac OS X) ... how do I put my Java menu bar (JMenuBar) on the Mac menubar?

Answer: In your Java Mac application, set the system property apple.laf.useScreenMenuBar to true, like this:

Java animated GIF example

Someone asked yesterday if you can use animated GIF images in Java applications using the JFC/Swing toolkit. That's something I hadn't tried with Java and Swing before, so I wrote a quick test program, and sure enough they work.

I just load an animated GIF as an ImageIcon, then put it on a JLabel and display it on a JFrame, and the animation starts right up. Note that I'm using a Java/JDK 1.4.x release.

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 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:

Syndicate content