简体   繁体   English

从表中的复选框获取选中的行

[英]Getting checked rows from Checkbox in Table

I would to create a java program whereby there is a JTable(which fetches content from mysql database and one of its columns is boolean). 我将创建一个Java程序,其中有一个JTable(它从mysql数据库中获取内容,并且其列之一是布尔值)。 There is only one button which when clicked, should pop up a message displaying which rows have been checked(the rows whose checkbox is checked). 只有一个按钮,单击时应弹出一条消息,显示已选中哪些行(选中其复选框的行)。 Here is my code; 这是我的代码;

public class NewJFrame extends javax.swing.JFrame   {

public NewJFrame() {


    initComponents();        
    setTitle("Found Items");
    try {
        String myDriver = "com.mysql.cj.jdbc.Driver";
        String myUrl = "jdbc:mysql://localhost:3306/lostfound";
        Class.forName(myDriver);
        Connection c = DriverManager.getConnection(myUrl, "root", "");
        String sql = "SELECT * FROM found";
        Statement st = c.createStatement();

        // execute the query, and get a java resultset
        ResultSet rs =st.executeQuery(sql);
        int row =0;
        while(rs.next())
        {
            private static final int BOOLEAN_COLUMN = 4; 
        String name = rs.getString("name");
        String description = rs.getString("description");
        String location = rs.getString("location");
        meza.getModel().setValueAt(name, row, 0); 
        meza.getModel().setValueAt(description, row, 1);   
        meza.getModel().setValueAt(location, row, 2);  
        row++;

        }

    }
    catch (SQLException e)
    {
        System.out.println( e.getMessage() );
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
    meza.show();
    claim.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i =0; i<meza.getRowCount(); i++)
            {
                Boolean checked  = Boolean.valueOf(meza.getValueAt(i,3).toString());
                String col = meza.getValueAt(i, 1).toString();
                //DISPLAY POP UP
                if(checked)
            {
                JOptionPane.showMessageDialog(null,col);
            }
            }
        }
    });

}

However, i am getting this error. 但是,我收到此错误。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at NewJFrame$1.actionPerformed(NewJFrame.java:65) NewJFrame $ 1.actionPerformed(NewJFrame.java:65)上的线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException

Kindly assist 请协助

Don't declare your variable with private or public in your method : 不要在您的方法中使用private或public声明变量:

private static final int BOOLEAN_COLUMN = 4; //this is forbidden

Instead use : 而是使用:

int BOOLEAN_COLUMN = 4;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM