我如何才能防止触发和JPopupMenu仅Mouse Cursor在选中时显示JTable'Row
我的问题:是否还有其他方法可以getBounds从选定的行中确定并与Mouse位置进行比较...
我的简单sscce展示了不想要的相反状态,可以选择任何行并JPopupMenu从整体上触发JTable
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableCheckBox extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public TableCheckBox() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50), false},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
{"Sell", "Apple", new Integer(3000), new Double(7.35), true},
{"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
createPopupMenu();
}
private void createPopupMenu() {
JPopupMenu popup = new JPopupMenu();
JMenuItem myMenuItem1 = new JMenuItem("cccccccccccccccccccccc");
JMenuItem myMenuItem2 = new JMenuItem("bbbbbbbbbbbbbbbbbbbbbb");
popup.add(myMenuItem1);
popup.add(myMenuItem2);
MouseListener popupListener = new PopupListener(popup);
table.addMouseListener(popupListener);
}
private class PopupListener extends MouseAdapter {
private JPopupMenu popup;
PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
慕码人2483693
largeQ
相关分类