Results 1 to 6 of 6
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Java Swing Login Form

    I am using swing to build an application, and want to have a login screen.

    I basically have 2 panel classes, one being the main page of the application, and the other being the login screen panel.

    My question is, how do I (in java) do a "hold", type of action.

    In other words, In my main program, before I do an initComponents, I do a loginscreen.setVisible(true), which displays the login to the user, and then based on whether or not they put in the correct login. I am going to set a boolean up to be true or false, which I guess will be interpreted by my main applcationView class.

    But how would I create a "pause" after I set the screen visible to wait on the user to hit the "submit" button, at which point I want to check the login, and then run the rest of the program.

    I saw something about wait and notify, but was unsure of how to use them, or if this was the right approach.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    One way is to make the login screen a modal JDialog and not display the main JFrame until after it returns and it's fields are checked and found to comply with your login restraints.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    actually, i meant to say that the login screen is actually a jdialog.

    Here is the first part of the code in my main applicationView class:

    public InventoryView(SingleFrameApplication app) {
    super(app);
    F = new JFrame();
    db = new DatabaseConn(this);
    //Connect to the Database.
    db.Connect();
    Login = new LoginScreen(F, true, db, this);
    Login.setVisible(true);
    initComponents();


    so basically, I want there to be a pause between the login.setVisible(true), and the init components, which basically waits for a response from the jdialog, on whether or not the conditions are met after they click OK on the login screen.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    A modal JDialog will do just that. For instance a JOptionPane is in fact a modal JDialog, so it can demonstrate this well:
    Java Code:
    import java.awt.Dimension;
    import java.awt.GridLayout;
    
    import javax.swing.*;
    
    public class LoginTest {
       private static void createAndShowUI() {
          LoginPanel login = new LoginPanel();
          int response = JOptionPane.showConfirmDialog(null, login, "Please Enter Name and Password", 
                   JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
          
          if (response == JOptionPane.OK_OPTION) {
             String name = login.getName();
             String pWord = login.getPassword();
             
             if (name.equals("Fubar") && pWord.equals("Snafu")) {
                JFrame frame = new JFrame("Main Application");
                JLabel label = new JLabel("Main Application", SwingConstants.CENTER);
                label.setPreferredSize(new Dimension(400, 300));
                frame.getContentPane().add(label);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
             }
             
             else {
                String msg = "Incorrect name and password. \n(try Fubar and Snafu)";
                JOptionPane.showMessageDialog(null, msg);
             }
          }
          
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class LoginPanel extends JPanel {
       private JTextField nameField = new JTextField(10);
       private JPasswordField passwordField = new JPasswordField(10);
       
       public LoginPanel() {
          setLayout(new GridLayout(2, 2, 5, 5)); // change to GridBagLayout later
          add(new JLabel("Name:"));
          add(nameField);
          add(new JLabel("Password:"));
          add(passwordField);
       }
       
       public String getName() {
          return nameField.getText();
       }
       
       public String getPassword() {
          return new String(passwordField.getPassword()); // shouldn't do this!
       }
    }

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    so I tried to create a string in the login class called passOrFail, and basically whenever they login, the string will be pass or "fail". And then in my main application class, right after I make the login screen visible, I say

    if (loginscreen.passorfail().equals("fail"){
    system.out.println("wrong login")
    }
    else{
    initComponents..
    etc
    etc
    etc.
    }


    but I get the error, notStatic method "passorfail" cannot be referenced from a static context.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by sehudson View Post
    but I get the error, notStatic method "passorfail" cannot be referenced from a static context.
    So don't reference it in a static way -- create an object and use this object. Knowledge of static and non-static is a bit of a prerequisite for coding with Swing, and so you might want to hit some of the basic tutorials about object creation first before moving on here.

Similar Threads

  1. Get data from Login form on webpage
    By rosebabz in forum Java Servlet
    Replies: 1
    Last Post: 01-14-2010, 07:05 PM
  2. SWT login form
    By Sureshgurram in forum SWT / JFace
    Replies: 4
    Last Post: 09-07-2009, 03:14 PM
  3. Login form issue
    By mfayaz in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 08:35 PM
  4. help needed regarding LOGIN form
    By innocent.crook in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:03 PM
  5. can i make a login form?
    By mitsakos in forum New To Java
    Replies: 2
    Last Post: 12-19-2007, 10:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •