当JTable上的鼠标侦听器触发时,组件位置无效

时间:2012-08-16 21:51:13

标签: java swing jtable

我希望当我点击JTable上的某个单元格时,JFrame中的某个标签中的某些文字发生了变化。我写下面的代码;但是当我点击时,组件的位置被破坏,我看到一个重新排列的gui。我也没有在日志文件中得到任何异常!

JTable鼠标监听器的代码部分:(xJTable

x.addMouseListener(new MouseListener() {

    @Override
    public void mouseClicked(MouseEvent e) {

        try {
            String temp = (String) model.getValueAt(x.getSelectedRow(), 0);
            Class.forName(Configure.driver);
            Connection con = DriverManager.getConnection(Configure.url
                + Configure.dbName, Configure.userName, Configure.password);

            java.sql.PreparedStatement prs2 = con.prepareStatement(
                "select count(*) as tedad from messages where receiver=?");

            prs2.setString(1, temp);
            temppResultSet = prs2.executeQuery();
            if (temppResultSet.next()) {

                int RecNum = temppResultSet.getInt("tedad");
                System.out.print("RECEIVED NUM: " + RecNum);

                numberOfMessageReceiveByuser.setText("dsdsdsaf");//LABEL NAME
            }
        } catch (Exception ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }

    //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }
});

1 个答案:

答案 0 :(得分:0)

问题解决了。 我静态设置了所有组件的位置(而不是布局管理器)。 当你想静态设置位置时,你应该使用.setLayout(null)将组件布局设置为null 这是我忘记做的手指笔记!