imageicon

JOptionPane showMessageDialog examples (part 1)

JOptionPane showMessageDialog examples: I've been working with the Java JOptionPane showMessageDialog a lot lately, so I thought I'd create a page here with a number of JOptionPane showMessageDialog examples, sort of a JOptionPane reference page.

I'll walk you through some JOptionPaneexamples here, starting simple, and increasing the level of difficulty as we go on.

JList image - rendering images in JList cells

Here's another JList example, this time sharing some JList code I use to render images and text in JList cells.

Before going on, if you just want to display a set of images in a JList, all you have to do is create your images as an array Icons, and then add this array to your JList, and you're done. The JList is smart enough to render a Java Icon as an image, so in the most simple case, that's all you have to do.

However, in my case, because I want to render an image and text in each cell, I need to do a little more work. If you want to do something like this, read on.

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:

JOptionPane showMessageDialog examples (part 2)

With custom icon

It's important to note that you can also customize the icon that is displayed when using a JOptionPane showMessageDialog. In the following example, I specify that I want to display my own PNG image by creating an ImageIcon as an additional argument to the showMessageDialog method:

Java screenshots - How to take a screen capture with Java and Swing

Just digging around through an old Java/Swing application again, and I found this method that takes a snapshot of the current screen, and returns that screen shot as an BufferedImage.

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.

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.

Syndicate content