• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Globally replacing BooleanTableCellRenderer

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have written a replacement for the default BooleanTableCellRenderer.

I know how to apply it to a single table:



But I would like to apply it to ALL JTables in my application.

Is there a simple way to do this? My guess is that I need to create a custom LookAndFeel. All the documentation I've seen on that looks very complicated. Isn't there some simple way to change just one part of the look and feel?


FYI:
I don't like the default renderer because it gives no visual clue where the keyboard focus is or whether the cell is editable. Such visual feedback is very useful when moving through the table with keyboard controls rather than the mouse. I made my renderer display "focus" and "editable" properties in the same way that the renderer for Strings does.


Here is the class, in case you'd like to use it too.

 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would avoid the plaf code option.
Possibilities:
1 — make a method in your app that will do this for each table

2 — make and use a special JTable class that overrides the method
 
Ed Ewing
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Craig Wood:
I would avoid the plaf code option.
Possibilities:
1 — make a method in your app that will do this for each table
2 — make and use a special JTable class that overrides the method



Thanks for the suggestions.

I don't want to do it that way because I want to encourage a consistent JTable look and feel throught the application without needing to edit every place in the code that creates a JTable. The application has a plug-in architecture (partially), so I can't control the code written by others.

Anyway, I found a fairly simple answer to my own question, and I'm going to try this for a while.

Somewhere in the start-up of the application I call:


Where the class MyTableUI is defined like this:




I've left off the import statements, as well as the definition for ColorTableCellRenderer() and ColorTableCellEditor(). Those are based on the ones in Sun's tutorials, but are modified such that "focus", "selected" and "editable" are all taken into account in the same way that they are for the DefaultTableCellRenderer.

Note that this simply re-defines the default renderers for JTable. These defaults can still be overridden by any specific JTable. And I have tested that this looks good with the three big Look-and-Feels: Metal, Aqua, and Windows.

I hope this proves useful to others. It really bothers me that the default renderer for Boolean objects doesn't give good feedback for keyboard focus the way the DefaultTableCellRenderer does. I consider that a bug in Swing. Now that I'm used to using this renderer, it is hard to live without it in other applications.
 
reply
    Bookmark Topic Watch Topic
  • New Topic