0

I wrote simple code which accesses a local database, this code opens like a pop-up window where I write data in the text fields and I press save and it updates the information in the database. After the update, it should close the old window and open a new one so I can keep adding data. However, instead, the old window doesn't close and it opens a new window on top of that that I cannot edit at all. I'm trying to find out how I can fix this.

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String id = jLabel3.getText();
        String name = jTextField1.getText();
        String opt1 = jTextField2.getText();
        String opt2 = jTextField3.getText();
        String opt3 = jTextField4.getText();
        String opt4 = jTextField5.getText();
        String answer = jTextField6.getText();
        try {
           Connection con = ConnectionProvider.getCon();
           PreparedStatement ps = con.prepareStatement("insert into question values(?, ?, ?, ?, ?, ?, ?)");
           ps.setString(1, id);
           ps.setString(2, name);
           ps.setString(3, opt1);
           ps.setString(4, opt2);
           ps.setString(5, opt3);
           ps.setString(6, opt4);
           ps.setString(7, answer);
           ps.executeUpdate();
           JFrame jf = new JFrame();
           jf.setAlwaysOnTop(true);
           JOptionPane.showMessageDialog(jf, "Successfully Updated");
           setVisible(false);
           new AddNewQuestion().setVisible(true);
        }
        catch(Exception e) {
            JFrame jf = new JFrame();
            jf.setAlwaysOnTop(true);
            JOptionPane.showMessageDialog(jf, e);
            
        }
    }  
  • It seems that you never actually close the window. Take a look at [how to programmatically close a jframe](https://stackoverflow.com/questions/1234912/how-to-programmatically-close-a-jframe). – Heiko Ribberink Mar 18 '22 at 08:50

0 Answers0