Forums Register Login

Using cell editors in JTable

+Pie Number of slices to send: Send
Hi all,

Here is my requirement -

When the value of a particular cell in a JTable gets changed, another cell in the same selected row should display a JButton. I tried using a ListSelectionListener and with valueChanged() method, the JButton can be made to dispplay whenever a value of the selected row gets changed. But, since the renderer and the editor for this JButton is set for the entire column, JButton gets displayed for the entire column on clicking the cells and not only for the row in which the value is changed.

Here is the code for setting the renderer and editor for that column-

TableColumnModel colModel = aTable.getColumnModel();
TableColumn undoColumn = colModel.getColumn(6);
undoColumn.setCellRenderer(new ButtonRenderer());
undoColumn.setCellEditor(new ButtonCellEditor(undoButton));

Is there a way by which the button gets displayed only for the cell whose row has any value change and not for the entire column?
Please clarify

Thanks in advance!!!
+Pie Number of slices to send: Send
 

Originally posted by ram_shyam:
Hi all,

Here is my requirement -

When the value of a particular cell in a JTable gets changed, another cell in the same selected row should display a JButton. I tried using a ListSelectionListener and with valueChanged() method, the JButton can be made to dispplay whenever a value of the selected row gets changed. But, since the renderer and the editor for this JButton is set for the entire column, JButton gets displayed for the entire column on clicking the cells and not only for the row in which the value is changed.

Here is the code for setting the renderer and editor for that column-

TableColumnModel colModel = aTable.getColumnModel();
TableColumn undoColumn = colModel.getColumn(6);
undoColumn.setCellRenderer(new ButtonRenderer());
undoColumn.setCellEditor(new ButtonCellEditor(undoButton));

Is there a way by which the button gets displayed only for the cell whose row has any value change and not for the entire column?
Please clarify

Thanks in advance!!!



You can always specify an editor/renderer for a specific row too. The getTableCellEditor/Renderer arguments are the row and column index. I dont know if this is a good practise or not, but that should solve your problem.
+Pie Number of slices to send: Send
Hi,

I actually searched to do the same as what you have suggested. But I couldnt find any such way to set the editor/renderer to a specific row/cell other than for the entire column.
Could you please send me some useful link on this or explain how this can be done?

Thanks in advance!!
+Pie Number of slices to send: Send
"ram_shyam",
Please check your private messages.
+Pie Number of slices to send: Send
 

Originally posted by ram_shyam:
Hi,

I actually searched to do the same as what you have suggested. But I couldnt find any such way to set the editor/renderer to a specific row/cell other than for the entire column.
Could you please send me some useful link on this or explain how this can be done?

Thanks in advance!!



Something like that; it's real easy.
If you need to access something from another column of the same row, the table itself is available in the method. So if you need the value in column 0 (of the model!), convert it using "table.convertColumnIndexToView(0)", then use its return value with "table.getValueAt" method. Alternatively, get the model using "table.getModel", then use its "getValueAt" method.
+Pie Number of slices to send: Send
Hi,

Thanks for your reply!

I have started implementing the design you have suggested. I will get back to you if I have any more issues.

Meanwhile, I have one more problem in the same renderer/editor.
Say, I need to display a pop-up window on selecting a particular row. So, the renderer and editor of this new window has to be set to all columns of that row and on clicking any column, the window will be displayed.
Since there is no setCellRenderer/setCellEditor for the row, this has to be implemented as I have mentioned above. Please correct me if I am wrong.

Thanks in advance!!
+Pie Number of slices to send: Send
Is it on selection, or on editing? If it is the former, perhaps using a ListSelectionListener (through the table's getSelectionModel method) is a better option.

Also, if you don't want to specifically set one single renderer / editor for all columns, you could create a subclass of JTable and override the getCellRenderer(int row, int column) and getCellEditor(int row, int column) methods:
The latter is how I managed to get a column with different renderers and editors dependant on row, like a check box for booleans, right aligned renderers / editors for numbers, etc.
+Pie Number of slices to send: Send
Hi,

Many thanks for your response!

Now, coming back to the previous problem. I still have the problem in displaying the button only when the values of a particular row is changed. The button gets displayed if I click that button cell which should not happen as the values are not changed for the corresponding row. Also, once the button is displayed for a particular row, if i click some other row, button gets disappeared for the previously selected row. The requirement is that a JButton has to be displayed in a cell whose corresponding row has any changes in its values. The button should remain displayed for that row. Other rows should not have the button displayed unless there is any change in value of other columns in that row.

Here is the code I have implemented. Please correct me where I am going wrong.

Renderer code for the button-
==================

class ButtonRenderer extends JButton implements TableCellRenderer {

public ButtonRenderer() {
setOpaque(true);
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else{
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}

TableModel model = table.getModel();
// get values from the table model's selected row and specific column

if(// check if values are changed from old ones)
{
setText("Undo");
setVisible(true);
return (JButton) value;
}
else
{
JButton button = new JButton("");
button.setBackground(Color.white);
button.setBorderPainted(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setEnabled(false);
return button;
}

Editor code for the button -
class ButtonCellEditor extends DefaultCellEditor
{
public ButtonCellEditor(JButton aButton)
{
super(new JTextField());
setClickCountToStart(0);
aButton.setOpaque(true);
aButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
editorComponent = aButton;
}

protected void fireEditingStopped()
{
super.fireEditingStopped();
}

public Object getCellEditorValue()
{
return editorComponent;
}

public boolean stopCellEditing()
{
return super.stopCellEditing();
}


public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{

return editorComponent;

}
}

Code setting the renderer/editor -
==================================

TableColumn column = colModel.getColumn(6);
button.setVisible(true);
button.setEnabled(true);
column .setCellRenderer(new ButtonRenderer());
column .setCellEditor(new ButtonCellEditor(button));

Please correct me where I am going wrong.

Thanks in advance!!
+Pie Number of slices to send: Send
Hi,

Could you please go through the code and help me where the problem is in rendering the Undo buttons correctly?

Many thanks for your help!!!
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3606 times.
Similar Threads
Obtaining row id in JTable
JTable
JTable Cell Renderers
JTable shows previous data
JTable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:40:04.