A Java program to open, read, and display an image file
In an earlier blog post I shared a small section of Java code that showed how to place an image on a JLabel.
In an earlier blog post I shared a small section of Java code that showed how to place an image on a JLabel.
In an earlier tutorial, I shared some code on how to maximize a JFrame, but every once in a while you'll run into a situation where you want to fill an entire screen with a JFrame, but not call a method to actually maximize it. This is rare, but I just ran into this situation, so I thought I'd share the source code here.
I'm often asked, "How do I center a JFrame on screen?", or, "Is there a method to center a JFrame?"
I always center my initial JFrame on screen using the setLocationRelativeTo
method, and pass it a null
reference. As the Javadoc for setLocationRelativeTo
method states:
I was just digging around through one of my Java Swing applications, and I found this method that writes a given String to the system clipboard, using the Toolkit, Clipboard, and
Java Swing FAQ: How do I increase the JScrollPane scrolling speed when using the mouse wheel?
Once you put a component (JTextArea, JEditorPane, JTable) into a JScrollPane in a Java Swing application, you'll quickly see that the default scrolling speed when trying to vertically scroll the viewport with the mousewheel is very slow. To make your scroll pane faster, i.e., more like a native Mac or Windows application, you'll want to increase this default mousewheel vertical scrolling speed.
Java screen size FAQ - "How do I determine the size of the screen (or display) in pixels? (i.e., the screen size, or screen resolution)
The following Java code demonstrates how to get the screen size, using the Java Toolkit class:
Java Swing FAQ: How can I tell if my Java/Swing code is running on the Event Dispatch Thread (EDT)?
I was just digging around through an old Java/Swing application and found this method that takes a snapshot of the current screen, and returns that screen shot as an BufferedImage, so I thought I would share it here.
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.
Using hypertext and hyperlinks in Java Swing applications makes them look more like web applications, and that's often a very good thing. Here's a quick example of how to add a Java HyperlinkListener to a component in Java. In this case the component I'm going to use is a JEditorPane.
Java Swing FAQ: How do I create a Java tooltip (also known as balloon or bubble help text)?
Lately I've been doing a lot of Java programming on Apple's Mac OS X platform, and my most recent effort has been to handle drag and drop events in my Java Swing application. Not satisfied to handle "simple" drag and drop events, I decided I wanted to take my application even farther, and let users drag files and images to my Java application icon in the Mac Dock.
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");
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.
Earlier today I wrote a blog entry about how to create a transparent/translucent JFrame on a Mac OS X system, and then I thought, "Wouldn't it be cool if you could dynamically control the transparency level of the frame?" I did a little bit of research to make sure this can be done, and sure enough, it can.