Posts in the “java” category

Java mail IMAP list example - List all the email addresses in an IMAP mailbox

Java Mail (JavaMail) IMAP FAQ: Can you show me how to get a list of all the email addresses in an IMAP mailbox using the Java Mail API (JavaMail API)?

Sure. Here's the source code for a Java mail (JavaMail) program that extracts all of the "from" fields out of a specified mailbox. This works for both POP3 and IMAP mailboxes.

Before going to the code, note the cool use of a TreeSet (java.util.TreeSet) in this example. Based on our offline discussion, you mentioned that you really want is this:

How to use the Java ImageIO class to read an image file

Wow, I haven't worked with images in Java in a while, and it turns out that opening and reading an image file in Java is very easy these days. Just use the read method of the Java ImageIO class, and you can open/read images in a variety of formats (GIF, JPG, PNG) in basically one line of Java code.

How to maximize (or minimize) a JFrame

Recently I was asked "How can I maximize a frame (JFrame) in Java?"

Like a lot of things, once you know how to do it, it turns out to be pretty easy.

How to maximize a JFrame

Here's some code that shows how to maximize a JFrame:

A Java “keytool export” tutorial

Java keytool export FAQ: Can you share some examples of the Java keytool export command and export process?

Once you've created a private key in a Java keystore file, you can export that private key to a certificate file using the Java "keytool export" command. I'll demonstrate that command in this tutorial.

The Java keytool “list” command

Java “keytool list” FAQ: Can you share some examples of the Java keytool list command, and Java keytool list process?

In a long, earlier article on Java keytool, keystore, and certificates, I demonstrated how to list the contents of a Java keystore file, but to simplify things a little for this tutorial, I'm just going to show how to query a Java keystore file using the keytool list command.

How do I convert a String to a double with Java?

Question: How do I convert a String to a double with Java?

Answer: Converting a String to a double requires that you first convert the String to a Double object, then convert the Double object to a double data type (Double is an object, while double is a primitive data type).

Here's the source code for an example Java program that performs this conversion:

A simple Java Random class example

As I was digging around through some code today, I found the following Java Random class example, and thought I'd share it here for anyone needed to see how to work with the Random class in Java. This example shows how to generate a random number in Java that is greater than or equal to 0, and less than 100:

How to convert a Java String to a float

Java String/float FAQ: How do I convert a Java String to a float?

Converting a String to a float requires that you first convert the String to a Float object, then convert the Float object to a float data type (Float is an object, while float is a primitive data type).

An example of a simple program that performs this conversion is shown below:

A custom JavaFX web browser (to show stock quotes)

I’m currently doing something completely different, and writing a little custom web browser using JavaFX and its WebView component. I’m using it so I can easily look at stock quotes and charts. I just started on it, and the current UI looks like this:

A custom JavaFX WebView web browser

How to create a LibGDX ImageButton

I just started working with LibGDX, so I don’t know if there’s a better way to create a LibGDX Scene2d ImageButton, but I can confirm that this approach works:

Texture hikeTexture = new Texture(Gdx.files.internal("hike_btn.jpg"));
Texture hikeTexturePressed = new Texture(Gdx.files.internal("hike_btn_pressed.jpg"));
hikeButton = new ImageButton(
    new TextureRegionDrawable(new TextureRegion(hikeTexture)),
    new TextureRegionDrawable(new TextureRegion(hikeTexturePressed))
);
hikeButton.setPosition(60, 300);  //hikeButton is an ImageButton
stage.addActor(hikeButton);

I currently use this code in the show() method of a class that implements Screen, and it works as desired.

TypeAhead, a continuous predictive text editor

TypeAhead is a continuous predictive text editor. It always tries to help you auto-complete the current word by using words in (a) the current document, as well as (b) a dictionary.

This short video shows a very rough prototype of how this might work:

Improvements

A few improvements are possible and easy.

A Java menubar example

Java Swing FAQ: How do I create a menubar (and menu and menuitems) in Java?

As I was working on a Java/Swing problem last week, I was scouring through the Mac OS X Java-Dev mailing list, and along the way I ran into some really nice source code I thought I could modify into a decent Java Menubar example. After a little rewriting, here is that "Java Menubar" example.