I have an interesting requirement, wherein there will be buttons (JButton
) in some cells. On pressing of buttons a long task need to be executed and this button should be disabled. On completion of this task, the value of one of the cell has to be updated.
Have tried lots of combinations of TableCellRenderer
and editors but of no avail.
Any insights will be appreciated.
Here's the code snapshot of what I am trying to do. There's no impact of enabling or disabling of the buttons here.
private final JTable table;
public JTableButtonMouseListener(JTable table) {
this.table = table;
}
public void mouseClicked(MouseEvent e) {
int column = table.getColumnModel().getColumnIndexAtX(e.getX());
int row = e.getY()/table.getRowHeight();
if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
Object value = table.getValueAt(row, column);
if (value instanceof JButton) {
JButton thePressedButton = (JButton)value;
System.out.println("Boss Button Has Been Clicked ");
// Extract the Button Label
String theLabel = thePressedButton.getText();
if (theLabel.equals("Action Button"))
{
thePressedButton.setEnabled(false);
}
if (theLabel.equals("X"))
{
// Do the Long Running Job Here
// System.out.println("Started Long Running Job");
// setValueAt("Button Pressed", rowIndex, 1);
// thePressedButton.setText(" ");
thePressedButton.setVisible(false);
}
// table.getModel().setValueAt("Button Pressed", row, 1);
// table.repaint();
//((JButton)value).doClick();
}