I added a button to the column in the table but when I clicked on the button it did not work
Closed this issue · 1 comments
I added a button to the column in the table
Table.getColumnModel().getColumn(2).setCellRenderer(new ButtonRenderer());
Table.getColumnModel().getColumn(2).setCellEditor(new ButtonEditor(new JCheckBox(), this));
and this is Event click it.
public class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private int row_;
private JTable table_;
private boolean isPushed;
private int count;
private static Connection con;
private static ResultSet rs;
Main_Screen m1;
private static String status = "";
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("Resource/Bundle");
public ButtonEditor(JCheckBox checkBox, Main_Screen m) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
m1 = m;
button.addActionListener((ActionEvent e) -> {
System.out.println("custom.ButtonEditor");
fireEditingStopped();
});
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
count = 0;
if (isSelected) {
button.setForeground(Color.decode("#43B7BA"));
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label = (value == null) ? "" : "button";
button.setText(label);
table_ = table;
isPushed = true;
row_ = row;
return button;
}
@Override
public Object getCellEditorValue() {
if (isPushed) {
count = count + 1;
if (count == 1) {
selected();
}
}
isPushed = false;
return label;
}
public void selected() {
}
@Override
public boolean stopCellEditing() {
return super.stopCellEditing();
}
@Override
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
but it is not worked.I appreciate all everybody help.
Seems to be a question related to swing JTable in general (and not specifically to this library). I suggest you to compose a short self contained example and re-post it at stackoverflow.com:
- short: current version contains redundant imports, color switching, empty method - clean it before posting
- self contained: it should be possible to simply run it as is - there's no
main()
method at the moment
Also, add explanation of what this example is supposed to do. Is it for a clickable checkbox?
UPD: and mention in your post what exactly is not working: checkbox doesn't appear? Color can't be switched? Button doesn't appear at all?