Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Using cell editors in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using cell editors in JTable" Watch "Using cell editors in JTable" New topic
Author

Using cell editors in JTable

ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
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!!!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
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!!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"ram_shyam",
Please check your private messages.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
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!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
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!!
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using cell editors in JTable
 
Similar Threads
JTable Cell Renderers
JTable
Obtaining row id in JTable
JTable shows previous data
JTable