Home | Links | About

Helpful Java Tips

I have been working a lot with Java the past few months working on my Minesweeper solver. (This is currently the older version; the new version includes new solvers) It is written in Java, so there have been a lot of cool things I have learned because of this project.

Here they are (in no particular order):

Setting the UI to look less Swingy
As much as some might like the default look of the Swing, I do not. It just does not fit with the rest of the windows on my screen. So, there is a really simple way to have the java UI match your operating system!

try
{
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
}
catch(Exception E)
{
System.out.println("Error setting look and feel");
}


Overlaying labels in Swing
I wanted to make two JLabel's stack on each other, while still being able to see both labels. It turns out there is a really easy way to do this using a JLayeredPane!
When you add the JLabel to a panel, just pass the argument JLayeredPane.DEFAULT_LAYER or JLayeredPane.PALETTE_LAYER.

JLayeredPane panel = new JLayeredPane();

JLabel below = new JLabel("I am below");
below.setLocation(0, 0);
panel.add(below, JLayeredPane.DEFAULT_LAYER);

JLabel above = new JLabel("I am above");
cellLabel.setLocation(0, 0);
keyPanel.add(cellLabel, JLayeredPane.PALETTE_LAYER);


The only downside is you have to use absolute positioning, but it works!

Making threads and stopping threads
I also had to work on managing some threads which was completely new to me.

Here is how you can make a thread.


  1. Make sure the class you want to make into a thread implements Runnable

  2. Create a public void run() method of what you want the thread to do when you run it

  3. Now start the thread;

    Thread myThread = new Thread(myClass);
    myThread.run();



And if you want to stop your thread you can call myThread.interrupt();

Although this will not really do to much, since you have to write the code to interrupt the thread yourself!

The way I decided to do this was in my run method.

public void run()
{
try
{
// Code
if(Thread.interrupted())
{
throw new InterruptedException();
}
// Code
}
catch(InterruptedException ex)
{
// We have been interrupted!
}
}


After the you call Thread.interrupted() it will remove the interrupted flag, so you have to handle it right away. You should add these statements throughout your thread and throughout the methods that your thread calls, so it can be interrupted quicker.

Fixing Balkin's Zune Case

I recently acquired a Zune 30GB (in Brown) and a Belkin Folio Kickstand Case for Zune (F8M031).

I really like the case, my only problem is that you cannot click the Play/Pause and Back buttons while it is in the case. This is a design flaw of the case, I tried out 3 cases and they all had the same problem. In fact, reading the Amazon.com Reviews of the case seam to show that everyone is having this button problem.

Belkin Folio Kickstand Case for Zune

In the design of the case, there are pieces of rubber imbedded inside the leather case which click the buttons on the Zune. The problem with the case is, these pieces of rubber are not aligned correctly inside the leather.

How the Belkin Case works, rubber pieces

The fix to this is quite easy, move the pieces of rubber.
***NOTE: Moving the pieces of rubber will VOID the case's warrantee!***


  1. Put your Zune in the case

  2. Figure out what way the Back button rubber piece needs to move in the case for both buttons to function correctly.

  3. Take your Zune out of the case

  4. With a razor blade, cut along the seam of the Back button to expose the piece of rubber

  5. With some tweezers, carefully move the rubber piece in the direction you determined from step 2. The rubber piece is glued down, though if you gently nudge the rubber piece you can get it loose

  6. Put your Zune back in the case. If the buttons still do not work correctly, go back to step 2



The Belkin Zune Case now fits my Zune perfectly!

Always ask really does mean always ask!

A little thing with Windows Vista Security Warnings is their ability to popup even if you tell them not to!

Everytime I start PuTTY by double clicking on the file,I get a Security Warning saying "The publisher could not be verified. Are you sure you want to run this software?"

PuTTY Icon
PuTTY Warning on Vista

I always click "Run" because I like using PuTTY for connecting to my school computer. The PuTTY I'm using is coming from a trusted source so I decided to uncheck "Always ask before opening this file."
Sure enough, next time I open the program again I get the same security warning.

There is a way to fix this though.

To do this, you must "Unblock" the application from it's original file location. By original file location I mean the location where you downloaded it to. If you move the file before unblocking, you will never be able to unblock the file unless you delete it and redownload the file. If the file you need to unblock came in a zip, you need to unblock the zip before extracting the contents.

To Unblock an application, right click on the file and goto Properties. In the general tab, click Unblock.
Unblock PuTTY

Now you can move the application to any folder you want, and will not get the annoying popup ever again!

IBM Support... A month long process

There comes a point in our lives where our computer breaks. It's sad, but true. Well, my ThinkPad T60p has been broken for awhile. It had a video card problem where if an application puts strain on the GPU (Like a Video Game), the screen will start to flicker and eventually cause the computer to just power off.

