1

I'm using Java eclipse for my database but eventually I encounter error like this: JOptionPane: parentComponent does not have a valid parent I don't know what is the problem please somebody explain because I'm just 2nd year IT student and I want to learn more about Java I want to complete my Goals.

import java.awt.EventQueue;
import java.lang.*;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;

import javax.swing.*;

public class Login {

  Connection connection = null;

  private JFrame frame;
  private JTextField username;
  private JPasswordField password;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Login window = new Login();
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */

  public Login() {
    initialize();
    connection = Database.dbconector();

  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    username = new JTextField();
    username.setBounds(127, 55, 200, 50);
    frame.getContentPane().add(username);
    username.setColumns(10);

    password = new JPasswordField();
    password.setBounds(127, 125, 200, 50);
    frame.getContentPane().add(password);

    JButton btnLogin = new JButton("Log in");
    btnLogin.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {


        try {
          String query = "select * from bsit-db where Username=? and Password=?";
          PreparedStatement pst = connection.prepareStatement(query);
          pst.setString(1, username.getText());
          pst.setString(2, password.getText());


          ResultSet rs = pst.executeQuery();
          int count = 0;
          while (rs.next()) {
            count = count + 1;
          }
          if (count == 1) {
            JOptionPane.showInternalInputDialog(null, "Successfully Login to your account!");



          } else {
            JOptionPane.showInternalInputDialog(null, "Incorrect ID/Password. Please Try Again!");
          }
          rs.close();
          pst.close();

        } catch (Exception e1) {
          JOptionPane.showInternalInputDialog(null, e1);

        } finally {
          try {

          } catch (Exception e1) {
            JOptionPane.showInternalInputDialog(null, e1);
          }
        }


      }
    });
    btnLogin.setBounds(103, 202, 112, 37);
    frame.getContentPane().add(btnLogin);

    JButton btnClear = new JButton("clear");
    btnClear.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {

        username.setText(null);
        password.setText(null);
      }
    });
    btnClear.setBounds(237, 202, 112, 37);
    frame.getContentPane().add(btnClear);

    JLabel lblUsername = new JLabel("username");
    lblUsername.setBounds(66, 55, 200, 50);
    frame.getContentPane().add(lblUsername);

    JLabel lblPassword = new JLabel("password");
    lblPassword.setBounds(66, 125, 200, 50);
    frame.getContentPane().add(lblPassword);
  }
}

this is the console error

Sun Aug 06 20: 54: 43 CST 2017 WARN: Establishing SSL connection without server 's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn' t set.For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'.You need either to explicitly disable SSL by setting useSSL = false, or set useSSL = true and provide truststore for server certificate verification. java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent at javax.swing.JOptionPane.createInternalFrame(Unknown Source) at javax.swing.JOptionPane.showInternalOptionDialog(Unknown Source) at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source) at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source) at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source) at Database.dbconector(Database.java: 19) at Login. < init > (Login.java: 44) at Login$1.run(Login.java: 29) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

pppery
  • 3,731
  • 22
  • 33
  • 46
ahja
  • 13
  • 4

1 Answers1

0

showInternalInputDialog is for option panes meant to appear in a JDesktopPane. Look instead to showInputDialog.

See this question: JOptionPane: parentComponent does not have a valid parent

r3dst0rm
  • 1,876
  • 15
  • 21