mKo*_*bel 10 java swing jtable onmouseover jpopupmenu
如何防止触发并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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
Hov*_*els 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者相反,为了防止弹出窗口仅显示在选定的行上:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个有趣的问题,因为它突出了JComponent上缺少的api :-)
众所周知,注册popupMenus的推荐方法是使用componentPopupMenu属性.相关的api是
void setComponentPopupMenu(JPopupMenu);
JPopupMenu getComponentPopupMenu();
Point getPopupLocation(MouseEvent);
Run Code Online (Sandbox Code Playgroud)
缺少什么(实际上需要这个要求)是
JPopupMenu getComponentPopupMenu(MouseEvent);
Run Code Online (Sandbox Code Playgroud)
这个缺点更令人烦恼,因为在 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();
}
};
Run Code Online (Sandbox Code Playgroud)
副作用是弹出显示不是由键盘触发,只要鼠标位于表格上方的任何位置,这可能是或不是问题.
| 归档时间: |
|
| 查看次数: |
12947 次 |
| 最近记录: |