1

I would like my original window to close when someone enters the password and a new one to pop up, or if you have a better recommendation please tell me. Here is my code,

The main class,

package notebook;

import java.awt.EventQueue;

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;


public class mainPage extends JDialog  {
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainPage frame = new mainPage();
                    frame.setVisible(true);
                    frame.setResizable(false);
                    Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
                    frame.setIconImage(icon);
                    frame.setTitle("Notebook");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws IOException 
     */
    public mainPage() throws IOException {
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 560, 390);      
        JLabel contentPane = new JLabel(
                new ImageIcon(
                        ImageIO.read(new File(
                                "C:\\Users\\Gianmarco\\workspace\\notebook\\src\\notebook\\cool_cat.jpg"))));
        contentPane.setBorder(new CompoundBorder());
        setContentPane(contentPane);
        contentPane.setLayout(null);


        JLabel lblEnterPassword = new JLabel(" Enter Password");
        lblEnterPassword.setForeground(Color.LIGHT_GRAY);
        lblEnterPassword.setBackground(Color.DARK_GRAY);
        lblEnterPassword.setOpaque(true);
        lblEnterPassword.setBounds(230, 60, 100, 15);
        contentPane.add(lblEnterPassword);

        security sInfo = new security();


        textField = new JPasswordField(10);
        nbTab notebook = new nbTab();

        Action action = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String textFieldValue = textField.getText();
                if (sInfo.checkPassword(textFieldValue)){ 
                    System.out.println("working");
                    notebook.setVisible(true);
                    //dispose();
                }
            }
        };

        JPanel panel = new JPanel(); 
        textField.setBounds(230, 85, 100, 15);
        contentPane.add(textField);
        contentPane.add(panel);
        textField.setColumns(10);
        textField.addActionListener(action);


    }
}

The password class,

package notebook;

public class security {

    private String password = "kitten";

    protected boolean checkPassword(String x){
        if(x.length()<15 && x.equals(password)) return true;
        return false;
    }

}

The JTabbedPane class,

package notebook;

import javax.swing.JTabbedPane;
import javax.swing.JEditorPane;
import javax.swing.JList;
import javax.swing.JButton;

public class nbTab<E> extends JTabbedPane {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Create the panel.
     */

    public nbTab() {

        JEditorPane editorPane = new JEditorPane();
        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(480, 345, 40, 30);
        editorPane.add(btnNewButton);
        editorPane.setBounds(80, 45, 400, 300);

        addTab("New tab", null, editorPane, null);


        JList<? extends E> list = new JList();
        addTab("New tab", null, list, null);



    }

}

In my main class, on lines 76 - 82 (where the action event listner is located) I would like to have my current window close and a new window of notebook to open. I used dispose() to close the password window. Then I try to open the JTabbedPane with setVisible(), setSelectedComponent, and setSelectedIndex however I am either using them incorrectly or there must be some better way to do this because it is not working. Any advice is appreciated guys thanks for all help.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1st of all, don't use null layout. Instead use a proper [Layout Manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) (Or combinations of them). For more about this, check [Null Layout is Evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout in Swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Frakcool Dec 14 '15 at 19:58
  • 2
    Use a CardLayout to switch views or a dialog of some kind to show the login view – MadProgrammer Dec 14 '15 at 19:58
  • Avoid using null layouts, they are more trouble then they are worth – MadProgrammer Dec 14 '15 at 19:59
  • You could have **only 1** JFrame, use a [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) (Recommended), and change views through them. Or have your login window as a JOptionPane but still having only 1 JFrame. For more read [The use of multiple JFrames, Good / Bad Practice](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) – Frakcool Dec 14 '15 at 20:01
  • Thanks guys, I will have to do some reading on null layouts and layout managers. – Jon_the_developer Dec 14 '15 at 20:50
  • Also, you tabbed pane should be added to something that is displayable (like a window) – MadProgrammer Dec 14 '15 at 20:51

1 Answers1

0

As already suggested by MadProgrammer and Frakcool, the CardLayout layout manager is an interesting option in your case. A nice introduction to several Layout Managers for Swing is available here: A Visual Guide to Layout Managers.

You can use the code below to get an idea of how it could work. I have made a few modifications to your main application class:

  • The main class now extends from JFrame (instead of JDialog).
  • A few more panels and layout managers are used.
  • In Java class names usually start with a capital letter, so I have renamed your classes to MainPage, NotebookTab, and Security.

Here is the modified MainPage class:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class MainPage extends JFrame {
    private static final String LOGIN_PANEL_ID = "Login panel";
    private static final String NOTEBOOK_ID = "Notebook tabbed pane";

    private JPanel mainPanel;
    private CardLayout cardLayout;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainPage frame = new MainPage();
                    frame.setResizable(false);
                    Image icon = new BufferedImage(1, 1,
                                                   BufferedImage.TYPE_INT_ARGB_PRE);
                    frame.setIconImage(icon);
                    frame.setTitle("Notebook");
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws IOException
     */
    public MainPage() throws IOException {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 560, 390);

        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);

        mainPanel.add(createLoginPanel(), LOGIN_PANEL_ID);
        mainPanel.add(new NotebookTab(), NOTEBOOK_ID);

        getContentPane().add(mainPanel);
    }

    private JPanel createLoginPanel() throws IOException {
        JPanel loginPanel = new JPanel(new BorderLayout());

        JPanel passwordPanel = new JPanel();
        passwordPanel.setLayout(new BoxLayout(passwordPanel, BoxLayout.PAGE_AXIS));

        JLabel lblEnterPassword = new JLabel("Enter Password");
        lblEnterPassword.setForeground(Color.LIGHT_GRAY);
        lblEnterPassword.setBackground(Color.DARK_GRAY);
        lblEnterPassword.setOpaque(true);
        lblEnterPassword.setHorizontalAlignment(SwingConstants.CENTER);
        lblEnterPassword.setMaximumSize(new Dimension(100, 16));
        lblEnterPassword.setAlignmentX(Component.CENTER_ALIGNMENT);

        JTextField textField = new JPasswordField(10);
        textField.setMaximumSize(new Dimension(100, 16));
        textField.setAlignmentX(Component.CENTER_ALIGNMENT);

        passwordPanel.add(Box.createRigidArea(new Dimension(0, 42)));
        passwordPanel.add(lblEnterPassword);
        passwordPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        passwordPanel.add(textField);

        loginPanel.add(passwordPanel, BorderLayout.NORTH);

        Action loginAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (new Security().checkPassword(textField.getText())) {
                    System.out.println("working");
                    cardLayout.show(mainPanel, NOTEBOOK_ID);
                }
            }
        };

        textField.addActionListener(loginAction);

        String imagePath = "C:\\Users\\Gianmarco\\workspace\\" +
                           "notebook\\src\\notebook\\cool_cat.jpg";
        BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
        JLabel imageLabel = new JLabel(new ImageIcon(bufferedImage));

        loginPanel.add(imageLabel, BorderLayout.CENTER);

        return loginPanel;
    }
}
Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
  • Hey thanks a bunch. I actually started working on it and made another notepad project, but I will definitely take a look at this too. Thanks a lot for your help answer. – Jon_the_developer Dec 17 '15 at 02:54