• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JTable, custom renderers

Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a JTable, but I want to use custom renderers. Actually, I would like to reuse the default renderers, with only one property changed - when the cell is not editable, I want it to be disabled.
The scenario is this - I have a row that contains a few cells, first of which is a combo with a set of values (enum). I am using a custom editor that uses a combo box. When a certain value is chosen, some of the cells in the row should be made "irrelevant", and I want to convey this to the user making the cells uneditable and have them rendered as "disabled". Some of the columns are boolean, whcih the default rederers show as check boxes.
What I did is that I add a TableModelListener to the model, and detect the value set in the column with enums, and if the certain value is set, I add the row to a set of "uneditable" rows (actually, now the whole rows are uneditable, just a few columns), and call fireRowsUpdated for the row. This forces the JTable to redraw the row, asking JTable isCellEditable. In this method, I check if the row is found in the uneditable set, and if it is, I check if the column number asked is to be uneditable. All of this workes fine.
However, I have a question to the renderers. JTable contains static inner classes like BooleanRenderer or NumberRenderer. I would like to reuse these, as the only thing I am changing there is that that in the getTableCellRendererComponent I check with the JTable passed as an argument if a given cell is editable, and if not, I set setEditable(true) or (false) accordingly. Unfortunately, the classes are package private, so I cannot extend them. What I did is pretty much copied the code (border settings, opacity settings and so on) to my own classes, but this doesn't sound like good code reuse, does it?
Is it possible to somehow reuse the default renderers, is there any way I can get hang of them, maybe use composition, and just add my tiny little bit of code? Or is the fact that the classes are package private the end of my dreams, and copying the code to my classes the only thing I can do?

Best refards,
Posts: 3205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be able to override the preparedRenderer(...) method of JTable to simply set the renderer component to disabled based on the rows in your uneditable Set.
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just what I am looking for, this is great. Thank you very much sir!
Beware the other head of science - it bites! Nibble on this message:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic