我在JTable
其中有一个单元格JPanel
。JPanel
里面有 2 个标签。单击左标签时我想执行不同的操作,单击右操作时我想执行另一个操作。我不想使用TableCellEditor
,它使我的代码如此复杂。因为我的单元格值有一个类型范围。
我编写以下代码从鼠标事件中获取选定的组件,但没有成功。我也试过SwingUtilies.convertMouseEvent
了,但它没有改变任何东西。下面的代码有什么问题?为什么JComponent
contains 方法不检查鼠标点。
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;
}
}
}
}
}
});