• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using cell editors in JTable

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"ram_shyam",
Please check your private messages.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic