Results 1 to 6 of 6
Thread: Java Swing Login Form
- 06-01-2010, 03:50 AM #1
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
-
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.
- 06-01-2010, 04:28 AM #3
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.
-
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! } }
- 06-01-2010, 04:59 AM #5
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.
-
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
-
Get data from Login form on webpage
By rosebabz in forum Java ServletReplies: 1Last Post: 01-14-2010, 07:05 PM -
SWT login form
By Sureshgurram in forum SWT / JFaceReplies: 4Last Post: 09-07-2009, 03:14 PM -
Login form issue
By mfayaz in forum New To JavaReplies: 5Last Post: 03-27-2009, 08:35 PM -
help needed regarding LOGIN form
By innocent.crook in forum New To JavaReplies: 2Last Post: 07-08-2008, 04:03 PM -
can i make a login form?
By mitsakos in forum New To JavaReplies: 2Last Post: 12-19-2007, 10:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks