Search code examples
javaswingnetbeansjtabletablecellrenderer

Netbeans GUI Builder - How to add custom table cell renderer?


I'm a little bit embarrassed to ask this - but what do I have to do to add a custom table cell renderer for a JTable to Netbeans GUI Builder? I'm trying to build a project from scratch but I haven't found a way to do this. I haven't found any related discussions on the internet either, so I'm really stuck right now. If I did this by hand, it would simply look like this:

myTable.setDefaultRenderer(Integer.class, new myRenderer());

Answering peeskillet's questions:

Basically, it's still about this example. I know the first two columns, let's say someone's name (String) and age (Integer). I also know that the rest of the columns will contain Boolean values.

Hence, my table model looks like this:

@Override
public Class<?> getColumnClass(int column) {
  switch (column) {
    case 0:
      return String.class;
    case 1:
      return Integer.class;
    default:
      return Boolean.class;
  }
}

It's not that this isn't working. I simply couldn't find out how to do the setDefaultRenderer part in Netbeans.


Solution

  • After you drop your JTable onto your form, right-click and select "Customize Code..." Choose "Custom Creation" in the combobox next to the this line of code:

    myTable = new javax.swing.JTable();
    

    Place the cursor just to the left of the ";" on that line of code. Enter a new ";", then press Enter, then type in this:

    myTable.setDefaultRenderer(Integer.class, new myRenderer())
    

    Click the OK button. Note that this made the original ";" move out of your way, as you can't enter your new code after it. This is a lot like your "by hand" method, but it does let you integrate your default renderer setting into the NetBeans-generated initializer code, and it works.