0

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();
            }
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user3526364
  • 31
  • 1
  • 6
  • 5
    *"Have tried lots of combinations of TableCellRenderer and Editors but of no avail. Any insights will be appreciated."* My insight tells me that people want to see what you tried. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Dec 26 '14 at 09:14
  • 1
    Take a look at [Using Other Editors](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editor) and [Using Custom Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer) and [Table Button Column](http://tips4java.wordpress.com/2009/07/12/table-button-column/) – MadProgrammer Dec 26 '14 at 10:18
  • 1
    And [another example](http://stackoverflow.com/questions/17565169/unable-to-add-two-buttons-in-a-single-cell-in-a-jtable/17565826#17565826) an [example](http://stackoverflow.com/questions/12706306/how-can-i-add-jbutton-to-last-two-column-cells-of-row/12706653#12706653) – MadProgrammer Dec 26 '14 at 10:22
  • 2
    *"Here's the code snapshot.."* Note that an uncompilable code snippet is not an MCVE. If you thought you understood the linked article, go back and read it again. If you hadn't read it, read it now. – Andrew Thompson Dec 26 '14 at 11:43
  • 1
    Also consider the approach suggested [here](http://stackoverflow.com/a/27557106/230513). – trashgod Dec 26 '14 at 12:28
  • Thanks A Lot to all. I could make it work with combination of Editor,Renderer and Mouse Adapter. The key to success were two methods of JTable, convertRowIndexToModel & convertColumnIndexToModel – user3526364 Dec 30 '14 at 13:01

0 Answers0