简体   繁体   中英

How to Execute a selected CheckBox in GUI using Java Netbeans

I want to execute a selected item in palette GUI using Java NetBeans. I intention to select one or many items first, then execute the button. Here is the code:

User user = status.getUser();
Date dated = status.getCreatedAt();
PreparedStatement stmt = null;
Connection conne = null;
try {
    Class.forName("com.mysql.jdbc.Driver");                            
    jTextArea1.append("Connecting to database... " + "\n");
    conne = DriverManager.getConnection("jdbc:mysql://localhostuseUnicode=true&characterEncoding=UTF-8", "root", "");
    jTextArea1.append(status + "\n");
    jTextArea1.append("Inserting records into the table..." + "\n");
    stmt = conne.prepareStatement("set names 'utf8'");
    stmt.execute();
    stmt = conne.prepareStatement("set character set utf8");
    stmt.execute();
    stmt = conne.prepareStatement("INSERT INTO japantweet(ID,date,name,statusLocation,text,source) VALUES (?,?,?,?,?,?)");
    stmt.setInt(1, (int) status.getId());
    stmt.setString(2, getTimeStamp());
    stmt.setString(3, status.getUser().getScreenName());
    stmt.setString(4, user.getLocation());
    stmt.setString(5, status.getText());
    stmt.setString(6, status.getSource());
    stmt.executeUpdate();
    jTextArea1.append("this record inserted!" + "\n");
    jTextArea1.append("=======================" + "\n");
} catch (SQLException se) {
    se.printStackTrace();
} catch (final Exception e) {
    e.printStackTrace();
}

And this is the interface:

在此处输入图片说明

Many thanks for any comment!

If you are using the designer.

  1. Select the run button.
  2. Right click for popup menu and choose Events -> Action -> ActionPerformed. (or just double click the button).

If you are not using the Designer put this in your constructor()

 this.jButton1.addActionListener(this::jButton1ActionPerformed);

...

Either way you action performed function would look like this:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)    {                                         
    if(this.jCheckBox1.isSelected()) {
        // do the thing associated with checkbox1
    }
    if(this.jCheckBox2.isSelected()) {
        // do the thing associated with checkbox2
    }
}  

If both checkboxes are checked it does both things, if only one it will do only the one thing etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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