I sent in my computer to IBM EasyServ Repair Center in December to fix this problem, but they sent me back my computer a week later saying "We could not reproduce the problem." This was unacceptable to me at the time, but spring semester was about to start and I needed to have my computer for school.

So finally summer comes, and I decided to send in my computer again. It is June 23rd when I sent in my computer for the first time. I got my computer back on June 27th from IBM EasyServ, and I took a look at the Repair Action Report to find they replaced my Planar Card. The GPU is embedded onto the Planar Card, so I thought they had actually fixed my problem this time!

But I was wrong, so very wrong. I powered on my computer and a message came up saying "New Hardware Found." At first I thought this was just Windows Vista reinstalling the drivers. But, my heart sank when I saw what drivers it had installed. It had just installed the drivers for an "ATI Mobility Radeon X1300" - which was the wrong Graphics Card. I had an "ATI Mobility Fire GL V5250." The X1300 graphics card was on the cheaper model ThinkPad, the T60 which is a $750 cheaper laptop.

I removed the keyboard to look to see what IBM EasyServ had done to my baby, to find a screw rattling around in the case and my WWAN card was not plugged in to anything. This was because the T60 did not support a WWAN adapter.

I was outraged at this. What kind of people do they hire at IBM EasyServ?

I called up IBM Support instantly to get this corrected. The lady on the phone was one of the rudest IBM Support people I have ever gotten. She told me I had to file a "Missing Parts Request" and that it was going to be 2 days before anyone would even review my request.

Missing parts? What part was missing? They installed the wrong part in my system! And so I waited my 2 days, hearing nothing from IBM. I finally called them up after my 2 days were up and again I was helped by a rude person saying that I had to wait another day before anyone could help me.

So I finally get a call from the head-IBM-EasyServ support person saying that filing the Missing Parts Request was a mistake, and they should have just sent me another box to send my laptop back to the EasyServ depot.
So, she set me up with a new box to send my laptop in, as well as making sure the depot would have my Planar Card in stock when my laptop got there.

It is now July 2nd I am finally sending in my computer to be fixed, again. I get my computer back on June 6th and all is well with the world. Well, for awhile at least.

I start to have problems with my computer again, I get a generic Blue Screen of Death any time my computer is using a lot of RAM.
Blue screen of death
The message I get is:
***Hardware Malfunction

Call your hardware vender for support

NMI: Parity Check / Memory Parity Error

*** The system has halted ***


So, I decide to investigate the problem more. I have 2-1gig sticks of RAM in my computer, so I decided to only plug one in. My computer works fine if I just have 1 stick of RAM in at a time. But, when they are both in, I get the blue screen of death. So I tried a stick of RAM in the upper slot. Bam, within an hour of using my computer I get the same blue screen of death.

But, by now it is time to go to MEMCON, which I need my computer for so I cannot send it back in until I get back.

When I got back from MEMCON I called up IBM Support. The guy was one of the nicest IBM Support people I have ever talked to. He said there could be about 4 things that are related to that error message, and that IBM Tech Support gets a lot of calls related to that error message on the T60's. He also said that I'm the second person he had heard of that got the wrong Planar Card installed in their ThinkPad T60p.
This really makes me wonder the "quality control" that IBM ThinkPads have nowadays. I guess Lenovo really screwed up the ThinkPad brand.

Anyways, it's July 26th when I send in my computer and I get it back July 31st.

My computer works great now, I don't have any problems with it.

After sending in my computer 4 times, 3 of which were within the same month, my computer finally works.
So, next time you send in your computer to IBM EasyServ, make sure they put in the right part.

If you do have a problem with IBM EasyServ installing the wrong part, call up IBM at 1-800-952-1141 Extension 1, because these are the people that handle EasyServ problems.

Automatic Means Manual for Windows Messenger

In the past I have been hesitant to use the standalone version of Windows Messenger and instead found myself using an instant messenger that supports more AIM and MSN. But, ever since Trillian 3.0 came out, I have been disappointed in its capabilities. I had tried alternatives in the past like Gaim (Pidgin) but they were not what I was looking for.
It has been awhile since I had used the normal client for MSN, so I decided that I might as well try it out. I installed the latest version of Windows Live Messenger (I did not install the beta) and decided to give it a whirl. I think it's fine and for the amount of IM's that I send these days, it suits my needs perfectly.

My one problem with it is, it will never sign me on automatically. I do not have it configured to start up when Windows starts, but I do have it set to sign me on anytime I click on the icon from my start menu. Though for whatever reason, it continually makes me click "Sign In" for myself.
Sign me in automatically checked

I guess nothing is automatic these days. Maybe next release.