0

I want to develop a Swing desktop app but if I clicked the login button, it doesnt go to next page. Anyone help me?

ActionListener code below:

private void loginAddActionListener(java.awt.event.ActionEvent evt) {                         

    int count = 0;
    try {
        String sql = "SELECT * FROM tbl_name WHERE username=? AND password=?";
        c = ConnectionKimyaLab.getConnection();
        ps = c.prepareStatement(sql);
        ps.setString(1, username.getText());
        ps.setString(2, password.getText());
        rs = ps.executeQuery();

        while (rs.next()) {
            count = count + 1;
        }

        if (count == 1) {
            JOptionPane.showMessageDialog(null, "Correct", "Main Page Title", JOptionPane.INFORMATION_MESSAGE);
            Next_JPanel = new Next_JPanel ();
            k.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "Incorrect", "Main Page Title", JOptionPane.WARNING_MESSAGE);
        }
    } catch (HeadlessException | SQLException e) {
        JOptionPane.showMessageDialog(null, e);
    } finally {
        try {
            rs.close();
            ps.close();
            //conn.close();
        } catch (Exception e) {
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `Next_JPanel = new Next_JPanel ();` << it is very error prone to have variables called like their class. Variables should start with a lower-case letter. – Arnaud Aug 17 '17 at 12:59
  • 1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Aug 17 '17 at 13:03
  • Thanks Andrew Thompson and also im using camelCase naming but i wanted to show you that code – Tolga DURAN Aug 18 '17 at 15:03

1 Answers1

1

You should checkout the CardLayout example provided by Java:

https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

It helps you to ready multiple user interfaces at once in the same container, but make only one visible at any one time.

Antony Ng
  • 767
  • 8
  • 16
  • I examined it before but i didnt used it efficiency as i want. But i will checkout that example again. Thanks Antony... – Tolga DURAN Aug 18 '17 at 15:04