我有Jtable
一个按钮渲染器。我已经成功地在其中一列中呈现了按钮。但是,我也希望编辑按钮,为此我使用了按钮编辑器。
单击按钮后,将执行业务逻辑并禁用按钮。
问题是如果有多个记录,则在单击其中一个记录的按钮时,该单元格中的按钮被禁用(应该如此)。但是尝试单击另一条记录的按钮,会禁用之前的按钮执行业务逻辑并启用先前记录中的按钮。
现在按钮在这两条记录之间切换,我无法单击第二条记录的按钮。
我对导致这个问题的原因一无所知。
这是我面临的问题的SSCCE。
下面是 UI 类的代码
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();
}
}
下面是渲染器和编辑器类的代码
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");
}
}
下面是表格模型类的代码
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();
}
}
对此的任何帮助将不胜感激。