0

So I have a login class that basically opens a new window if the login credentials are correct. So I call another class if the credentials are right. Problem is, the window comes up but its empty. Here is the window that I am trying to open. Its pretty basic as you can see: (Summary: its just adding some buttons and textfields via grid bag layout and those buttons open up another window)

   import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.InputStream;

    import javax.imageio.ImageIO;
    import javax.swing.*;
    public class Library extends JFrame{
        private static JFrame frame;
        private GridBagConstraints padding;
        private JButton addB;
        private JTextField aB;
        private JButton issueB;
        private JTextField iB;
        private JButton holdB;
        private JTextField hB;
        private JButton renewB;
        private JTextField rB;
        private JButton logout;
        private ImageIcon logo;
        private JMenuBar menubar;
        private JMenu file;
        private JMenuItem exit;





        public Library(){
            frame = new JFrame();
            frame.setLayout(new GridBagLayout());
            padding = new GridBagConstraints();
        }
        //deals with the adding of textfield and label of adding book
        public void addBLabels()
        {
            addB = new JButton("Add Book: ");
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 4;
            padding.weightx = 1;
            padding.gridy = 0;
            padding.weighty = 1;
            frame.add(addB, padding);
            aB = new JTextField(30);
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 5;
            padding.weightx = 1;
            padding.gridy = 0;
            padding.weighty = 1;
            frame.add(aB, padding);
            event1 butt = new event1();
            addB.addActionListener(butt);
        }
        public class event1 implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                    AddBookWindow bookWin = new AddBookWindow(Library.this);
                    bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                    bookWin.setTitle("Add Book");
                    bookWin.setSize(500,200);
                    bookWin.setLocation(300,300);
                    bookWin.setVisible(true);
                    bookWin.setResizable(false);

            }
        }




        //deals with issue book labels and textfield
        public void issueBLabels(){
            issueB = new JButton("Issue Book: ");
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 4;
            padding.weightx= 1;
            padding.gridy = 2;
            padding.weighty = 1;
            frame.add(issueB, padding);
            iB = new JTextField(30);
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 5;
            padding.weightx = 1;
            padding.gridy = 2;
            padding.weighty = 1;
            frame.add(iB, padding);
            event2 iss = new event2();
            issueB.addActionListener(iss);
        }
        public class event2 implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                    AddBookWindow bookWin = new AddBookWindow(Library.this);
                    bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                    bookWin.setTitle("Issue Book");
                    bookWin.setSize(500,200);
                    bookWin.setLocation(300,300);
                    bookWin.setVisible(true);
                    bookWin.setResizable(false);

            }
        }

        //deals with holdbook labels and textfield
        public void holdBookLabels(){
            holdB = new JButton("Hold Book: ");
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 4;
            padding.weightx = 1;
            padding.gridy = 4;
            padding.weighty = 1;
            frame.add(holdB, padding);
            hB = new JTextField(30);
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 5;
            padding.weightx = 1;
            padding.gridy = 4;
            padding.weighty = 1;
            frame.add(hB, padding);
            event3 hold = new event3();
            holdB.addActionListener(hold);
        }
        public class event3 implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                    AddBookWindow bookWin = new AddBookWindow(Library.this);
                    bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                    bookWin.setTitle("Hold Book");
                    bookWin.setSize(500,200);
                    bookWin.setLocation(300,300);
                    bookWin.setVisible(true);
                    bookWin.setResizable(false);

            }
        }

        //deals with the renewbook labels and textfield
        public void renewBookLabels(){
            renewB = new JButton("Renew Book: ");
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 4;
            padding.weightx = 1;
            padding.gridy = 6;
            padding.weighty = 1;
            frame.add(renewB, padding);
            rB = new JTextField(30);
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 5;
            padding.weightx = 1;
            padding.gridy = 6;
            padding.weighty = 1;
            frame.add(rB, padding);
            event4 renew = new event4();
            renewB.addActionListener(renew);
        }
        public class event4 implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                    AddBookWindow bookWin = new AddBookWindow(Library.this);
                    bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                    bookWin.setTitle("Renew Book");
                    bookWin.setSize(500,200);
                    bookWin.setLocation(300,300);
                    bookWin.setVisible(true);
                    bookWin.setResizable(false);

            }
        }

        //deals with adding the logout button
        public void logOutButton(){
            logout = new JButton("Logout");
            padding.gridx = 5;
            padding.weightx = 1;
            padding.gridy = 10;
            padding.weighty = 1;
            padding.gridwidth = 1;
            frame.add(logout, padding);
        }

        //deals with adding the image
        public void addImage() throws IOException{
            InputStream imageStream = this.getClass().getResourceAsStream("0521-1005-0822-0024_brunette_girl_smiling_and_holding_a_stack_books.jpg");
            BufferedImage image = ImageIO.read(imageStream);
            JLabel picLabel = new JLabel(new ImageIcon(image));
            padding.gridheight = 10;
            padding.fill = GridBagConstraints.VERTICAL;
            padding.gridx = 0;
            padding.weightx = 1;
            padding.gridy = 0;
            padding.weighty = 1;
            frame.add(picLabel, padding);
        }

        private void menuBar(){

            menubar = new JMenuBar();
            padding.fill = GridBagConstraints.HORIZONTAL;
            padding.gridx = 0;
            padding.weightx = 1;
            padding.gridy = 0;
            padding.weighty = 1;
            frame.add(menubar, padding);

            file = new JMenu("File");
            menubar.add(file);

            exit = new JMenuItem("exit");
            file.add(exit);
        }


        public static void main(String args[]) throws IOException{
            Library gui = new Library();
            //not working right now gui.menuBar();
            gui.addBLabels();
            gui.issueBLabels();
            gui.holdBookLabels();
            gui.renewBookLabels();
            gui.logOutButton();
            gui.addImage();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("La Biblioteca");
            frame.setSize(880,430);
            frame.setVisible(true);     
            frame.setResizable(false);
        }
    }

Basically in my Login class, my if statement says:

if(login credentials are correct)
 Library gui = new Library();
 gui.setSize(880,430);
 gui.setVisible(true);
 gui.addBLabels();
 gui.issueBLabels();
 gui.holdBookLabels();
 gui.renewBookLabels();
 gui.logOutButton();
 gui.addImage();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

1 Answers1

2

Start by look at The Use of Multiple JFrames, Good/Bad Practice?.

The problem is Library extends from JFrame, but you have ANOTHER JFrame as a (static) instance field...

public class Library extends JFrame{
    private static JFrame frame;

When you construct the Library class, you are using the frame field, but when you set it's properties from are using the Library class, you have two different frames.

The solution, don't extend from JFrame or any other top level container, ever, you are not adding any meaningful functionality to it and are limiting the re-use of your components.

Instead, consider using a CardLayout, see How to Use CardLayout for more details...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ok Thanks! I will check it out. Thing is we never covered this in class. – user3754524 Nov 23 '14 at 05:34
  • @MadProgammer so I looked up card layout and I understand the concept. I am wondering how this would apply to my code. Like for example if the login credentials are right, would I do something like show(mainContainer,"the second window")? – user3754524 Nov 24 '14 at 23:21
  • Not "second window" per say, but "second view" where view is somekind of `Component` – MadProgrammer Nov 25 '14 at 01:31
  • I asked my professor and he said we can use a GUI editor so I am probably just going to use that, much more easier to use. – user3754524 Nov 25 '14 at 17:49