Posts in the “java” category

How to center a Java JDialog

Java/Swing FAQ: How do I center a JDialog?

Answer: The answer here really depends on what you mean by "center". If you really want to center a JDialog on screen, you can use code like this:

// center a jdialog on screen
JDialog d = new JDialog();
d.setSize(400, 300);
d.setLocationRelativeTo(null);
d.setVisible(true);

By calling setLocationRelativeTo with null, the JDialog will be centered.

JDialog close/closing events

JDialog FAQ: How do I capture the JDialog close event?

Solution

To get the event when a JDialog is closed, you can add a window listener using addWindowListener to your JDialog, or add a component listener, using addComponentListener.

JList tips and examples

I've been working with the Java JList a fair amount recently, so I thought I'd include a short collection JList examples here while everything is still fresh.

Free Java/Swing components

If you're in the market for free Java components for your Swing applications, it looks like there is a decent collection of free Java components to choose from.

I was going to make a list of different free Java component websites, but it looks like the Java Desktop people already have a list of Java components, so I recommend just visiting that link. (Beware that they are not all free, however.)

JList JScrollPane - a JList scrollbar/scroll example

Continuing my exploration of the Java JList, here's some sample JList/JScrollPane code, showing how I add a JList to a JScrollPane.

To get started, somewhere at the top of my class I declare my JList and JScrollPane, like this:

private JList menuList;
private JScrollPane menuScrollPane;

Later in my Java class I then set up my JList, like this:

Java/Swing UIManager default values

As I'm preparing a different Java Swing tutorial, I thought I'd share a test class I just created to look at the default entries from the Java UIManager class.

Here's the source code for my Java class that prints all the Java Entry objects from the UIManager class. As you can see, I call the getLookAndFeelDefaults method of that class, and then call the entrySet method of that class to get all the default entries:

JList "add data" - how to add/edit JList data

I don't need to do add data to the JList in my current Java/Swing application, but out of curiosity I did some research to see what you would have to do if you wanted to add data to a JList, and here are the results.

It turns out that if you need to add or edit JList data, you're probably better off creating your data as an instance of a DefaultListModel (as opposed to on object array or Vector).

Java UIManager color keys

Java color FAQ: Do you have a list of color keys I can use with the Java UIManager class?

Solution

If you're asking this question about the list of color keys in the UIManager class, you probably already know that you can write code like this to use color values from the Java UIManager class:

A Java “wait cursor” example

Java Swing FAQ: How do I create a wait cursor in a Java application?

I was going to write a nice, complicated Java “wait cursor” example today, showing you how to create an animated wait cursor in Java using threads, but after looking at a much simpler approach, I've decided to show that instead.

Java/Swing colors - Java color palettes from JFormDesigner

A few days ago I wrote about Java UIManager colors (color keys), and as a quick follow-up to that article, I thought I'd share a few "Java color palette" screen shots from the wonderful JFormDesigner application, which first got me to explore this topic.

The screen shots below come directly from JFormDesigner, and I like them because they show (a) the names of the UIManager colors keys and (b) the actual color corresponding to each color key value.

A Java splash screen with a progress bar (Part 1)

In this tutorial I look at how to create a splash screen in a Java application. At the end of the discussion I'll share all of the Java classes that are needed to implement your own Java splash screen.

Converting a Java properties file to a Map (example)

At the moment I can't remember why I wrote this method to read a Java properties file and return it as a Map of key/value pairs ... but if for some reason you happen to need a Java method that reads a properties file and returns the contents as a Map, here's some free source code for you:

JEditorPane line/word wrapping problem

I had a problem this weekend with the JEditorPane line wrap (word wrap), where it was wrapping lines on the right margin right in the middle of words. Wherever my sentence hit the right margin, that's where the line wrapped (with no consideration for word boundaries), leaving me with lines that looked like this: