import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
*
* @author Daniel Dormann
*/
public class JLoginDialog extends JComponent implements ActionListener {
private static String[] result = { null, null };
private static JDialog dialog;
private static JTextField tUser;
private static JPasswordField tPassword;
public JLoginDialog(String title, String text) {
GridBagConstraints gbc = new GridBagConstraints();
dialog = new JDialog();
dialog.setTitle(title);
dialog.setModal(true);
dialog.setSize(250, 150);
dialog.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 125, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 75);
dialog.setLayout(new GridBagLayout());
gbc.fill = gbc.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 3;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 10, 0, 10);
JLabel lText = new JLabel(text);
lText.setHorizontalAlignment(JLabel.CENTER);
dialog.add(lText, gbc);
gbc.fill = gbc.NONE;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 10, 0, 0);
JLabel lUser = new JLabel("Benutzer");
dialog.add(lUser, gbc);
gbc.fill = gbc.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 0.5;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 10, 0, 10);
tUser = new JTextField();
tUser.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == evt.VK_ENTER)
OKPressed();
}
});
dialog.add(tUser, gbc);
gbc.fill = gbc.NONE;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 0.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 10, 0, 0);
JLabel lPassword = new JLabel("Passwort");
dialog.add(lPassword, gbc);
gbc.fill = gbc.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 0.5;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 10, 0, 10);
tPassword = new JPasswordField();
tPassword.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == evt.VK_ENTER)
OKPressed();
}
});
dialog.add(tPassword, gbc);
gbc.fill = gbc.NONE;
gbc.anchor = gbc.CENTER;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.weightx = 0.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 10, 0, 10);
JButton bOK = new JButton("OK");
bOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OKPressed();
}
});
dialog.add(bOK, gbc);
gbc.fill = gbc.NONE;
gbc.anchor = gbc.CENTER;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 2;
gbc.gridy = 3;
gbc.weightx = 0.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 0, 0, 10);
JButton bCancel = new JButton("Abbrechen");
bCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
dialog.add(bCancel, gbc);
dialog.setVisible(true);
}
public static String[] showLoginDialog(String text, String title) {
JLoginDialog jld = new JLoginDialog(text, title);
return result;
}
private void OKPressed() {
result[0] = tUser.getText();
result[1] = String.valueOf(tPassword.getPassword());
dialog.setVisible(false);
dialog.dispose();
}
public void actionPerformed(ActionEvent e) {
}
}