如果仅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);
}
@Override
public void mouseReleased(MouseEvent e) {
if (table.getSelectedRow() != -1) {
maybeShowPopup(e);
}
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableCheckBox frame = new TableCheckBox();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}
答案 0 :(得分:12)
你在寻找这样的东西吗?
仅显示所选行的弹出窗口
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
// get row that pointer is over
int row = table.rowAtPoint(e.getPoint());
// if pointer is over a selected row, show popup
if (table.isRowSelected(row)) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
或者相反,为了防止弹出窗口只显示在选定的行上:
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
int row = table.rowAtPoint(e.getPoint());
int[] selectedRows = table.getSelectedRows();
if (!table.isRowSelected(row)) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
答案 1 :(得分:7)
这是一个有趣的问题,因为它突出了JComponent上缺少的api :-)
众所周知,注册popupMenus的推荐方法是使用componentPopupMenu属性。相关的api是
void setComponentPopupMenu(JPopupMenu);
JPopupMenu getComponentPopupMenu();
Point getPopupLocation(MouseEvent);
缺少的(实际上需要这个要求)是
JPopupMenu getComponentPopupMenu(MouseEvent);
这个缺点更令人讨厌,因为在 getComponentPopup()之后调用了getPopupLocation(由LAF深处的AWTEventHelper)。因此,存储没有余地,例如存储可能触发弹出窗口的最后一个鼠标事件,然后决定哪个/是否返回弹出窗口。并且为该位置返回null只会导致在鼠标位置显示它
唯一(脏)黑客(我完全不愿意弄脏MouseListener ;-)是覆盖getComponentPopup并决定是否根据当前鼠标位置返回它
table = new JTable(model) {
/**
* @inherited <p>
*/
@Override
public JPopupMenu getComponentPopupMenu() {
Point p = getMousePosition();
// mouse over table and valid row
if (p != null && rowAtPoint(p) >= 0) {
// condition for showing popup triggered by mouse
if (isRowSelected(rowAtPoint(p))) {
return super.getComponentPopupMenu();
} else {
return null;
}
}
return super.getComponentPopupMenu();
}
};
副作用是弹出显示不是由键盘触发的,只要鼠标在桌子上方的任何位置,这可能是或不是问题。