1

I a swing application in which i have a table inside it i'm putting a panel that can contain a button.The code is follow

 public class MyCellDataRenderer implements TableCellRenderer, TableCellEditor {


@Override
public Component getTableCellRendererComponent(JTable table, Object    value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    MyCellData myCellData = (MyCellData) table.getValueAt(row, column);

    JPanel panel = GridBagHelper.createPanel();
    if (myCellData.isATableHeader()) {
        panel.setBackground(myCellData.getCellBackgroundColor());
        panel.add(myCellData.getContenant(), GridBagHelper.createGridBagConstraints(0, 0, 1, 1,
                GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH));
        return panel;
    }
  boolean condition=true;
    if (condition==true) {
        panel.setBackground(myCellData.getCellBackgroundColor());
        panel.add(myCellData.getContenant(), GridBagHelper.createGridBagConstraints(0, 0, 1, 1,
                GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH));
        return panel;
    }
    panel.setBackground(myCellData.getCellBackgroundColor());
    panel.add(myCellData.getContenant(), GridBagHelper.createGridBagConstraints(0, 0, 1, 1,
            GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH));

    return panel;

}

My question is can i detect a click on a button that contains inside the panel or not? I'm asking about if it's possible technically or not?

Thanks

BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42
  • Seems you are looking for something like [next](http://tips4java.wordpress.com/2009/07/12/table-button-column/). – alex2410 Jun 23 '14 at 14:36
  • @alex2410: Cited [here](http://stackoverflow.com/q/5555938/230513), I originally thought of this, too. – trashgod Jun 24 '14 at 11:47

2 Answers2

2

In my cell, I have two buttons and three labels; they are all in one panel.

You are correct to use a TableCellRenderer and TableCellEditor. In this complete example, the StatusEditor queries the enclosing StatusPanel and returns a suitable value in its implementation of getCellEditorValue().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • You are welcome; credit to @mKorbel's example; please consider updating your question to reflect the panel requirement. – trashgod Jun 24 '14 at 11:49
0

Yes it is possible and there are few ways how to do it based on your application design. As I do not know the details I would suggest this simple solution:

table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
       final int row = table.rowAtPoint(e.getPoint());
       final int column = table.columnAtPoint(e.getPoint());

       if(isButtonCell(row, column)) { //check if the cell is your button
           handleButtonClicked(); //invoke the click button handle
       }
    }
});
wladimiiir
  • 351
  • 1
  • 4
  • No You did not understand me.In my cell i have two buttons and 3 labels(They are all in one panel).How can i detect which component has been selected from all those component? – BenMansourNizar Jun 23 '14 at 14:14