Posts in the “java” category

How to set Java FlowLayout to flow left, and control horizontal and vertical spacing

This code shows how to create a Java FlowLayout that flows left and has horizontal spacing of ten pixels and vertical spacing of five pixels:

FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 10, 5);
jPanel.setLayout(flowLayout);

Here’s what the FlowLayout constructor arguments look like:

FlowLayout(int align, int horizontalSpacing, int verticalSpacing);

FlowLayout 'align' property definitions

The align property can be any of these:

How to delete a file from a Java Jar file (use zip)

Java Jar file FAQ: Is there an easy way to delete a file from a Jar file?

Yes. Because a Jar file is just a Zip file, you can use the zip command to remove a file from a Jar file, like this:

zip -d MyApp.jar fileToDelete.foo

Of course the file to delete can be any sort of file. I just went through this process where I had to delete a configuration file, and my command looked like this:

A Java TimerTask, Timer, and scheduleAtFixedRate example

As a quick note, if you ever need to use a Java TimerTask, you can define one like this:

class BrightnessTimerTask extends TimerTask {
    @Override
    public void run() {
        // your custom code here ...
    }        
}

and you can then instantiate it, create a Timer, and schedule the task like this:

How to report Mac Java bugs

Mac Java FAQ: How do I report Mac Java bugs?

I just saw this information on the Mac Java mailing list, how to report Mac Java bugs:

A Scala cheat sheet in PDF format

I've been working with Scala quite a bit lately, and in an effort to get it all to stick in my brain, I've created a Scala cheat sheet in PDF format, which you can download below.

This PDF is very different from my earlier Scala cheat sheet in HTML format, as I tried to create something that works much better in a print format. (I first tried to get it all in one page, but short of using a one-point font, that wasn't going to happen.)

Java StringTokenizer - strings, words, and punctuation marks

I was just reading the book, Hadoop in Action, and came across a nice, simple way to use the Java StringTokenizer class to break a sentence (String) into words, taking into account many standard punctuation marks. Before looking at their solution, first take a look at the code they used to break a String into words using whitespace (a blank):

Java on OS X - A free Mac/Java application to get the mouse cursor location and color

As I go through some old applications I wrote for various purposes, but never released, I thought I'd share the source code for a Java program that you can use to get the x/y coordinates or the mouse cursor (pointer) and RGB value of the pixel at that location. I've used this application for various reasons, including determining the location of something on screen, determining the width or height between two points, or getting the RGB value of a pixel on screen.

A Java JRuby screenshot (screen capture) example

Java image FAQ: Can you show me how to create a screen capture in Java?

I don't have any Java screen capture code to share at the moment, but if you don't mind a JRuby screen capture example (which you can easily convert to Java), this JRuby screenshot (screen capture) source code example should help point you in the right direction:

Free Java and Object Oriented Programming (OOP) training material

Just a quick note that over the last few days I finished a major re-work of my old Java and Object Oriented Programming (OOP) training material. I used variations of this Java/OOP training material with several classes I taught from 1998 through about 2005.

I still need to make a few more changes to improve the material, but this big change involved reformatting the training material (courseware) into much longer pages that are easier to read, and improving the appearance of the pages.