Hi,
In my login screen, when I enter the username and password I have to press Tab and space to Submit. As the submit button is not set default. The problem is when I use getRootPane I am getting an exception in thread main when i run the application. I am sure there is something wrong with my code. Can somebody please suggest something.
package com.spanlink.cc.bulk;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.RootPaneContainer;
import java.util.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import com.spanlink.cc.bulk.common.BulkUtil;
import com.spanlink.cc.bulk.common.BulkConstant.BulkMessage;
import com.spanlink.cc.bulk.common.BulkConstant.LoginElem;
import com.spanlink.cc.bulk.model.UserApi;
import com.spanlink.cc.domain.misc.UserCredentials;
public class LoginDialog {
private JDialog loginDialog;
private static final String SUBMIT = "SUBMIT";
private static final String CANCEL = "CANCEL";
private static final String LOGIN = "login";
private static final String PASSWORD = "password";
private static final String TITLE = "title";
// private static final String INVALIDMESSAGE = BulkUtil.getLocalMsg(BulkMessage.BLEC1014.getText());
private BulkLoadGUI bulkLoadGUI;
public LoginDialog(BulkLoadGUI bulkLoadGUI){
this.bulkLoadGUI = bulkLoadGUI;
Object [] objects = { buildLoginPanel() };
String message = BulkUtil.getLocalText(TITLE);
JOptionPane pane = new JOptionPane(
message,
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.OK_OPTION,
null,
objects);
loginDialog = pane.createDialog(loginDialog, message);
loginDialog.setVisible(true);
}
private Component buildLoginPanel() {
JPanel Msgpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
final JLabel Msglabel = new JLabel("");
/* public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
g.drawString(Msglabel + g.getColor(), 130, 40);
}*/
// Msglabel.paintComponent(g);
// Msgpanel.setColor(Color.red);
Msglabel.repaint();
Msgpanel.add(Msglabel);
JPanel userpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel Loginlabel = new JLabel(BulkUtil.getLocalText(LOGIN));
final JTextField Login = new JTextField(20);
Loginlabel.setLabelFor(Login);
userpanel.add(Loginlabel);
userpanel.add(Login);
JPanel pwdPanel = new JPanel(new FlowLayout());
JLabel passwordLabel = new JLabel(BulkUtil.getLocalText(PASSWORD));
final JPasswordField pwdField = new JPasswordField(20);
passwordLabel.setLabelFor(pwdField);
pwdPanel.add(passwordLabel);
pwdPanel.add(pwdField);
final JButton submit = new JButton(BulkUtil.getLocalText(SUBMIT));
// getRootPane().setDefaultButton(submit);
// ((Object) getRootPane()).setDefaultButton(submit);
// submit.requestFocus();
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String login = Login.getText();
String passWord = new String(pwdField.getPassword());
boolean submit = validatePassword(login, passWord);
if(submit==true){
loginDialog.dispose();
}
else if(submit==false) {
Msglabel.setText(BulkMessage.BLEC1014.getText());
Login.setText("");
pwdField.setText("");
Login.requestFocus();
}
}
});
// loginDialog.getRootPane().setDefaultButton(submit);
// submit.setVisible(true);
submit.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String login = Login.getText();
String passWord = new String(pwdField.getPassword());
boolean submit = validatePassword(login, passWord);
if(submit==true){
loginDialog.dispose();
}
else if(submit==false) {
Msglabel.setText(BulkUtil.getLocalMsg(BulkMessage.BLEC1014.getText()));
Login.setText("");
pwdField.setText("");
Login.requestFocus();
}
}
});
}
}
});
final JButton cancel = new JButton(BulkUtil.getLocalText(CANCEL));
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
cancel.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
}
}
});
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(Msgpanel);
panel.add(userpanel);
panel.add(pwdPanel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(submit);
buttonPanel.add(cancel);
panel.add(buttonPanel);
return panel;
}
private boolean validatePassword(String username, String password) {
try{
UserApi user = new UserApi();
bulkLoadGUI.setUserCredentials(user.authenticateUser(username, password));
return true;
}catch(Exception exc){
bulkLoadGUI.setUserCredentials(null);
return false;
}
}
}