Hello I have a JTable And i want to grey out all the disabled checkbox cells i tried with a custom renderer checking isEnabled() and then changing the background color but still not workin. Any suggestions? thanks!!!
-
It should work as you stated. Try with debug and finally call refresh() for component to repaint the changes. – Senthil Apr 27 '11 at 03:28
-
what do you mean by "disabled checkbox cells"? – kleopatra Apr 30 '11 at 13:14
1 Answers
As noted in Concepts: Editors and Renderers, "a single cell renderer is generally used to draw all of the cells that contain the same type of data." You'll need to maintain the enabled
state in your table model.
Addendum: As a concrete example, the data model in this example is a simple array of Date
instances. Overriding getTableCellRendererComponent()
as shown below causes odd days to be disabled. In this case, being odd is a property inherent to the Date
value itself, but the model could be queried for any related property at all.
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int col) {
Calendar calendar = Calendar.getInstance();
calendar.setTime((Date) value);
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
return c;
}
Addendum: In the example above, the DateRenderer
is evoked because the TableModel
returns the type token Date.class
, for which it has been made the default.
table.setDefaultRenderer(Date.class, new DateRenderer());
An identical appearance can be obtained by overriding prepareRenderer()
as shown below, but the method is invoked for all cells, irrespective of class. As a result, prepareRenderer()
is ideal for affecting entire rows, as shown in Table Row Rendering.
private final JTable table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
if (col == DATE_COL) {
Calendar calendar = Calendar.getInstance();
calendar.setTime((Date) model.getValueAt(row, col));
c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
}
return c;
}
};
-
I can't resist, no don't do it this way, @ iassael > excelent example a correct workAround in link posted by `trashgod` here> http://stackoverflow.com/questions/5796139/jtable-change-cell-colors-using-tablecellrenderer – mKorbel Apr 27 '11 at 06:58
-
2@mKorbel: `prepareRenderer()` calls `getTableCellRendererComponent()`, so the result is the same. This approach is more general, particularly if the data model is non-trivial. – trashgod Apr 27 '11 at 15:00
-
+1 I agreed with that, but you can k*i*l*l* me example is example. and Rob there shows ..., – mKorbel Apr 27 '11 at 18:09
-
-
@mKorbel: No problem; I appreciate your critical review. Let me know if I need to change anything. A common problem arises from trying to store state in the renderer. If the state is inherent in the value, then `prepareRenderer()` is easier. – trashgod Apr 27 '11 at 19:00
-
no, your review ins't (aren't) problem, never, but 99pct of codeSuckers totally igrored DefaultTableModel, Abstract is better 'cos contains ten times more code, five more Methods, and everyting must be overRode, that isn't joke, people never believe in the DefaultTableModel, anything which starts with Defalut, cos they are never ever want to reads API, or tutorials <:-)> and we have Jeanette (kleapatra) too, isn't it, she is very criticizes, be sure that heard full of gold – mKorbel Apr 27 '11 at 19:59
-
1@trashgod can't resist nitpicking (if only to not disappoint @mKorbel :-) applying decorations in prepareRenderer vs. on the renderer itself is _not_ the same - the former effects all renderers while the latter only that particular renderer. Oh ... and just in case there's still anybody out there not knowing: SwingX comes with complete consistent support of pluggable visual decorations of rendering components by Highlighter (which are applied in prepareRenderer, to come back to the topic :) – kleopatra Apr 30 '11 at 13:13
-
1@kleopatra: Thank you for commenting. I sensed that I had not understood @mKorbel's point entirely, and I have elaborated above to clarify. One of my favorite things about StackOverflow is being able to update examples. – trashgod Apr 30 '11 at 17:06
-
@trashgod +1 for nice answer reusing your old answers. BTW what would you do if you would also wanted to disable editing of those disabled cells only, not the whole column? If there is other way to Private Message I am not aware of please teach me then. – Boro Apr 30 '11 at 22:18
-
@Boro: Thanks! I'm never sure I understand it until I try to explain it to someone else. :-) Re disabling editing, I'd override `isCellEditable()` in the model, as a function of state. – trashgod Apr 30 '11 at 22:46
-
@Boro: BTW, feel free to adapt (or cite) any old examples of mine as the occasion arises. Sometimes it's easier to focus on a few lines. – trashgod Apr 30 '11 at 22:48
-
@ trashgod +1 ...
but now I can see that original is alway original == it was jerk resistant (don't allowed typos into Input Mask) and in case of any Renderer you have to test "if (value instanceof Date) {...}" and here my knowledge ends ... – mKorbel Apr 30 '11 at 22:49 -
@mKorbel: I wonder if one could use the the [type token](http://download.oracle.com/javase/tutorial/extra/generics/literals.html) pattern to minimize `instanceof`. – trashgod Apr 30 '11 at 23:07
-
@trashgod thanks for allowing me to use/reuse your code. Re: editing in isCelEditable() of the model I know only how to make all cells not editable for a column. I do not know how to get my hands onto a cell from within the model. And I think something like that is required to check if the cell is editable then return false else true. – Boro Apr 30 '11 at 23:14
-
@Boro: You're welcome, but all the SO user content is [cc-wiki](http://creativecommons.org/licenses/by-sa/3.0/) licensed. In your model's `isCellEditable()`, return `true` only for cells you want to be editable. – trashgod Apr 30 '11 at 23:26
-
@trashgod I was expecting that the code is under some licence like this one. Even that the code sharing is permitted by the licence it is nice to be personal about this stuff. RE: editing I would expect to be little intrusive into the example code I would need a boolean list to store if element is editable or not? Maybe what I am after is how to connect tighter the renderer and the model? – Boro Apr 30 '11 at 23:57
-
@Boro: Yes, that's one nice thing about a `TableModel`—it can contain whatever data you need. Why not work up an example and post it as a question. – trashgod May 01 '11 at 02:26
-
@trashgod thanks for your time. I will try to do as you suggest, i.e. to ask a 'proper' question. It will take my a while since I am not 100% sure what I am after, but it relates somehow to having the same in model and renderer without writing lots of code :) – Boro May 01 '11 at 02:31
-
1@trashgod `JTable.prepareRenderer` reports view indices. FWICT `DATE_COL` is a model index. If you want to compare `column` with `DATE_COL`, you need to convert one or the other to model/view. – TT. Feb 16 '16 at 20:00