Eclipse Ganymede: Install Visual Studio keymapping.

This is just great. Since I am one stubborn Visual Studio developer I know almost all the shortcuts by heart. Switching to another IDE is thus very hard for me, since I don’t like to learn all the shortcuts again.

Now I am kind of forced to use Eclipse to develop for my G1 (The Google Phone) (Yes I bought one in the USA). Since Eclipse uses very weird (to me) keys it takes me way to much time to learn the shortcuts or use the menu.

BUT…

I was googling for a VS keymap for Eclipse, but this one didn’t pop up in my list. Guess what: You need to install the C/C++ development tools for Eclipse and then the keymap just pops up! And guess what, it’s almost identical!

Take a look at this link to install de CDT to your Eclipse and then go to Window > Preferences > General > Keys and change the keymap to Visual Studio.

This ofcourse doesn’t apply if you already have the CDT isntalled, then you can just set up the keymap.

Works like a charm!

Netbeans: JPanel in designer mode with code behind.

I wanted to add a JPanel to my palette so I could easily include it into a JFrame, in designer mode that is.

This was the constructor of my JPanel:

/** Creates new form SetupPanel */
public SetupPanel()
{
	super();
	this.initComponents();

	this.jTextFieldDataDirectory.setText(Main.getProperties().getProperty(Settings.DATADIRECTORY));
	this.jTextFieldOpenSSLLocation.setText(Main.getProperties().getProperty(Settings.OPENSSLLOCATION));
}

This gave me a nice error when I added it to the panel:

The component cannot be instantiated. Please make sure it is a JavaBeans component.

This is because when you add it to the palette Netbeans compiles the class, and at that point it cannot find ‘Main’, so it throws a NullPointer.

Surrounding those statements with Beans.isDesignTime() solves the problem:

/** Creates new form SetupPanel */
public SetupPanel()
{
	super();
	this.initComponents();

	if (!Beans.isDesignTime())
	{
		this.jTextFieldDataDirectory.setText(Main.getProperties().getProperty(Settings.DATADIRECTORY));
		this.jTextFieldOpenSSLLocation.setText(Main.getProperties().getProperty(Settings.OPENSSLLOCATION));
	}
}

Now you can add it to the palette without problems.

Java: casting Object to int

I found something odd when writing a Java program yesterday.

In a JTable you have an int to display (the amount ordered e.g.), but when getting the value of a cell you obviously get an Object, not an int.

In order to get the Object into an int variable, you might experience this issue:

//this does not works (obviously)
int element = this.jTableProducts.getElementAt(4, 4);

//this works! (obviously, Integer is a subclass of Object)
Integer element = (Integer)this.jTableProducts.getElementAt(4, 4);

//this odly enough does not works
int element = (int)this.jTableProducts.getElementAt(4, 4);

//but this works
int element = (Integer)this.jTableProducts.getElementAt(4, 4);

This is due the autoboxing of Integer to int (and vice versa).

Java: My FileFilter implementation

This is my javax.swing.filechooser.FileFilter implementation (for the JFileChooser):

FileFilter fileFilter = new FileFilter()
{
	@Override
	public boolean accept(File file)
	{
		return file.getAbsolutePath().endsWith(".xml") || file.isDirectory();
	}

	@Override
	public String getDescription()
	{
		return "xml settings files";
	}
};

It accepts .xml files, but allows browsing through directories