Saturday, February 27, 2010

Rendering ComboBox in a JTable (SWING)

Ever wondered how to make a cell in a JTable to render a JComboBox? This example will guide you on that. The scope of this tutorial does not have to be limited to combobox, the same concept applies in rendering other components in a JTable.
  1. Create a JTable with two columns as shown below.
  2. Create an inner class that implements TableCellRender as below
  3. Set the particular column's renderer as the renderer class that you have created
  4. Create a JComboBox variable with the necessary values and pass it as a parameter to the constructor of the DefaultCellEditor class
  5. WALLA....there’s your combobox, right where you want it...


Author: Thiagu

Tuesday, February 9, 2010

Changing JLabel’s background during mouse over (SWING)

Let’s look at how to change the background colour of a JLabel (by default is transparent) when the mouse cursor moves over it.

1. Create a JLable as above on your application using Netbeans IDE
2. Include the code snippet below to change the background colour of the JLabel.


3. As you can see in the code, JLabel3.setOpaque(true) line is used because by default a JLable will have a “false” value for the Opaque which makes the background transparent.
4. Once the Opaque is set to true, change the background colour.

5. Select the mouse entered action, which provides an action when the cursor gets into the boundaries of the JLabel.
6. Include the code snippet below to change the colour of the JLabel during mouse over.

Results:

Before Mouse Over


After Mouse Over

Author: Thiagu