0

I am trying to make a 'modal' JDialog ( I hope this is what it's supposed to look like), I want to enter my user and password into my dialog and if it's correct then open my main JFrame where I parse some info. My problem occurs when I do the login button, i cannot refer to the current object(this) with it's textboxes, etc; This is the code, and here's the error :

image error

Also I'm a bit confuse because when I'll have a successful login, I am supposed to open a JFrame..... not sure how to proceed because I am supposed to work with only one JFrame and switch my panels. Thanks in advance

package test_area;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;




public class jpanel1 extends JFrame{

    static boolean loginBool = false; //if this goes true, then I go to my frame




    // JPANEL---------------------------------------------
    public static class panel1 extends JPanel implements ActionListener{


        //simple constructor
    public panel1(){

        JPanel a = this;

        a.setLayout(new GridLayout(5,1,5,5));  

        JLabel username = new JLabel("Username");
        JTextField usernameTxt = new JTextField(8);
        JLabel password = new JLabel("Password");
        JPasswordField passwordTxt = new JPasswordField(55);
        JButton doIt = new JButton("Log In");

        doIt.addActionListener(this);

        a.add(username);
        a.add(usernameTxt);
        a.add(password);
        a.add(passwordTxt);
        a.add(doIt);


        a.setSize(200,200);
        a.setVisible(true);


    }

        //constructor with param
  //  public panel1(JLabel a, JTextField b, JLabel c, JPasswordField d, JButton e){

    //}


    //@Overwritten method

        @Override
        public  void actionPerformed(ActionEvent ae) {
            String user;
            String pw;

            user = a.usernameTxt.getText();
            pw   = a.passwordTxt.getText();

            // package does not exist
        }

}
    // JPANEL---------------------------------------------




    //JFrame
    public jpanel1(){

        JFrame frame = new JFrame("Login Pane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new panel1());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }


    public static void main(String[] args)
    {
       jpanel1 gg = new jpanel1(); //frame;


    }

}

Declared a static boolean that is supposed to kick in once the login is correct, then somehow open the new JFrame...

ExtremeSwat
  • 794
  • 1
  • 12
  • 34

1 Answers1

2

Your code doesn't even have a reference to the JDialog...

Assuming frame is your application's main window of type JFrame and dialog your login dialog of type JDialog. At the begin show the modal login dialog.

frame.setVisible(true);
dialog.setModal(true);
dialog.setVisible(true);

When login succeeds hide the dialog with

dialog.setVisible(false);

You don't need to change modality.

ka3ak
  • 2,435
  • 2
  • 30
  • 57
  • Where am I supposed to enter that those lines of code? Because if I enter them into the Jframe constructor, I get an error, I cannot refer to my Jpanel. – ExtremeSwat May 03 '14 at 14:58
  • Look at JAVA tutorials http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html – ka3ak May 03 '14 at 15:03