My program is written in Java using Eclipse and what my program does is first you have to login your username and password to the server and once that is verified, then you go to a new page (frame) where I have a JMenuBar, JMenu, and JMenuItem. The JMenuItem that I am working on is called "Logout" under the JMenu Exit, when you click on logout at the moment, it closes the screen. What I want to happen is to redirect me back to the login page. I've tried many functions, but none of them worked. I would appreciate the help.
Here is my program:
public class ScanTest2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static LoginDialog loginDialog;
public ScanTest2() {
loginDialog = new LoginDialog(this, true);
loginDialog.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// create new frame for new page
JFrame frame = new ScanTest2();
//create a Menu bar
JMenuBar menuBar = new JMenuBar();
frame.getContentPane().setBackground(Color.BLUE);
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/home/a002384/logo2.bmp")))));
} catch (IOException e) {
e.printStackTrace();
}
frame.setTitle("Scan Gun Information");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// create Menus
JMenu databaseMenu = new JMenu("Database");
JMenu ticketMenu = new JMenu("Tickets");
JMenu inquiriesMenu = new JMenu("Inquiries");
JMenu reportsMenu = new JMenu("Reports");
JMenu exitMenu = new JMenu("Exit");
// create Menu Items
JMenuItem lotMenuItem = new JMenuItem("Lots");
lotMenuItem.setMnemonic(KeyEvent.VK_L);
lotMenuItem.setActionCommand("Lots");
JMenuItem gunMenuItem = new JMenuItem("Guns");
gunMenuItem.setMnemonic(KeyEvent.VK_G);
gunMenuItem.setActionCommand("Guns");
JMenuItem batteryMenuItem = new JMenuItem("Batteries");
batteryMenuItem.setMnemonic(KeyEvent.VK_B);
batteryMenuItem.setActionCommand("Batteries");
JMenuItem logoutMenuItem = new JMenuItem("Logout");
logoutMenuItem.setMnemonic(KeyEvent.VK_L);
logoutMenuItem.setActionCommand("Logout");
MenuItemListener menuItemListener = new MenuItemListener();
lotMenuItem.addActionListener(menuItemListener);
gunMenuItem.addActionListener(menuItemListener);
batteryMenuItem.addActionListener(menuItemListener);
logoutMenuItem.addActionListener(menuItemListener);
// add Menu Items to Menus
databaseMenu.add(lotMenuItem);
databaseMenu.add(gunMenuItem);
databaseMenu.add(batteryMenuItem);
exitMenu.add(logoutMenuItem);
// add menu to MenuBar
menuBar.add(databaseMenu);
menuBar.add(ticketMenu);
menuBar.add(inquiriesMenu);
menuBar.add(reportsMenu);
menuBar.add(exitMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
} // end of run method
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Logout"))
System.exit(0);
// go back to login screen
}
}
}); // end of Runnable
}// end of Main
}
class LoginDialog extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JLabel loginlbl = new JLabel("Username");
private final JLabel Passwordlbl = new JLabel("Password");
private final JTextField loginName = new JTextField(15);
private final JPasswordField loginPassword = new JPasswordField();
private final JButton loginButton = new JButton("Login");
private final JButton cancelButton = new JButton("Cancel");
private final JLabel Status = new JLabel(" ");
private String username;
private String password;
private int m_errCounter = 0;
public static final int MAX_LOGIN_ATTEMPTS = 3;
public LoginDialog() {
this(null, true);
}
public LoginDialog(final JFrame parent, boolean modal) {
super(parent, modal);
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(loginlbl);
p3.add(Passwordlbl);
JPanel p4 = new JPanel(new GridLayout(2, 1));
p4.add(loginName);
p4.add(loginPassword);
JPanel p1 = new JPanel();
p1.add(p3);
p1.add(p4);
JPanel p2 = new JPanel();
p2.add(loginButton);
p2.add(cancelButton);
JPanel p5 = new JPanel(new BorderLayout());
p5.add(p2, BorderLayout.CENTER);
p5.add(Status, BorderLayout.NORTH);
Status.setForeground(Color.RED);
Status.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p5, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
loginPassword.enableInputMethods(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UniJava uJava = new UniJava();
try {
username = loginName.getText();
password = new String (loginPassword.getPassword());
if (username == " " && password == " ")
{
Status.setText("Invalid username or password!");
}
else
{
UniSession session = uJava.openSession();
session.setHostName("docdbtst.starcalif.com");
session.setUserName(username);
session.setPassword(password);
session.setAccountPath("/mnt/data1/DD");
session.connect();
parent.setVisible(true);
setVisible(false);
}
} catch (UniSessionException e1) {
if (++m_errCounter > MAX_LOGIN_ATTEMPTS)
{
JOptionPane.showMessageDialog(LoginDialog.this,
"All Login attempts failed!",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
else
{
Status.setText("Invalid Login! Cannot Connect!");
loginName.setText("");
loginPassword.setText("");
}
e1.printStackTrace();
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.dispose();
System.exit(0);
}
});
}
}
System.exit()
everything will be cleaned up
JFrame.dispose() vs System.exit()
logoutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
f.dispose();
// northInformation.removeAll();
// init();
LoginWindow login = new LoginWindow();
}
});