Java应用程序中的多窗口

Java应用程序中的多窗口,java,swing,jtable,mouselistener,listselectionlistener,Java,Swing,Jtable,Mouselistener,Listselectionlistener,另一个问题。我想双击JTable,用表单打开新窗口。所以最后我这样做了: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent event){

另一个问题。我想双击JTable,用表单打开新窗口。所以最后我这样做了:

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

                    }
                });
            }   

        }
    });
table.setSelectionMode(ListSelectionModel.SINGLE_选择);
table.getSelectionModel().addListSelectionListener(新ListSelectionListener()){
public void值已更改(ListSelectionEvent事件){
int viewRow=table.getSelectedRow();
如果(视图行<0)
System.out.println(“LOL”);
否则{
final int modelRow=table.convertRowIndexToModel(viewRow);
表.addMouseListener(新的MouseAdapter(){
公共无效mouseClicked(MouseEvent e){
如果(如getClickCount()==2)
试一试{
新书单();
}捕获(IOE1异常){
e1.printStackTrace();
}
}
});
}   
}
});

它可以工作,但并不完美。当我第一次双击JTable时,它会打开两个窗口(为什么不打开一个?),下次它会打开4个窗口,然后再打开6个窗口,等等。有什么想法吗?也许我应该用不同的方法?谢谢你的帮助

花点时间查看您的代码

每次选择更改时,您都会添加一个新的
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();
                        }

                }
            });
        }   

    }
});

在你用大量新窗口轰炸你的用户之前,也要看一看…

天哪,真是个愚蠢的错误。甚至没有朝那个方向看。我不使用很多框架,事实上它是唯一的。不过,这是我在家里的小图书馆,我自己用的。非常感谢你的帮助,我成功了。有时我们看不到森林,只看到树木——欢迎来到我的世界;)
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();
                }
            }
    }
});