samedi 6 mars 2021

How to insert JCheckBox value into a JTextField as a replacement if checked?

But I'm making this client formula and the list of clients is displayed in a JTable and sent to my SQL Database. I want a option saying if there is no name yet (a newborn, new business etc.) then the JCheckbox "unknown" is checked instead of filling the name in the JtextField area.

I think I should use the if-else statement, but I am unsure how to really make it work that way in consideration of that they are two different things. I am not sure if the code options I have found could be used at all cause they are used for 2 checkboxes in the tutorial, or if there is a complete 3rd option/solution.

addButton.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent arg0) {
    try { 
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/num klienter", "root", "");    
        PreparedStatement pst = con.prepareStatement("INSERT INTO klient (Name,Birthday,Description) VALUES (?,?,?)");

        if (unknownCB.isSelected()) {??what code to insert here??}
        //nameTxt = "Unknown";? 
        //pst.setString(1, unknownCB);?

        else { pst.setString(1, nameTxt.getText());}

        pst.setString(2, dayTxt.getText()+"-" + monthTxt.getText()+"-" + yearTxt.getText());
        pst.setString(3, descriptionTxt.getText());
        pst.executeUpdate();
        
        JOptionPane.showMessageDialog(null, "Saved in database");
        upDateDB();
        
        Clientpage cSite= new Clientpage();
        cSite.setVisible(true);
        dispose();
    }catch (Exception ex){
        JOptionPane.showMessageDialog(null, ex); 
    }}
});

Aucun commentaire:

Enregistrer un commentaire