Search code examples
javaswingjtablemouselistenerlistselectionlistener

Multiplying windows in Java App


another problem. I wanted to make doubleclick on JTable which open new window with form. So finally i made it this way:

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent event){
            int viewRow = table.getSelectedRow();
            if(viewRow < 0)
                System.out.println("LOL");
            else{
                final int modelRow = table.convertRowIndexToModel(viewRow);
                table.addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent e){
                        if(e.getClickCount() == 2)
                            try {
                                new BookForm();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }

                    }
                });
            }   

        }
    });

It works, but not perfect. First time when i doubleclick on JTable it opens 2 windows (why not one?), next time it opens 4 windows, next another 6 windows, etc. Any ideas? Maybe i should have to use different method? Thanks for help!


Solution

  • Take a second to look over your code...

    Each time the selection changes, you add a new MouseListener

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent event){
            int viewRow = table.getSelectedRow();
            if(viewRow < 0)
                System.out.println("LOL");
            else{
                // You add a new mouse listener...
                final int modelRow = table.convertRowIndexToModel(viewRow);
                table.addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent e){
                        if(e.getClickCount() == 2)
                            try {
                                new BookForm();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
    
                    }
                });
            }   
    
        }
    });
    

    So, when you "finally" double click a row, you will have 1-n MouseListeners registered against the table...

    You could just get rid of the selection listener and simple add the MouseListener directly to the table...

    table.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            if(e.getClickCount() == 2)
                int selectedRow = table.getSelectedRow();
                if (selectedRow > -1) {
                    int modelRow = table.convertRowIndexToModel(selectedRow);
                    try {
                        new BookForm();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
        }
    });
    

    Also take a look at The Use of Multiple JFrames: Good or Bad Practice? before you bomb bard your user with lots of new windows...