0

I am developing this MainWindows class that extends a SingleFrameApplication abstract class that automatically provide a JFrame

When the main() method is executed it check if the user is logged in, if the user is not logged in, first show another JFrame windows implemented by the LoginFrame class. Here the user insert its username and password and can log in returning to my MainWindows (if the login is ok).

At this time this MainWindows class contains only a LogOut button, and the click event on this button is handled by the actionPerformed method definied inside an AbstractAction inner class. I want that, when this button is clicked, this MainWindows have to be setted as invisible and the LoginFrame windows appears in its place.

The problem is that, from here, seems that is impossible to set this MainWindows as invisible. Eclipse give me an error on this line:

mainFrame.setVisible(false);



package com.test.login;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;

import org.jdesktop.application.SingleFrameApplication;

public class MainWindows extends SingleFrameApplication {

    private static final int FIXED_WIDTH = 880;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 440);


    private final Action actionLogOut = new AbstractAction() {
        {
            putValue(Action.NAME, ("LogOut"));
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("logOutButton clicked !!!");
            String[] args = null;
            launch(LoginFrame.class, args);
            mainFrame.setVisible(false);
        }
    };


    // First execute the LoginFrame class to open the login windows:
    public static void main(String[] args) {
        System.out.println("Inside: MainWindows() ---> main()");

        if(!(args.length > 0 && args[0].equals("loggedIn"))){
            launch(LoginFrame.class, args);
        }

    }

    @Override
    protected void startup() {
        // TODO Auto-generated method stub

        System.out.println("Inside MainWindows ---> startup()");

        JFrame mainFrame = this.getMainFrame();         // main JFrame that represents the Windows
        mainFrame.setTitle("My Appliction MainFrame");

        mainFrame.setPreferredSize(INITAL_SIZE);
        mainFrame.setResizable(false);

        mainFrame.add(new JButton(actionLogOut));
        //mainFrame.add(new JButton("LogOut"));

        show(mainFrame);


    }

}

Someone can help me?

Tnx

Andrea

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • `Eclipse give me an error on this line:` but what kind of error ? please post the stack trace – Sage Nov 12 '13 at 15:29

1 Answers1

1

To access the current frame from your ActionListener use generic code to you don't need to access class variables. Something like:

publc void actionPerformed(ActionEvent e)
{
    JButton button = (JButton)e.getSource();
    Window window = SwingUtilities.windowForComponent(button);
    window.setVisible(false);
}
camickr
  • 321,443
  • 19
  • 166
  • 288