0

i want to add Jdialog before frameview...my frameview consist of my main page of application. i just want to add Jdialog which get password from user and then enter in the Main frame. can any one tell me how can i achieve this in java swing??

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mushahid Hussain
  • 4,052
  • 11
  • 43
  • 62

3 Answers3

2
MyMainPanel mainPanel = new MyMainPanel();
LoginPanel loginPanel = new LoginPanel();

JFrame mainApp = new JFrame();
mainApp.add( mainPanel );
mainApp.pack();
mainApp.setVisible(true);

JDialog dialog = new JDialog( mainApp, true );
dialog.add( loginPanel );
dialog.setVisible( true );

if( login.isAuthenticated() ) { // after dialog is dismissed we can set the user
    mainPanel.setAuthenticatedUser( loginPanel.getAuthenticatedUser() );
} else {
    System.exit(-1);
}

That will show a dialog in front of your main panel and the user won't be able to use it until they login because its modal, and your LoginPanel can force the user to login by not offering any other option but Login, Signup, etc.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
1

by reading

1) How to use Top-Level Containers

2) How to Make Dialogs with methods

  • setModal

  • ModalityTypes

  • toFront

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You could use following constructors to make a JDialog parent-less

JDialog d = new JDialog((Dialog)null);

JDialog d = new JDialog((Window)null);

JDialog d = new JDialog((Frame)null);

Quick code sample :

public class TestFrame extends JFrame{
    public TestFrame(){
        setSize(100,200);
    }

    public static void main(String[] args) {
            //Using null constructor ( Since JDK 6)
        final JDialog loginDialog = new JDialog((Dialog)null);
            //just a button for demo
        JButton okButton = new JButton("Login");
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                TestFrame test = new TestFrame();
                test.setVisible(true);
                loginDialog.dispose();
            }
        });
        loginDialog.getContentPane().add(okButton);
        loginDialog.pack();
        loginDialog.setVisible(true);
    }
}
ring bearer
  • 20,383
  • 7
  • 59
  • 72
  • just tell me which properties i need to change when i drop the jdialog in my swing application so that it will open at first get the password and then allows to open the main frame. – Mushahid Hussain Dec 22 '11 at 16:05
  • @james - You need to construct your JDialog using one of the constructors I listed. If you read through the sample I provided, you can see that the dialog is created first as part of running your application (`main` method). On the dialog's "Login" Action, we are setting "main frame" visible. (lines 13,14,15). – ring bearer Dec 22 '11 at 16:57