Reputation: 342
I have a Jtable
to which I have set a button renderer. I have successfully rendered button in one of the columns.However, I also wish to edit the buttons and for that I have used button editor.
Upon clicking the button,business logic is executed and button is disabled.
The issue is if there are more than one records, then upon clicking button of one of the records, the button in that cell is disabled (as it should be).But trying to click the button of the other record,disables the button before executing the business logic and the button in previous record gets enabled.
Now the button toggles between these two records and I am not able to click on the button of the second record.
I am clueless as to what is leading to this issue.
Here's an SSCCE of the problem faced by me.
Below is code for UI Class
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableUI extends JFrame
{
JTable j = null;
JScrollPane scp = null;
public TableUI()
{
j = new JTable();
j.setModel(new Tablemodel());
j.setDefaultRenderer(JButton.class, new ButtonRenderer());
j.getColumn("Column1").setCellEditor(new ButtonEditor());
scp = new JScrollPane(j);
add(scp);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new TableUI();
}
}
Below is the code for renderer and editor class
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
class ButtonRenderer implements TableCellRenderer
{
JButton button;
boolean enabled = true;
public ButtonRenderer()
{
button = new JButton();
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column)
{
button.setText("Install");
button.setEnabled(enabled);
return button;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
}
class ButtonEditor extends AbstractCellEditor implements
TableCellEditor,ActionListener
{
JButton button;
private JTable cwwObjTblMainTable = null;
boolean enabled = true;
int clickCountToStart = 1;
public ButtonEditor()
{
button = new JButton();
button.addActionListener(this);
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row, int column) {
cwwObjTblMainTable=table;
button.setText("Install");
button.setEnabled(enabled);
return button;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Object getCellEditorValue() {
return button.getText();
}
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
}
return true;
}
public void actionPerformed(ActionEvent e)
{
enabled=false;
button.setEnabled(false);
//Business logic execution
System.out.println("Clicked");
}
}
Below is code for Table model class
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;
public class Tablemodel extends AbstractTableModel
{
private static final Class[] columnClasses = {JButton.class,Integer.class};
public int getColumnCount()
{
return 2;
}
public int getRowCount()
{
return 2;
}
@Override
public Object getValueAt(int pRow, int pCol)
{
return 1;
}
public String getColumnName(int pColumn)
{
String[] colheads =
{ "Column1" , "Column2"};
return colheads[pColumn];
}
public Class getColumnClass(int col)
{
return columnClasses[col];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public void removeRow(int pRow)
{
fireTableRowsDeleted(pRow,pRow);
fireTableDataChanged();
}
}
Any help on this would be appreciated.
Upvotes: 1
Views: 6972
Reputation: 9
You should Try this code It works for me hope its useful
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
ta = table;
r = row;
c = column;
if (ta.getValueAt(r, c) == "clicked") {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
return button;
}
Upvotes: 0
Reputation: 109823
JButton.class
no never to put JComponents
to the JTables Cell
,
put String value
to the XxxTableModel
for JButtons Renderer and Editor
, String value
works for JButtons Components
correctly
use Table Button Column by @camickr
search on this Great Code Depots for proper AbstractTableModel
Upvotes: 1
Reputation: 347332
There is ever only one instance of an editor per table column. This means, the editor that was used from row 1 is the same editor for for 2 (and every other row for that column)
What you need to is set the button state when getTableCellEditorComponent
is called.
This may require you to carry some logic with the model that you can interrgate when the method is called in order to determine of the action should be carried out, or better yet, have the model return a value for the column/row, that way you can use the value
parameter to determine the state of button. You would, of course need to set the value within the table model ;)
Upvotes: 1