1

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.

Community
  • 1
  • 1
not_a_number
  • 305
  • 1
  • 6
  • 18
  • Do you only want to render a specific column? How is this column determined? Is it determined during runtime? What is supposed to be rendered? Please edit your post, answering these questions, along with a [MCVE](http://stackoverflow.com/help/mcve) -hand coded preferred. – Paul Samsotha Jun 01 '14 at 16:46
  • What's wrong with doing it by hand? I don't think there is a way from the design view to set the renderer. Also you want to pass `Object.class` to set renderer method. Also what's special about this renderer, that you need a custom one? I guess still really don't understand the problem you're facing. The related question you link doesn't seem to have anything to do with your post – Paul Samsotha Jun 01 '14 at 17:22
  • Okay, let's break it down to the boolean values. The default renderer for boolean values would give me checkboxes, which is not what I want. Also, I don't know how many columns with boolean values I'll end up with (as that depends on the selected variables to display). Let's say "likes music", "likes sports" etc. Hacking the code created by the Netbeans GUI Builder is strongly disadvised. I don't know how to put it clearer. – not_a_number Jun 01 '14 at 18:27
  • 1
    After you drop your JTable onto your form, right-click and select "Customize Code..." Choose "Custom Creation" in the combobox next to the line of code that says, "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, "myTable.setDefaultRenderer(Integer.class, new myRenderer())" and 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 (so I'm not offering it as an answer), but it works. – Stevens Miller Jun 13 '14 at 23:38
  • Thanks a lot for posting this approach. As I found out in the meantime, it's also possible to create a post-setModel entry using the same dialog. That's probably less dirty. I don't know if there any disadvantages in setting the renderers after the model, though. – not_a_number Jun 14 '14 at 15:08
  • I'm no expert, but I believe a substantial part of the benefits we get as programmers by using models that are isolated from renderers is that they are largely independent of each other. That is, you can set the renderer after the model, and do so as many times as you like. At run-time, you might want to change renderers depending upon how the user would like to see the data in the model. If anything, setting the renderer before the model is what I would expect to be problematic, as the renderer needs a model, but a model doesn't need a renderer. – Stevens Miller Jun 14 '14 at 16:00
  • That said, I think the post-setModel is the better way of doing it. In any case, we need to access the Customize Code dialog. – not_a_number Jun 14 '14 at 16:39
  • Glad you found it useful. As there are no other answers, I'll go ahead an resubmit my comment as an answer now. – Stevens Miller Jun 14 '14 at 17:58

1 Answers1

3

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.

Stevens Miller
  • 1,387
  • 8
  • 25