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

Java syntax en Swing, tegen C++ syntax en .NET (Windows Forms)

Vandaag mijn examen Java zitten te leren, eigenlijk gewoon alle oefeningen opnieuw maken, zodat ik terug de feel van Java te pakken krijg (veel gewerkt met C++ laatste tijd), toch gek, die syntax van klasses in Java, t.o.v. die van C++.

Een voorbeeldje:

//Java
public class ProfileTest extends Test
{
	public ProfileTest()
 	{
 		super();
 	}
}
//C++
public class ProfileTest : Test
{
 	public ProfileTest() : Test()
 	{
 	}
}

Veel verschil is er niet, doch vind ik het gek, waarom ze werken met die super();, omdat die toch op de eerste regel MOET staan, voordat er iets anders wordt uitgevoerd. Dan is de syntax van C++ duidelijker, omdat er daar geen verwarring kan bestaan, op welke regel in de constructor deze moet komen.

Een ander onderdeel, waar een compleet andere gedachtengang nodig voor is, is het schrijven van een GUI, neem het simpele voorbeeld van een knop:

In Java doet men:

JButton b = new JButton();
b.addActionListener(new Listener());
public class Listener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
	}
}

In .NET (Windows Forms) doet met het zo:

Button^ b;
b->Click += gcnew System::EventHandler(this, &button_Click);
System::Void button_Click(System::Object^ sender, System::EventArgs^ e)
{
}

Weer is .NET veel meer to the point, het enige wat er moet gebeuren, is een functie schrijven, met de code wat er moet gebeuren, en dan een bepaald event aan de knop hangen, die de functie triggert, itt Java, daar moet de code worden geschreven, eventueel een class, of innerclass, die weer bepaalde klassen implementeert, en wat voor een namen: addActionListener(), actionPerformed()… in .NET is het Click. Simpeler kan toch niet?