简体   繁体   中英

Button in JTable cell not click-able

I have a jtable with the first column having jbuttons. However when i try to click the button nothing happens. Hovering over the button also doesn't change it's shade to show that it's clickable..

I am running this from within a Java Applet.

I am using the Button Column Class from here: http://www.camick.com/java/source/ButtonColumn.java

and here is the code i inserted myself

tablemodel = new DefaultTableModel();
//PnlThinClientTable.COLUMNS is an array of strings with the titles of the columns
tablemodel.setColumnIdentifiers(PnlThinClientTable.COLUMNS);
JTable table = new JTable(tablemodel);


table.setEnabled(false);
table.setDefaultRenderer(table.getColumnClass(5), new CustomTblCellRenderer());
table.setBackground(Color.WHITE);

Action wakeUpRow = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e){
        JTable table = (JTable)e.getSource();
        int modelRow = Integer.valueOf( e.getActionCommand() );         
        System.out.println("Action Performed");
    }
};
// Main.hm_language.get(Language.WAKE_ON_LAN) returns the title of the column i'm interested in
table.getColumn(Main.hm_language.get(Language.WAKE_ON_LAN)).setCellRenderer(new ButtonColumn(table,wakeUpRow,0));
table.getColumn(Main.hm_language.get(Language.WAKE_ON_LAN)).setCellEditor(new ButtonColumn(table, wakeUpRow, 0));

Thanks to @alex2410 for the solution

I had to make sure the cell was Editable

this can be done by either extending the Table upon declaration and overriding the isCellEditable(int row, int col): boolean method, or in my case I overrode isCellEditable(EventObject e):boolean in the Cell Editor which I apply to the column,

hence the snippet within the Cell Editor I am using would be

@Override
public boolean isCellEditable(EventObject e){
    return true;
}

This is as all cells to which the editor is applied need to be editable, as they are all buttons in my case.

Answering to the comment of "how to make 1st column editable" here's how

class MyTableModel extends AbstractTableModel {
    public boolean isCellEditable(int row, int col) {
        if (col == 1) {
            return true;
        } else {
            return false;
        }
    }
}

Anyway I leave How to Use Tables Documentation in case it's needed.

And also this post that could help: How to make a table (Jtable) not editable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM