Remove Renderer from a JTable - java

I have a JTable with a Renderer written by myself. When I set the JTable disabled, I want it to be painted in gray, and with the current Renderer that's not possible. That's why I want to remove the renderer when I set the JTable disabled, and then when enabled set it again.
Is that possible, or may I take a different solution?

Before setting your custom renderer, get the default renderer from the JTable. Store it in a reference for later use. Now set your custom renderer to JTable.
Before disabling the table, set the default one and when you enable it set the custom one.
I hope this should work for you.

You can set a new DefaultTableRenderer(). It should be possible too in your Renderer. Alternatively you could extend DefaultTableRenderer.

use JXTable (from the SwingX project) - its renderers respect the table enablement automatically
Edit
on reading again:
I have a JTable with a Renderer written by myself. When I set the
JTable disabled, I want it to be painted in gray, and with the current
Renderer that's not possible
why isn't that possible? In your custom renderer simply query the table enablement and configure its state accordingly
public Component getTableCellRendererComponent(...) {
...
myRenderingComponent.setEnabled(table.isEnabled());
return myRenderingComponent;
}

You can check if the table is enabled when the renderer component is returned. You can then set a state variable in the renderer which will render the cell differently when it is disabled than when it is enabled. This approach also makes it possible to allow for other states - I have a renderer that allows for different "profiles" depending on values in a specific column.

Related

Need to "link" table cell editor with its table

I need to customise the selection behaviour of the cells inside a JTable descendant. I have a custom cell editor using a JTextField descendant as the editorComponent; there's a focus listener registered on it which manages the desired selection behaviour.
I need different behaviour when tabbing around the table than when another window or application comes to the front and then goes away again.
This is currently not possible, because the cell editor's editorComponent seems to have no off-the-shelf way of knowing (or telling it) that it's the editor for a table cell, so it doesn't know that it's "inside" the table, so my focusGained() and focusLost() think focus is moving between different windows even if I'm just tabbing around in the table.
SwingUtilities.windowForComponent() returns null for the editorComponent.
Before I roll my own solution, is there an accepted way of dealing with this issue? I can't be the first person to need to do this...

When a custom renderer is set to a jTable does it work everytime it is updated?

I'm using a custom renderer to set background color to every row in a constantly updating jTable depending on a certain value, thing is I'm not sure if I should set the custom renderer after setting a new model to the jTable or if it's fine setting it only once after initializing it.
EDIT: Making a more precise question: Where should I set the custom renderer?
You will need to reapply all renderers/editors when ever you change the model, as they are actually associated with the table's TableColumnModel which is reset based on the information in the new TableModel
You might consider updating the contents of the model instead of applying a new one if you can, it's less of disruption to the UI (generally) and to the user

CellEditor for JXTreeTable does not work

I'm trying to implement a JXTreeTable with scrollable cells.
I used custom TableCellRenderer and it looked good, but renderer does not handle events, therefore I added an custom editor to the JXTreeTable.
I thought the cells would be editable once I override isCellEditable() and let it return true anyway but It turned out not. Cells are still not editable, thus I cannot scroll the cells even there are scroll bars.
Any ideas?

How would a dev make a JTable LOOK editable

I'm writing an application that has a JTable, and an edit button that sets the current selected row to be editable. Then once the user is done altering the data, they can click the edit button again (with text that now says "Save") to save the data.
The problem is though, when I set a row to be editable, there isn't a visible difference. I could add some code to the renderer to draw the editable cells a little differently, but I don't know what the proper way to make a cell look editable is. Change the color? Make it look like a JTextField? What's the standard method?
Thanks!
Really this is a user interface design question, not a programming one.
To do what you want you need to supply an appropriate cell renderer with the changes you desire but you will need to decide on your own settings. One option might just be to look at the difference between an editable and non-editable text area and apply those to all the cells on the table. This may be as simple as setting the renderers to disabled for any read-only rows.

A Table like JTreeTable with editable components like ComboBox, TextArea, CheckBox as Rows

Is there any custom plugin(like JTreeTable with editable java components) in java, like in the above image.
I know this can be done by Extending JTreeTable or JXTreeTable Class or using TreeCellEditor etcetera... But, I need a quite exact structure like shown in the above image, Please guide me and Thanks in advance.
Outline, seen here and here, uses the same renderer and editor schema as JTable. For example, to get a column of checkboxes, your RowModel implementation of getColumnClass() should return Boolean.class and your implementation of isCellEditable() should return true for the Logical2 column. I haven't tried it, but DefaultCellEditor, instantiated with a JCheckBox, should work for the combo column.

Categories

Resources