将特定的JXTable列值定位到新帧中打开

时间:2012-08-05 08:30:59

标签: java swing jtable mouseevent swingx

当我双击时,如何定位(在JXTable中)来自不同行的特定列值以打开新帧?

enter image description here

我现在有这个代码:

myTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    JXTable target = (JXTable)e.getSource();
                    int row = target.getSelectedRow();
                    int column = target.getTableColumn(3);  //for example the third column
                    new Detail().setVisible(true);

                }
            }
        });

解决了这段代码:

     final JOptionPane pane = new JOptionPane((Frame) null, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION);

     final JDialog d = pane.createDialog((Frame) null, "Comments");
                   d.setModal(false);
                   d.setAlwaysOnTop(true);
                   d.pack();
     myTable.addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
                   if (e.getClickCount() == 2) {
                          int row = myTable.rowAtPoint(e.getPoint());
                          Object value1 = myTable.getValueAt(row, 4);
                          pane.setMessage(value1);
                   if (!d.isVisible())
                   d.setLocationRelativeTo(myTable);
                   d.setVisible(true);
                   }
          }
      });

更新1:

正如@mKorbel所说,我想使用像this example这样的JPopupMenu(由@mKorbel提供)。但是如何实现在单击JMenuItem时打开的JDialog。

private void createPopupMenu() {
        JPopupMenu popup = new JPopupMenu();
        JMenuItem MenuItemComments = new JMenuItem("Show Comments");
        JMenuItem MenuItemReference = new JMenuItem("Show Reference");
        popup.add(MenuItemComments);
        popup.add(MenuItemReference);

        MouseListener popupListener = new PopupListener(popup);
        Table_Employee.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_Employee.getSelectedRow() != -1) {
                maybeShowPopup(e);
            }
        }
        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {

        // get row that pointer is over
        int row = Table_Employee.rowAtPoint(e.getPoint());

        // if pointer is over a selected row, show popup
        if (Table_Employee.isRowSelected(row)) {
           popup.show(e.getComponent(), e.getX(), e.getY());

        }
            }

        }


    }

1 个答案:

答案 0 :(得分:2)

  1. 不要在运行时创建新的J(X)Frame,此容器的内容将是相同的,没有理由,创建一次,重用此容器,以及在屏幕上的可见性(到hide / show)使用setVisible(false / true)

  2. 不要创建J(X)Frame,使用J(X)Dialog的父级创建JXTable,然后对话框将以JXTables

    为中心
  3. 您也可以使用ListSelectionListener,但使用单一选择模式

  4. 阅读有关JListJTableListSelectionListener的Oracle教程,逻辑将相同,对于SwingX components也是如此,

  5. 我会使用JPopup as control for displaying the JDialog,只是为了避免来自鼠标或键盘的恼人事件

  6. 修改

    必须添加

    • d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE),然后JDialog永远不会被关闭,只会更改为setVisible(false);

    • 必须对选定的JTable

    • 进行编码
    • 创建一个新的,单独的空格,将JTable行中的值填入JComponent

    • 中的JDialog
    • 确保J(X)Table已更改selectionModesingle selection

    • (不是必需的)现在您可以在屏幕上显示JDialog,屏幕上仍然无法显示JDialog

    • 然后调用d.setVisble(true),我建议将此代码行包装到invokeLater(),然后在完成所有更改后显示JDialog,将此事件移至Event Dispatch Thread

    • 的结尾