0

I have a JPanel that contains a JTable and a JButton. The desired functionality is: - for every row of the table the user clicks, a second table appears in the panel - if the button is clicked, a popup table appears

For the table I have implemented a mouse listener like this:

       table.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            int row = table.getSelectedRow();
            int col = table.getSelectedColumn();
            System.out.println("Table: " + row + "  " + col);

            // perform some table processing here
            // the new table to appear is data1

            // update the content of the panel dataP   
            dataP.removeAll();
            dataP.setLayout(new BorderLayout());
            dataP.add(new JScrollPane(data1), BorderLayout.NORTH);
            dataP.updateUI();
            }
        }
    });

The listener works fine until I click the button and the popup table appears. After doing this, if I click on the initial JTable I always get that the selected row/column is -1,-1. Why is this happening? Since the table and the button are two separate components (also placed in different panels), how is it possible for the one to affect the other? Thanks in advance!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
ginou
  • 73
  • 1
  • 6
  • 1
    I think that it will be hard for any of us to guess what could be wrong based on the code snippet you've posted, and that you'll instead want to create and post a [minimal runnable example](http://stackoverflow.com/help/mcve) (please check out link for the details). Also, your code looks like you really want to either use a CardLayout to swap views, or keep the same second JTable in view, but swap models on it. – Hovercraft Full Of Eels Jan 19 '14 at 16:41
  • [This](http://stackoverflow.com/questions/12664023/jtable-getselectedrow-is-returning-1) can be helpful! – Sazzadur Rahaman Jan 19 '14 at 16:45
  • @Sazzadur Rahaman I had seen that but it didn't work. I created a simple example as Hovercraft Full Of Eels suggested and everything was working fine. So I kept looking at my code and finally, in the action of the button, I was updating my initial table and that caused the problem. Thank you all for your time! – ginou Jan 19 '14 at 17:44
  • `Don't use the updateUI() method.` That method is used for a LAF change. When you add/remove components from a visible GUI you should be using `revalidate() and repaint()`. – camickr Jan 19 '14 at 18:43

0 Answers0