1

I have a cell in JTable where a JPanel has located in it. JPanel has 2 labels inside it. I want to do different action when left label is clicked, and I want to make another operation when right action is clicked. I do not want to use TableCellEditor, it makes my code so complicated. Becase my cell values has a range of type.

I write following code to get selected component from mouse event, but with no success. I tried also SwingUtilies.convertMouseEvent, but it did not change anything. What is the problem with below code? Why JComponent contains method does not check mouse point.

contSimTable.addMouseListener(new MouseAdapter() {
    public void mouseClicked(final MouseEvent event) {
        if (SwingUtilities.isLeftMouseButton(event)) {
            if (event.getClickCount() == 2) {
                JTable target = (JTable) event.getSource();
                int row = contSimTable.getSelectedRow();
                int column = contSimTable.getSelectedColumn();
                /**
                 * convert from view colum to model.It is column index
                 * which is stored in table model
                 */
                int modelColumn = target.convertColumnIndexToModel(column);

                Object clickedCell = contSimTable.getValueAt(row, modelColumn);
                if (clickedCell instanceof JPanel) {
                    boolean isSecond = false;
                    JLabel a = (JLabel) ((JPanel) clickedCell)
                        .getComponent(0);

                    JLabel b = (JLabel) ((JPanel) clickedCell)
                        .getComponent(1);
                    if (a.contains(event.getPoint())) {
                        isSecond = false;
                    }
                    //                                                      
                    if (b.contains(event.getPoint())) {
                        isSecond = true;
                    }

                }

            }
        }
    }
});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user725455
  • 465
  • 10
  • 36

3 Answers3

2

A TableCellEditor is the correct approach. There are a number of ways to address different data types:

  • By default, as discussed here.

  • By TableColumn, as shown here.

  • By the type-token returned from getColumnClass(), as shown here.

  • By overriding getCellEditor(), as shown here.

For more specific guidance, please edit your question to include an sscce that typifies your chosen approach.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also this [example](http://stackoverflow.com/a/11179669/230513) containing a panel with multiple components. – trashgod Aug 26 '13 at 18:43
0

You are adding the MouseListener to the JTable. It would be much easier for you to add the needed listeners to the single JComponent directly.

Example

How you are doing it:

contSimTable.addMouseListener(new MouseAdapter() {
    public void mouseClicked(final MouseEvent event) 
    {
         //Coordinate calculation for the jlabels
    }
    });

Try:

jlabel.addMouseListener(new MouseAdapter() {
    public void mouseClicked(final MouseEvent event) 
    {
         //Do stuff here
    }
    });
Avsar Himmet
  • 23
  • 1
  • 6
  • As I commented, I do not want to deal with custom table cell editor. I tried this.But I get other problems meanwhile clicking other cells(table.getselectedRow return -1 for mouseevent of table)) – user725455 Aug 26 '13 at 15:14
  • Hi, I found something weird. I just tested your code. It looks like your mouseClicked method is not correct. Try mousePressed(..). – Avsar Himmet Aug 26 '13 at 15:31
0

After clarifying in the comments that contSimTable and target variables are the same JTable instance I believe that the statement int modelColumn = target.convertColumnIndexToModel(column); seems to be the cause for the misbehaviour you encounter.

The JTable.getValueAt() method already calls convertColumnIndexToModel() and convertRowIndexToModel behind the scenes so you should probably not call those methods yourself. (Btw if for some reason you had to convert to the model manually you should do the same for the row right?).

Though it is not clear what you are trying to do since your table seems to store components in its model (a JPanel) instead of data. If you are trying to customize the display of your cells you should use a TableCellRenderer and if you want to edit the cell you should use a TableCellEditor as already suggested by others. Ideally your program should interact with the TableModel only, which would then take care of notifying the table to re-draw itself during data changes.

I hope this helps

c.s.
  • 4,786
  • 18
  • 32