1

How can I target (in JXTable) a specific column values from different rows to open a new frame when I double click?

enter image description here

I have this code for now:

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);

                }
            }
        });

Solved with this code:

     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);
                   }
          }
      });

UPDATE 1:

As @mKorbel said, I want to use a JPopupMenu like this example (provided by @mKorbel). But how can I implement a JDialog which opens when the JMenuItem is clicked.

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());

        }
            }

        }


    }
Community
  • 1
  • 1

1 Answers1

2
  1. don't create a new J(X)Frame on runtime, contents of this container will be the same, no reason for that, create that once time, reuse this container, and for visibility on the screen (to hide / show) use setVisible(false / true)

  2. don't create a J(X)Frame, create a J(X)Dialog with parent to the JXTable, then dialog will be centered to the JXTables

  3. you can to use ListSelectionListener too, but with single selection model

  4. read Oracle tutorial about JList, JTable and ListSelectionListener, logics will be the same and for SwingX components too,

  5. I'd be use JPopup as control for displaying the JDialog, just to avoids annoying events came from mouse or keyboard

EDIT

have to add

  • d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE), then JDialog never will be closed only changed to the setVisible(false);

  • have to code value from selected JTables row,

  • create a new, separate void that fills value from JTables row to the JComponents placed into JDialog

  • be ensure that J(X)Table has changed selectionMode to the single selection

  • (not required) now you can possitioning JDialog on the screen, JDialog still is not visible on the screen

  • thenafter to call d.setVisble(true), I'd suggest wrap this code line to the invokeLater(), then JDialog will be visible after all changes are done, move this event to the end of the Event Dispatch Thread

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I have a popup menu called from specific cell from a JXTable. But the problem now is how can I implement inside popup a JDialog to come up. See the UPDATE 1 please. – Apopei Andrei Ionut Aug 05 '12 at 14:44