0

im making a program and i've no idea how to make this thing work: I have a mainFrame :

package itneizapenoitseg;

public class mainFrame extends JFrame {

    private dbconnection database = new dbconnection(); // DB connetion data (login , passw ..)
    private JDesktopPane desktop = new JDesktopPane(); // Need this to make Frame inside Frame

    public mainFrame() {
        super("");
        setLayout(null);
        setSize(850, 700);
        setLocation(500, 280);
        Login login = new Login(database);// I want to perform a login
        desktop.add(login);
        try {
            login.setSelected(true);
        }
        catch (Exception e) {
            // TODO: handle exception
        }
        setContentPane(desktop);
        /////////////////////////////////////////

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String [] args) {
        new mainFrame();
    }
}

The login class :

package itneizapenoitseg;

import... //

public class Login extends JInternalFrame {

    // User and password fields
    JTextField usernameForm = new JTextField(15);
    JPasswordField passwordForm = new JPasswordField(15);
    JButton login = new JButton("Login");

    public Login(dbconnection database) {

        super("Login", true, true, true, true);

        setSize(300, 200);

        // The login panel
        JPanel panel = new JPanel();
        panel.setLayout(null);
        JLabel username = new JLabel("username :");
        JLabel password = new JLabel("password :");

        // Position
        username.setBounds(70, 10, 80, 11);
        password.setBounds(70, 55, 80, 17);
        usernameForm.setBounds(70, 30, 150, 20);
        passwordForm.setBounds(70, 75, 150, 20);
        login.setBounds(105, 100, 80, 20);

        // Addings elements to display panel
        panel.add(usernameForm);
        panel.add(passwordForm);
        panel.add(login);
        panel.add(password);
        panel.add(username);
        getContentPane().add(panel);

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// Doens't work
        setVisible(true);
        actionLogin(database);
    }

    // When pressing Login button...

    private void actionLogin(dbconnection database) {
        login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //TODO Have to change to MySql query
                String usname = usernameForm.getText();
                String passw = new String(passwordForm.getPassword());

                // Checking credencial
                if(usname.equals("test") && passw.equals("1234")){
                    // Here i want to call a mainFrame funcion (createGUI)
                    dispose();Destroy this panel
                } else{
                    JOptionPane.showMessageDialog(null, "Username or password wrong!");
                    usernameForm.setText("");
                    passwordForm.setText("");
                    usernameForm.requestFocus();
                }
            }
        });

    }
}  

I want that when somebody successfully login, a mainFrame's function createGui is called or a way to notify the mainFrame. My intention is to make the mainFrame empty until sombody successfully login and next display it's content.

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
HaZe
  • 45
  • 7

1 Answers1

0

Very similar to this SO question.

You can call ((mainFrame) SwingUtilities.getWindowAncestor((Component)(e.getSource()))).createGUI();

Hope that helps.

EDIT: An example I've just tested:

private void setHandlers() {
    jButton2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ((mainFrame) SwingUtilities.getWindowAncestor((Component)(e.getSource()))).showAlert();
        }
    });
}
public void showAlert() {
    JOptionPane.showConfirmDialog(this, "Confirming !!");
}

There is another option (that is less frequently used - atleast by me):

((mainFrame) SwingUtilities.getAncestorOfClass(mainFrame.class, ((Component)(e.getSource())))).createGUI();

RE-EDIT: You can also try:

((mainFrame) ((JComponent) (e.getSource())).getTopLevelAncestor()).createGUI();
Community
  • 1
  • 1
Gurusharan S
  • 365
  • 1
  • 7
  • It doens't work cause im inside an actionListener :( – HaZe Oct 02 '14 at 15:04
  • Ok - that's because you've used an anonymous class - that's bad, but not end of world! Use `(Component)e.getSource()` instead of `this` and check if it works. – Gurusharan S Oct 02 '14 at 15:20
  • Editted the answer as well :) – Gurusharan S Oct 02 '14 at 15:24
  • What you mean with anonymous class ? however now i can execute but i get nullPointer exception; – HaZe Oct 02 '14 at 15:36
  • Please check my editted answer - my comment has a problem with brackets. To know more about anonymous classes please see the [docs](http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html). – Gurusharan S Oct 02 '14 at 15:45
  • I've checked , i can execute but i got nullPointerExeption any idea why ? (thanks for docs link) – HaZe Oct 02 '14 at 16:04
  • Really? That's odd. I've added an example I just tested and two more ways of doing the stuff. Seems like the problem could be because the button's in a panel inside a frame that is inside another panel that is the content pane of the root frame. Though I've not tried this much depth, I'd say it should work. One of the three methods ought to work. – Gurusharan S Oct 03 '14 at 17:22