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-
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-
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.
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?
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.
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.
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.
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; }