Results 1 to 3 of 3
Thread: Java - Swing - LOGIN Page
- 10-09-2011, 08:38 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Java - Swing - LOGIN Page
Dear All
Instead of manualy feeding the user name and password in the code mention below , if i want to connect to MYSQL Database and get the username and password and if it match then allow . Can you please provide with the 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_CLOS E);
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!
}
}
-
Re: Java - Swing - LOGIN Page
Would you like to have a seat, kick your shoes off and help yourself to some doughnuts while we do your work for you?
Sorry but it doesn't work that way as we're volunteers and not here for this purpose. I suggest that you look for the tutorial for what you're trying to do, and then if you get stuck show us your code (with code tags so we can read it), your errors and perhaps we'll be able to help.
- 10-10-2011, 09:29 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Re: Java - Swing - LOGIN Page
I am really sorry , and i totaly agree with your point , i had written the code but i forgot to add between . Actually i had gone through some tutorial but still could not find a solution .
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! } }
Similar Threads
-
Java Swing Login Form
By sehudson in forum Advanced JavaReplies: 5Last Post: 06-01-2010, 11:53 AM -
Java Login Page
By turkuaz07 in forum Advanced JavaReplies: 1Last Post: 03-14-2009, 09:18 PM -
login page
By banie in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-09-2008, 08:37 AM -
JSP login page
By banie in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-06-2008, 04:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks