0

I have a pretty big setup here and its still very much a WIP. Right now I want to get my GUI displaying properly, and switching between panels at the click of a button. In my GUI() method, I set up my various panels with group layout. At the end of the class, you will notice methods i want to use to set the various panels visible or invisible. LoginP is the panel i want to see when i run the code, so i call the corresponding method at the end of my GUI() method, but for some strange reason, when I run it is as if there are no panels at all, just a blank JFrame. I added a System.out.println(); just after I call the method to set LoginP to visible (at the end of the GUI() method), but alas, that line is not printed. I'm sure the answer is staring me right in the face, but I'm just not able to see it.

Edit: For those who feel like playing a bit, here are all the source files: http://goo.gl/KjW8cH

    //Individual Imports
/*
import java.awt.GroupLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class GUI extends JFrame
{
/*#################################################################################################################################################################
 *#####################################################################################################################################################
 *###############################################################################################################################################################*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Login Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel loginP;
    private JLabel titleL;
    private JLabel instructL;
    private JLabel usernameL;
    private JLabel passwordL;
    private JLabel loginL;
    private JButton studentB;
    private JButton lecturerB;
    private JTextField usernameTF;
    private JTextField passwordTF;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Add Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ private JPanel addQP;
    private JPanel addQP;
    private JLabel instructionsL;
    private JLabel txtL;
    private JLabel infoL;
    private JButton appendB;
    private JButton overwriteB;
    private JTextField pathTF;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel questionP;
    private JLabel qNumL;
    private JLabel groupL;
    private JLabel questionL;
    private JLabel opt1L;
    private JLabel opt2L;
    private JLabel opt3L;
    private JLabel opt4L;
    private JButton opt1B;
    private JButton opt2B;
    private JButton opt3B;
    private JButton opt4B;
    String qNumber = "3";
    String group = "JDBC";
    String question = "This is the question";
    String opt1 = "This is option 1";
    String opt2 = "This is option 2";
    String opt3 = "This is option 3";
    String opt4 = "This is option 4";
    String a = "a.";
    String b = "b.";
    String c = "c.";
    String d = "d.";
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Viewer Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel viewerP;
    private JLabel aL;
    private JLabel bL;
    private JLabel cL;
    private JLabel dL;
    private JButton nextB;
    private JButton closeB;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel selectQP;
    private JLabel instruct1L;
    private JLabel askL;
    private JCheckBox checkBox1;
    private JCheckBox checkBox2;
    private JCheckBox checkBox3;
    private JCheckBox checkBox4;
    private JButton viewB;
    private JButton startB;
    private JTextField numOfQTF;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mark Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel markP;
    private JLabel percentageL;
    private JLabel outOfL;
    private JButton backB;
    private JButton logOutB;

    Connection con;
    Statement stmt;
    ResultSet rs;

/*#################################################################################################################################################################
 *#########################################################################Panels##################################################################################
 *###############################################################################################################################################################*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Login Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public GUI()
    {
        super("Tester");
        setLayout(new FlowLayout());

        loginP = new JPanel();
        add(loginP);
        GroupLayout loginPLayout = new GroupLayout(loginP);
        loginP.setLayout(loginPLayout);
        loginPLayout.setAutoCreateGaps(true);
        loginPLayout.setAutoCreateContainerGaps(true);

        Font font = new Font("Freestyle Script", Font.PLAIN,80);

        titleL = new JLabel("Tester");
        titleL.setFont(font);
        instructL = new JLabel("Please eneter your name and password and select your login type to login.");
        usernameL = new JLabel("Username:");
        passwordL = new JLabel("Password:");
        loginL = new JLabel("Login as");
        studentB = new JButton("Student");
        lecturerB = new JButton("Lecturer");
        usernameTF = new JTextField("", 100);
        passwordTF = new JTextField("", 100);

        studentB.addActionListener(new studentH());
        lecturerB.addActionListener(new lecturerH());

        loginPLayout.setHorizontalGroup(loginPLayout.createSequentialGroup()
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(usernameL)
                .addComponent(passwordL))
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                .addComponent(titleL)
                .addComponent(instructL)
                .addComponent(usernameTF)
                .addComponent(passwordTF)
                .addComponent(loginL)
                .addGroup(loginPLayout.createSequentialGroup()
                    .addComponent(studentB)
                    .addComponent(lecturerB)))
        );

        loginPLayout.setVerticalGroup(loginPLayout.createSequentialGroup()
            .addComponent(titleL)
            .addComponent(instructL)
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(usernameL)
                .addComponent(usernameTF))
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(passwordL)
                .addComponent(passwordTF))
            .addComponent(loginL)
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(studentB)
                .addComponent(lecturerB))
       );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        selectQP = new JPanel();
        add(selectQP);
        GroupLayout selectQPLayout = new GroupLayout(selectQP);
        selectQP.setLayout(selectQPLayout);
        selectQPLayout.setAutoCreateGaps(true);
        selectQPLayout.setAutoCreateContainerGaps(true);

        String cat1 = "Collections";
        String cat2 = "Multithreading";
        String cat3 = "Networking";
        String cat4 = "JDBC";

        instruct1L = new JLabel("Select the groups of questions you would like to be tested on");
        askL = new JLabel("How many questions would you like?");
        checkBox1 = new JCheckBox(cat1);
        checkBox2 = new JCheckBox(cat2);
        checkBox3 = new JCheckBox(cat3);
        checkBox4 = new JCheckBox(cat4);
        viewB = new JButton("View Questions");
        startB = new JButton("Start Test");
        numOfQTF = new JTextField("", 100);

        viewB.addActionListener(new viewH());
        startB.addActionListener(new startH());

        selectQPLayout.setHorizontalGroup(selectQPLayout.createParallelGroup()
            .addComponent(instruct1L)
            .addComponent(checkBox1)
            .addComponent(checkBox2)
            .addComponent(checkBox3)
            .addComponent(checkBox4)
            .addComponent(askL)
            .addComponent(numOfQTF)
            .addGroup(selectQPLayout.createSequentialGroup()
                .addComponent(viewB)
                .addComponent(startB))
        );

        selectQPLayout.setVerticalGroup(selectQPLayout.createSequentialGroup()
            .addComponent(instruct1L)
            .addComponent(checkBox1)
            .addComponent(checkBox2)
            .addComponent(checkBox3)
            .addComponent(checkBox4)
            .addComponent(askL)
            .addComponent(numOfQTF)
            .addGroup(selectQPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(viewB)
                .addComponent(startB))
        );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Add Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        addQP = new JPanel();
        add(addQP);
        GroupLayout addQPLayout = new GroupLayout(addQP);
        addQP.setLayout(addQPLayout);
        addQPLayout.setAutoCreateGaps(true);
        addQPLayout.setAutoCreateContainerGaps(true);

        instructionsL = new JLabel("Please enter the name of the file:");
        txtL = new JLabel(".txt");
        infoL = new JLabel("You will be logged out after the questions have been added.");
        appendB = new JButton("Append Questions");
        overwriteB = new JButton("Overwrite Questions");
        pathTF = new JTextField("", 100);

        appendB.addActionListener(new appendH());
        overwriteB.addActionListener(new overwriteH());

        addQPLayout.setHorizontalGroup(addQPLayout.createSequentialGroup()
            .addGroup(addQPLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                .addComponent(instructionsL)
                .addGroup(addQPLayout.createSequentialGroup()
                    .addComponent(pathTF)
                    .addComponent(txtL))
                .addComponent(infoL)
                .addGroup(addQPLayout.createSequentialGroup()
                    .addComponent(appendB)
                    .addComponent(overwriteB)))
        );

        addQPLayout.setVerticalGroup(addQPLayout.createSequentialGroup()
            .addComponent(instructionsL)
            .addGroup(addQPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(pathTF)
                .addComponent(txtL))
            .addComponent(infoL)
            .addGroup(addQPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(appendB)
                .addComponent(overwriteB))
        );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        qNumL = new JLabel("Question Number: " + qNumber);
        groupL = new JLabel("Group: " + group);
        questionL = new JLabel(question);
        opt1L = new JLabel(opt1);
        opt2L = new JLabel(opt2);
        opt3L = new JLabel(opt3);
        opt4L = new JLabel(opt4);
        opt1B = new JButton("A");
        opt2B = new JButton("B");
        opt3B = new JButton("C");
        opt4B = new JButton("D");

        questionP = new JPanel();
        add(questionP);
        GroupLayout questionPLayout = new GroupLayout(questionP);
        questionP.setLayout(questionPLayout);
        questionPLayout.setAutoCreateGaps(true);
        questionPLayout.setAutoCreateContainerGaps(true);

        optH handler = new optH();
        opt1B.addActionListener(handler);
        //opt2B.addActionListener(handler);
        //opt3B.addActionListener(handler);
        //opt4B.addActionListener(handler);

        questionPLayout.setHorizontalGroup(questionPLayout.createSequentialGroup()
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(questionL)
                .addComponent(opt1B)
                .addComponent(opt2B)
                .addComponent(opt3B)
                .addComponent(opt4B))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(opt1L)
                .addComponent(opt2L)
                .addComponent(opt3L)
                .addComponent(opt4L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                .addComponent(groupL))
        );

        questionPLayout.setVerticalGroup(questionPLayout.createSequentialGroup()
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(groupL))
            .addComponent(questionL)
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt1B)
                .addComponent(opt1L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt2B)
                .addComponent(opt2L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt3B)
                .addComponent(opt3L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt4B)
                .addComponent(opt4L))
       );
       showLoginP();
       System.out.println("Im here");

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Viewer Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        qNumL = new JLabel("Question Number: " + qNumber);
        groupL = new JLabel("Group: " + group);
        questionL = new JLabel(question);
        aL = new JLabel("A");
        bL = new JLabel("B");
        cL = new JLabel("C");
        dL = new JLabel("D");
        opt1L = new JLabel(opt1);
        opt2L = new JLabel(opt2);
        opt3L = new JLabel(opt3);
        opt4L = new JLabel(opt4);
        nextB = new JButton("Next");
        closeB = new JButton("Close");

        viewerP = new JPanel();
        add(viewerP);
        GroupLayout viewerPLayout = new GroupLayout(viewerP);
        viewerP.setLayout(viewerPLayout);
        viewerPLayout.setAutoCreateGaps(true);
        viewerPLayout.setAutoCreateContainerGaps(true);

        viewerPLayout.setHorizontalGroup(viewerPLayout.createSequentialGroup()
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(questionL)
                .addComponent(aL)
                .addComponent(bL)
                .addComponent(cL)
                .addComponent(dL))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(opt1L)
                .addComponent(opt2L)
                .addComponent(opt3L)
                .addComponent(opt4L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                .addComponent(groupL)
                .addGroup(viewerPLayout.createSequentialGroup()
                    .addComponent(nextB)
                    .addComponent(closeB)))
        );

        viewerPLayout.setVerticalGroup(viewerPLayout.createSequentialGroup()
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(groupL))
            .addComponent(questionL)
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(aL)
                .addComponent(opt1L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(bL)
                .addComponent(opt2L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(cL)
                .addComponent(opt3L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(dL)
                .addComponent(opt4L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(nextB)
                .addComponent(closeB))
       );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mark Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        markP = new JPanel();
        add(markP);
        GroupLayout markPLayout = new GroupLayout(markP);
        markP.setLayout(markPLayout);
        markPLayout.setAutoCreateGaps(true);
        markPLayout.setAutoCreateContainerGaps(true);

        int percentage = 90;
        int correct = 9;
        int total = 10;

        percentageL = new JLabel("You scored: " + percentage + "%");
        outOfL = new JLabel("You have " + correct + " corret answers out of a possible " + total);
        backB = new JButton("Back to Group Selection");
        logOutB = new JButton("Log Out");

        backB.addActionListener(new backH());
        logOutB.addActionListener(new logOutH());

        markPLayout.setHorizontalGroup(markPLayout.createParallelGroup()
            .addComponent(percentageL)
            .addComponent(outOfL)
            .addGroup(markPLayout.createSequentialGroup()
                .addComponent(backB)
                .addComponent(logOutB))
        );

        markPLayout.setVerticalGroup(markPLayout.createSequentialGroup()
            .addComponent(percentageL)
            .addComponent(outOfL)
            .addGroup(markPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(backB)
                .addComponent(logOutB))
        );
    }
    showLoginP();
        System.out.println("Im here");

/*#################################################################################################################################################################
 *#####################################################################Action Listeners############################################################################
 *###############################################################################################################################################################*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Login Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class studentH implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            Student stu = new Student();

            String username = usernameTF.getText();
            String password = passwordTF.getText();
            boolean correct = stu.Student(username, password);
            if(correct == true)
            {
                showSelectQP();
            }
            else
            {
                JOptionPane.showMessageDialog(null, "You have entered incorect details. Please try again", "Incorrect Details", JOptionPane.ERROR_MESSAGE);
                usernameTF.setText("");
                passwordTF.setText("");
            }
        }
   }

   private class lecturerH implements ActionListener
   {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            Lecturer lect = new Lecturer();

            String username = usernameTF.getText();
            String password = passwordTF.getText();
            boolean correct = lect.Lecturer(username, password);
            if(correct == true)
            {
                showAddQP();
            }
            else
            {
                JOptionPane.showMessageDialog(null, "You have entered incorect details. Please try again", "Incorrect Details", JOptionPane.ERROR_MESSAGE);
                usernameTF.setText("");
                passwordTF.setText("");
            }
        }
   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Add Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class appendH implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            //Append appen = new Append();
            String path = pathTF.getText();
            //appen.Append(path);
        }
   }

   private class overwriteH implements ActionListener
   {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            Overwrite over = new Overwrite();

        }
   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class optH implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent event )
        {

        }
    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class viewH implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent event )
        {

        }
   }

   private class startH implements ActionListener
   {
        @Override
        public void actionPerformed( ActionEvent event )
        {

        }
   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mark Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class backH implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent event )
        {

        }
   }

   private class logOutH implements ActionListener
   {
        @Override
        public void actionPerformed( ActionEvent event )
        {

        }
   }

    public void showLoginP()
    {
        addQP.setVisible(false);
        questionP.setVisible(false);
        viewerP.setVisible(false);
        markP.setVisible(false);
        selectQP.setVisible(false);
        loginP.setVisible(true);
        revalidate();
        repaint();
    }

    public void showAddQP()
    {
        questionP.setVisible(false);
        viewerP.setVisible(false);
        markP.setVisible(false);
        selectQP.setVisible(false);
        loginP.setVisible(false);
        addQP.setVisible(true);
    }

    public void showQuestionP()
    {
        addQP.setVisible(false);
        viewerP.setVisible(false);
        markP.setVisible(false);
        selectQP.setVisible(false);
        loginP.setVisible(false);
        questionP.setVisible(true);
    }

    public void showViewerP()
    {
        addQP.setVisible(false);
        questionP.setVisible(false);
        markP.setVisible(false);
        selectQP.setVisible(false);
        loginP.setVisible(false);
        viewerP.setVisible(true);
    }

    public void showMarkP()
    {
        addQP.setVisible(false);
        questionP.setVisible(false);
        viewerP.setVisible(false);
        selectQP.setVisible(false);
        loginP.setVisible(false);
        markP.setVisible(true);
    }

    public void showSelectQP()
    {
        addQP.setVisible(false);
        //questionP.setVisible(false);
        viewerP.setVisible(false);
        markP.setVisible(false);
        loginP.setVisible(false);
        selectQP.setVisible(true);
    }
}
vanatic
  • 3
  • 3
  • Remove everything except for the desired first effect. Pare your code down until you have just the bare bones. Get that working, then build from there. There's too much going on here for someone else to pick it up and easily tell you what's wrong, so it's unlikely you'll get a lot of help. – sage88 Nov 19 '14 at 21:37
  • Also if you want showLoginP() to actually run, it has to be inside the constructor, not floating out in no man's land. Same thing with your println statement. – sage88 Nov 19 '14 at 21:38
  • Where are the `Student`, `Lecturer`, and `Overwrite` classes defined? (If that just opens up a bigger can of worms, never mind.) BTW, I don't see `pack` anywhere. – DSlomer64 Nov 19 '14 at 21:39
  • And where is your main method? – sage88 Nov 19 '14 at 21:39
  • Yeah, Its been my last resort after posting here. It was working earlier today, and then I ran it once and it decided to be difficult. Here is a pastebin, much easier to read. http://pastebin.com/ADygqGPY – vanatic Nov 19 '14 at 21:40
  • No matter where i put showLoginP() or the println (even right after super) none of the printlns print. All the other classes exist in the same dir, but they aren't implemented as of yet, I just want to get to the point where i can see my loginP and after clicking studentB, I want the loginP to become invisible and selectQP to become visible. main method is here: http://pastebin.com/2Ly9Wxbi – vanatic Nov 19 '14 at 21:46
  • @DSlomer64 tell me more of this pack(). Where should I put it? I added it right at the end of the GUI() method before i call showLoginP, no luck. – vanatic Nov 19 '14 at 21:52
  • updated GUI class: http://pastebin.com/gELG7uLh – vanatic Nov 19 '14 at 21:54
  • Since it sounds like this is for an assignment based on your other comments, are you allowed to use CardLayout? Because that would be my first choice given what you're trying to do here. – sage88 Nov 19 '14 at 22:24
  • 1
    `GroupLayout` was designed for use by GUI form editors, and isn't particularly user friendly. You should also consider using a `CardLayout` ([How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)) to switch between different views – MadProgrammer Nov 19 '14 at 23:04
  • 1
    Since you didn't have a `main` method and I didn't find `pack` anywhere, I was reaching when I mentioned `pack`. Since @sage88 provided a `main` method and included `gui.setsize...`, that causes a frame to be displayed, which `pack` can also do. So forget `pack`. Its omission is not your problem. However, if you replace `gui.setsize...` with `gui.pack()`, you may find it preferable, depending on how much of your own code vs. sage88's you wind up using. You might go [here](https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html) for more info. – DSlomer64 Nov 20 '14 at 11:05
  • @DSlomer64 There was actually a main method in a separate class that vanatic linked above. I just copied it out and made it properly use invokeLater and put it in the same class. Otherwise I left it unchanged. My inclination is that using just pack is not going to have an effect vanatic wants given what I've seen from the layout, however, it is useful to know about and should be used preferentially over setSize when it makes sense. – sage88 Nov 20 '14 at 11:13

1 Answers1

2

This won't answer your question, but there's so much going on here I'm going to take it one part at a time. First of all, don't put the main method in another class, there's really no reason to. Add this to your existing GUI class and delete the other class:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            GUI gui = new GUI();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(600, 400);
            gui.setResizable(true);
            gui.setVisible(true);
        }
    });
}

I suggest doing so just above your constructor so it's easy to find.

EDIT: Here's a version that uses cardlayout. You're welcome.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class GUI extends JFrame {
    JComboBox<String> comboBox;

    private JPanel cardPanel;

/*#################################################################################################################################################################
 *########################################################################Constructors#############################################################################
 *###############################################################################################################################################################*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Login Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel loginP;
    private JLabel titleL;
    private JLabel instructL;
    private JLabel usernameL;
    private JLabel passwordL;
    private JLabel loginL;
    private JButton studentB;
    private JButton lecturerB;
    private JTextField usernameTF;
    private JTextField passwordTF;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Add Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     private JPanel addQP;
    private JPanel addQP;
    private JLabel instructionsL;
    private JLabel txtL;
    private JLabel infoL;
    private JButton appendB;
    private JButton overwriteB;
    private JTextField pathTF;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel questionP;
    private JLabel qNumL;
    private JLabel groupL;
    private JLabel questionL;
    private JLabel opt1L;
    private JLabel opt2L;
    private JLabel opt3L;
    private JLabel opt4L;
    private JButton opt1B;
    private JButton opt2B;
    private JButton opt3B;
    private JButton opt4B;
    String qNumber = "3";
    String group = "JDBC";
    String question = "This is the question";
    String opt1 = "This is option 1";
    String opt2 = "This is option 2";
    String opt3 = "This is option 3";
    String opt4 = "This is option 4";
    String a = "a.";
    String b = "b.";
    String c = "c.";
    String d = "d.";
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Viewer Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel viewerP;
    private JLabel aL;
    private JLabel bL;
    private JLabel cL;
    private JLabel dL;
    private JButton nextB;
    private JButton closeB;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel selectQP;
    private JLabel instruct1L;
    private JLabel askL;
    private JCheckBox checkBox1;
    private JCheckBox checkBox2;
    private JCheckBox checkBox3;
    private JCheckBox checkBox4;
    private JButton viewB;
    private JButton startB;
    private JTextField numOfQTF;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mark Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private JPanel markP;
    private JLabel percentageL;
    private JLabel outOfL;
    private JButton backB;
    private JButton logOutB;

    Connection con;
    Statement stmt;
    ResultSet rs;

/*#################################################################################################################################################################
 *#########################################################################Panels##################################################################################
 *###############################################################################################################################################################*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Login Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUI gui = new GUI();
                gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                gui.setSize(600, 400);
                gui.setResizable(true);
                gui.setVisible(true);
            }
        });
    }

    public GUI() {
        super("Tester");            
        loginP = new JPanel();
        GroupLayout loginPLayout = new GroupLayout(loginP);
        loginP.setLayout(loginPLayout);
        loginPLayout.setAutoCreateGaps(true);
        loginPLayout.setAutoCreateContainerGaps(true);

        Font font = new Font("Freestyle Script", Font.PLAIN,80);

        titleL = new JLabel("Tester");
        titleL.setFont(font);
        instructL = new JLabel("Please eneter your name and password and select your login type to login.");
        usernameL = new JLabel("Username:");
        passwordL = new JLabel("Password:");
        loginL = new JLabel("Login as");
        studentB = new JButton("Student");
        lecturerB = new JButton("Lecturer");
        usernameTF = new JTextField("", 100);
        passwordTF = new JTextField("", 100);

        studentB.addActionListener(new studentH());
        lecturerB.addActionListener(new lecturerH());

        loginPLayout.setHorizontalGroup(loginPLayout.createSequentialGroup()
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(usernameL)
                .addComponent(passwordL))
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                .addComponent(titleL)
                .addComponent(instructL)
                .addComponent(usernameTF)
                .addComponent(passwordTF)
                .addComponent(loginL)
            .addGroup(loginPLayout.createSequentialGroup()
                .addComponent(studentB)
                .addComponent(lecturerB)))
        );

        loginPLayout.setVerticalGroup(loginPLayout.createSequentialGroup()
            .addComponent(titleL)
            .addComponent(instructL)
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(usernameL)
                .addComponent(usernameTF))
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(passwordL)
                .addComponent(passwordTF))
                .addComponent(loginL)
            .addGroup(loginPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(studentB)
                .addComponent(lecturerB))
        );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        selectQP = new JPanel();
        GroupLayout selectQPLayout = new GroupLayout(selectQP);
        selectQP.setLayout(selectQPLayout);
        selectQPLayout.setAutoCreateGaps(true);
        selectQPLayout.setAutoCreateContainerGaps(true);

        String cat1 = "Collections";
        String cat2 = "Multithreading";
        String cat3 = "Networking";
        String cat4 = "JDBC";

        instruct1L = new JLabel("Select the groups of questions you would like to be tested on");
        askL = new JLabel("How many questions would you like?");
        checkBox1 = new JCheckBox(cat1);
        checkBox2 = new JCheckBox(cat2);
        checkBox3 = new JCheckBox(cat3);
        checkBox4 = new JCheckBox(cat4);
        viewB = new JButton("View Questions");
        startB = new JButton("Start Test");
        numOfQTF = new JTextField("", 100);

        viewB.addActionListener(new viewH());
        startB.addActionListener(new startH());

        selectQPLayout.setHorizontalGroup(selectQPLayout.createParallelGroup()
            .addComponent(instruct1L)
            .addComponent(checkBox1)
            .addComponent(checkBox2)
            .addComponent(checkBox3)
            .addComponent(checkBox4)
            .addComponent(askL)
            .addComponent(numOfQTF)
        .addGroup(selectQPLayout.createSequentialGroup()
            .addComponent(viewB)
            .addComponent(startB))
        );

        selectQPLayout.setVerticalGroup(selectQPLayout.createSequentialGroup()
            .addComponent(instruct1L)
            .addComponent(checkBox1)
            .addComponent(checkBox2)
            .addComponent(checkBox3)
            .addComponent(checkBox4)
            .addComponent(askL)
            .addComponent(numOfQTF)
            .addGroup(selectQPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(viewB)
                .addComponent(startB))
        );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Add Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        addQP = new JPanel();
        GroupLayout addQPLayout = new GroupLayout(addQP);
        addQP.setLayout(addQPLayout);
        addQPLayout.setAutoCreateGaps(true);
        addQPLayout.setAutoCreateContainerGaps(true);

        instructionsL = new JLabel("Please enter the name of the file:");
        txtL = new JLabel(".txt");
        infoL = new JLabel("You will be logged out after the questions have been added.");
        appendB = new JButton("Append Questions");
        overwriteB = new JButton("Overwrite Questions");
        pathTF = new JTextField("", 100);

        appendB.addActionListener(new appendH());
        overwriteB.addActionListener(new overwriteH());

        addQPLayout.setHorizontalGroup(addQPLayout.createSequentialGroup()
        .addGroup(addQPLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
            .addComponent(instructionsL)
            .addGroup(addQPLayout.createSequentialGroup()
                .addComponent(pathTF)
                .addComponent(txtL))
            .addComponent(infoL)
            .addGroup(addQPLayout.createSequentialGroup()
                .addComponent(appendB)
                .addComponent(overwriteB)))
        );

        addQPLayout.setVerticalGroup(addQPLayout.createSequentialGroup()
            .addComponent(instructionsL)
            .addGroup(addQPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(pathTF)
                .addComponent(txtL))
            .addComponent(infoL)
            .addGroup(addQPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(appendB)
                .addComponent(overwriteB))
        );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        qNumL = new JLabel("Question Number: " + qNumber);
        groupL = new JLabel("Group: " + group);
        questionL = new JLabel(question);
        opt1L = new JLabel(opt1);
        opt2L = new JLabel(opt2);
        opt3L = new JLabel(opt3);
        opt4L = new JLabel(opt4);
        opt1B = new JButton("A");
        opt2B = new JButton("B");
        opt3B = new JButton("C");
        opt4B = new JButton("D");

        questionP = new JPanel();
//            add(questionP);
        GroupLayout questionPLayout = new GroupLayout(questionP);
        questionP.setLayout(questionPLayout);
        questionPLayout.setAutoCreateGaps(true);
        questionPLayout.setAutoCreateContainerGaps(true);

        optH handler = new optH();
        opt1B.addActionListener(handler);
        //opt2B.addActionListener(handler);
        //opt3B.addActionListener(handler);
        //opt4B.addActionListener(handler);

        questionPLayout.setHorizontalGroup(questionPLayout.createSequentialGroup()
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(questionL)
                .addComponent(opt1B)
                .addComponent(opt2B)
                .addComponent(opt3B)
                .addComponent(opt4B))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(opt1L)
                .addComponent(opt2L)
                .addComponent(opt3L)
                .addComponent(opt4L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                .addComponent(groupL))
        );

        questionPLayout.setVerticalGroup(questionPLayout.createSequentialGroup()
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(groupL))
            .addComponent(questionL)
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt1B)
                .addComponent(opt1L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt2B)
                .addComponent(opt2L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt3B)
                .addComponent(opt3L))
            .addGroup(questionPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(opt4B)
                .addComponent(opt4L))
        );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Viewer Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        qNumL = new JLabel("Question Number: " + qNumber);
        groupL = new JLabel("Group: " + group);
        questionL = new JLabel(question);
        aL = new JLabel("A");
        bL = new JLabel("B");
        cL = new JLabel("C");
        dL = new JLabel("D");
        opt1L = new JLabel(opt1);
        opt2L = new JLabel(opt2);
        opt3L = new JLabel(opt3);
        opt4L = new JLabel(opt4);
        nextB = new JButton("Next");
        closeB = new JButton("Close");

        viewerP = new JPanel();
        GroupLayout viewerPLayout = new GroupLayout(viewerP);
        viewerP.setLayout(viewerPLayout);
        viewerPLayout.setAutoCreateGaps(true);
        viewerPLayout.setAutoCreateContainerGaps(true);

        viewerPLayout.setHorizontalGroup(viewerPLayout.createSequentialGroup()
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(questionL)
                .addComponent(aL)
                .addComponent(bL)
                .addComponent(cL)
                .addComponent(dL))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(opt1L)
                .addComponent(opt2L)
                .addComponent(opt3L)
                .addComponent(opt4L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                .addComponent(groupL)
                .addGroup(viewerPLayout.createSequentialGroup()
                    .addComponent(nextB)
                    .addComponent(closeB)))
        );

        viewerPLayout.setVerticalGroup(viewerPLayout.createSequentialGroup()
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(qNumL)
                .addComponent(groupL))
            .addComponent(questionL)
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(aL)
                .addComponent(opt1L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(bL)
                .addComponent(opt2L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(cL)
                .addComponent(opt3L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(dL)
                .addComponent(opt4L))
            .addGroup(viewerPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(nextB)
                .addComponent(closeB))
        );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mark Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        markP = new JPanel();
        GroupLayout markPLayout = new GroupLayout(markP);
        markP.setLayout(markPLayout);
        markPLayout.setAutoCreateGaps(true);
        markPLayout.setAutoCreateContainerGaps(true);

        int percentage = 90;
        int correct = 9;
        int total = 10;

        percentageL = new JLabel("You scored: " + percentage + "%");
        outOfL = new JLabel("You have " + correct + " corret answers out of a possible " + total);
        backB = new JButton("Back to Group Selection");
        logOutB = new JButton("Log Out");

        backB.addActionListener(new backH());
        logOutB.addActionListener(new logOutH());

        markPLayout.setHorizontalGroup(markPLayout.createParallelGroup()
            .addComponent(percentageL)
            .addComponent(outOfL)
        .addGroup(markPLayout.createSequentialGroup()
            .addComponent(backB)
            .addComponent(logOutB))
        );

        markPLayout.setVerticalGroup(markPLayout.createSequentialGroup()
            .addComponent(percentageL)
                .addComponent(outOfL)
            .addGroup(markPLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(backB)
                .addComponent(logOutB))
        );

        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Card Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        cardPanel = new JPanel(new CardLayout(0,0));
        JPanel[] cards = new JPanel[6];
        String[] titles = new String[6];
        cards[0] = loginP;
        cards[1] = selectQP;
        cards[2] = addQP;
        cards[3] = questionP;
        cards[4] = viewerP;
        cards[5] = markP;
        titles[0] = "loginP";
        titles[1] = "selectQP";
        titles[2] = "addQP";
        titles[3] = "questionP";
        titles[4] = "viewerP";
        titles[5] = "markP";
        for(int i = 0; i < cards.length; i++) {
            cardPanel.add(cards[i], titles[i]);
        }

        //You can remove this once you are satisfied card layout works with your buttons - this is just for the combobox I added
        setLayout(new BorderLayout());
        add(cardPanel, BorderLayout.CENTER);
        comboBox = new JComboBox<>(titles);
        comboBox.addActionListener(new CardListener());
        add(comboBox, BorderLayout.NORTH);
        //Add this back in once you remove the rest
//            add(cardPanel);

        pack();
    }

    //You can remove this once you are satisfied card layout works with your buttons - this is just for the combobox I added
    private class CardListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            CardLayout c1 = (CardLayout)(cardPanel.getLayout());
            c1.show(cardPanel, (String)comboBox.getSelectedItem());
        }
    }

/*#################################################################################################################################################################
 *#####################################################################Action Listeners############################################################################
 *###############################################################################################################################################################*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Login Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class studentH implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            Student stu = new Student();

            String username = usernameTF.getText();
            String password = passwordTF.getText();
            boolean correct = stu.Student(username, password);
            if(correct == true) {
                changeCard("selectQP");
            }
            else {
                JOptionPane.showMessageDialog(null, "You have entered incorect details. Please try again", "Incorrect Details", JOptionPane.ERROR_MESSAGE);
                usernameTF.setText("");
                passwordTF.setText("");
            }
        }
    }

    private class lecturerH implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            Lecturer lect = new Lecturer();

            String username = usernameTF.getText();
            String password = passwordTF.getText();
            boolean correct = lect.Lecturer(username, password);
            if(correct == true) {
                changeCard("addQP");
            }
            else {
                JOptionPane.showMessageDialog(null, "You have entered incorect details. Please try again", "Incorrect Details", JOptionPane.ERROR_MESSAGE);
                usernameTF.setText("");
                passwordTF.setText("");
            }
        }
    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Add Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class appendH implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            //Append appen = new Append();
            String path = pathTF.getText();
            //appen.Append(path);
        }
    }

    private class overwriteH implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
//            Overwrite over = new Overwrite();
        }
    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class optH implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent event ) {

        }
    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Question Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class viewH implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent event ) {

        }
    }

    private class startH implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent event ) {

        }
    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mark Panel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private class backH implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent event ) {

        }
    }

    private class logOutH implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent event ) {

        }
    }

    private void changeCard(String newCardName) {
       CardLayout c1 = (CardLayout)(cardPanel.getLayout());
       c1.show(cardPanel, newCardName);
    }
}
sage88
  • 4,104
  • 4
  • 31
  • 41
  • It's the way I was taught to. My lecturer insists on doing it that way. If I had it my way, for an app like this, it'd all be one class. – vanatic Nov 19 '14 at 22:19
  • Well your instructor is doing it wrong. He needs to put the GUI on the EDT thread. Your current code won't do that. It's fine if you want to put it in a separate class if you must, but at least use SwingUtilities.invokeLater... – sage88 Nov 19 '14 at 22:22
  • 1
    @vanatic posted working minimal self contained working code for your GUI class with fixed indenting (yours seemed pretty all over the place). You should use this as a starting point and slowly reintroduce the rest of the code until you find your error. – sage88 Nov 19 '14 at 22:39
  • 1
    @vanatic I added CardLayout code for you to use. Notice that each JPanel is no longer added directly to the JFrame, instead it's added to the cardPanel which has CardLayout. Each JPanel is then given a corresponding String title in the titles array. To switch to a different panel, just call changeCard and give it the String title of the card you want to switch to. – sage88 Nov 20 '14 at 00:17
  • @vanatic I added a JComboBox controlling the cardlayout to show it working. You can remove this very easily, but it illustrates that the CardLayout is working. – sage88 Nov 20 '14 at 00:28
  • Thank you so, so much. I tried using card layout, but I couldn't get it as perfect as this. The combo box was unnecessary, as the buttons worked already. You are amazing. Everybody up vote this guy, please, he has gone above and beyond my expectations. sage88, thank you. – vanatic Nov 20 '14 at 04:45
  • 1
    @vanatic Glad to help. Now if I were going to improve your code further, I'd limit it to a single action listener for all of your various buttons. Then with setActionCommand() you can tell each button what it sends to your listener as the ActionEvent. You then call getActionCommand on the ActionEvent object to retrieve the string you set as the action command. You can then write a switch statement to tell it how to behave differently for each button with the cases being the different possible strings. This will drastically reduce the clutter of inner classes you now have. – sage88 Nov 20 '14 at 09:06
  • @vanatic--I concur. sage88 rocks. I thank him, too, since I learned a lot from his code and commentary. – DSlomer64 Nov 20 '14 at 11:17