0

I am making a Log in window, so after enter username, password and click login button it will direct you to another frame, which is my GUI that use to insert, retrieve, update, and delete database. However, after click, it displayed nothing. Thank you! Here is my code:

It should redirect to GUI like this:

Login

public class Log extends JFrame {

    public static void main(String[] args) {
        Log frameTabel = new Log();
    }

    JButton blogin = new JButton("Login");
    JPanel panel = new JPanel();
    JTextField txuser = new JTextField(15);
    JPasswordField pass = new JPasswordField(15);

    Log() {
        super("Login Autentification");
        setSize(300, 200);
        setLocation(500, 280);
        panel.setLayout(null);

        txuser.setBounds(70, 30, 150, 20);
        pass.setBounds(70, 65, 150, 20);
        blogin.setBounds(110, 100, 80, 20);

        panel.add(blogin);
        panel.add(txuser);
        panel.add(pass);

        getContentPane().add(panel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        actionlogin();
    }

    public void actionlogin() {
        blogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String puname = txuser.getText();
                String ppaswd = pass.getText();
                if ( puname.equals("test") && ppaswd.equals("12345") ) {
                    CarSearch regFace = new CarSearch();
//                    regFace.setVisible(true);
                    dispose();
                } else {

                    JOptionPane.showMessageDialog(null,
                        "Wrong Password / Username");
                    txuser.setText("");
                    pass.setText("");
                    txuser.requestFocus();
                }

            }
        });
    }

Here is the CarSearch

public class CarSearch {

    public static void main(String[] args) {
        MainPanel logoPanel = new MainPanel();
        JFrame frame = new JFrame("Cars Search");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(logoPanel, BorderLayout.NORTH);
        JTabbedPane tabPage = new JTabbedPane();
//        tabPage.addTab("Log In", new Log());
        tabPage.addTab("Insert Data", new InsertPanel());
        tabPage.addTab("Retrieve Data", new RetrievePanel());
        tabPage.addTab("Update Data", new UpdatePanel());
        tabPage.addTab("Delete Data", new DeletePanel());
        frame.getContentPane().add(tabPage, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}
John
  • 53
  • 8

2 Answers2

1

First of all, I don't encourage the use of multiple frames, instead you should use a CardLayout, how ever. Your CarSearch class doesn't do anything, it only has static main method, which you never call.

I would change the class so it has a constructor which initialises the class and a method you can call so you can control when you want to the window shown

public class CarSearch {

    private MainPanel logoPanel;
    private JFrame frame;

    public CarSearch() {
        logoPanel = new MainPanel();
        frame = new JFrame("Cars Search");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(logoPanel, BorderLayout.NORTH);
        JTabbedPane tabPage = new JTabbedPane();
//        tabPage.addTab("Log In", new Log());
        tabPage.addTab("Insert Data", new InsertPanel());
        tabPage.addTab("Retrieve Data", new RetrievePanel());
        tabPage.addTab("Update Data", new UpdatePanel());
        tabPage.addTab("Delete Data", new DeletePanel());
        frame.getContentPane().add(tabPage, BorderLayout.CENTER);

    }

    public void show() {
        frame.pack();
        frame.setVisible(true);
    }
}

Now, having said that, I'd strongly encourage you to use a CardLayout.

Start by creating a LoginPanel and a CarSearchPanel, then you can add each to the same frame and use the CardLayout to switch between them as needed

For (a slight over the top) example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

If that is what you want to do then in your CarSearch class should be a JFrame and in your main method you should have CarSearch frame = new CarSearch("Cars Search") instead of JFrame frame = new JFrame("Cars Search"). Then introduce a method public void authenticated() which should contain the code for enabling the other tabs. Also your initialisation should have all the tabs other than the login tab disabled.

Now in your method public void actionlogin() in your validation condition if ( puname.equals("test") && ppaswd.equals("12345") ) you should have CarSearch frame = (CarSearch)getRootPane(); and after that you could call frame.authenticated(); which will disable the login tab and enable the other tabs.

Blip
  • 3,061
  • 5
  • 22
  • 50
  • *"CarSearch class should be a JFrame"* - No it doesn't, and in fact is generally discouraged; And rather then using tabs, you could just use a CardLayout tow switch between the login and search views, there by controller what the use can actually do/see – MadProgrammer Apr 17 '15 at 06:21