0

I'm trying to work on my GUI coding so am taking it upon myself just to create several different interfaces for examples I had been given in class.

Im trying to create a GUI here that at the top has Labels followed by their respective text fields for the user to input, with one on top of the other. I then want this to be followed by a series of two buttons and finally a results field that is uneditable (as will later introduce some fields for results) and opaque. I get a whole series of errors so was wondering if I am just doing something stupid wrong.

The code for the panel is here:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GradePanel extends JPanel {

private JButton addEntry, calculate;
private JLabel name, grade;
private JTextField nameField, gradeField, resultField;

public GradePanel() {

    // Button to add entry to list
    addEntry = new JButton("Add entry to list");
   // addEntry.addActionListener(new tempListener());

    // Button to print all entries in correct format
    calculate = new JButton("Print all user grades");
   // calculate.addActionListener(new tempListener());

    //Create Labels
    name = new JLabel("Enter student name: ");
    nameField = new JTextField(10);
   // nameField.addActionListener(new tempListener());

    grade = new JLabel("Enter students mark: ");
    gradeField = new JTextField(5);
    // gradeField.addActionListener(new tempListener());

    //Bottom segment for result
    resultField = new JTextField();
    resultField.setOpaque(false);
    resultField.setEditable(false);

    setLayout(new BorderLayout());

    //Bottom Panel
    JPanel GradePanel = new JPanel();
    GradePanel.setBorder(BorderFactory.createTitledBorder("Students/Results"));
    GradePanel.setOpaque(false);
    GradePanel.add(resultField);

    //Button Panel
    JPanel ButtonPane = new JPanel();
    ButtonPane.add(addEntry, BoxLayout.LINE_AXIS);
    ButtonPane.add(calculate, BoxLayout.LINE_AXIS);

    //Label Panel
    JPanel labelPane = new JPanel();
    labelPane.add(name);
    labelPane.add(Box.createRigidArea(new Dimension (5,0)));
    labelPane.add(nameField);
    labelPane.add(Box.createRigidArea(new Dimension (0,2)));
    labelPane.add(grade);
    labelPane.add(Box.createRigidArea(new Dimension (5,0)));
    labelPane.add(gradeField);

    //Add all panels to the main panel
    add(labelPane, BorderLayout.NORTH); 
    add(ButtonPane, BorderLayout.CENTER);
    add(GradePanel, BorderLayout.SOUTH);

    setBackground(Color.WHITE);
    setPreferredSize(new Dimension(400, 300));
}

Thanks in advance!

EDIT: Forgot the errors:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position
    at java.awt.Container.addImpl(Container.java:1087)
    at java.awt.Container.add(Container.java:467)
    at GradePanel.<init>(GradePanel.java:55)
    at Grade$1.run(Grade.java:26)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

EDIT 2:

public class GradePanel2 extends JPanel {

    private JButton addEntry, calculate;
    private JLabel name, grade;
    private JTextField nameField, gradeField, resultField;

    public GradePanel2() {

        // Button to add entry to list
        addEntry = new JButton("Add entry to list");
        // addEntry.addActionListener(new tempListener());

        // Button to print all entries in correct format
        calculate = new JButton("Print all user grades");
        // calculate.addActionListener(new tempListener());

        // Create Labels
        name = new JLabel("Enter student name: ");
        nameField = new JTextField(10);
        // nameField.addActionListener(new tempListener());

        grade = new JLabel("Enter students mark: ");
        gradeField = new JTextField(5);
        // gradeField.addActionListener(new tempListener());

        // Bottom segment for result
        resultField = new JTextField();
        resultField.setOpaque(false);
        resultField.setEditable(false);

        setLayout(new BorderLayout());

        // Bottom Panel
        JPanel GradePanel = new JPanel();
        GradePanel.setBorder(BorderFactory.createTitledBorder("Students/Results"));
        GradePanel.setOpaque(false);
        GradePanel.add(resultField);

        // Button Panel
        JPanel ButtonPane = new JPanel();
        ButtonPane.setLayout(new BoxLayout(ButtonPane, BoxLayout.PAGE_AXIS));
        ButtonPane.add(addEntry, BoxLayout.LINE_AXIS);
        ButtonPane.add(calculate, BoxLayout.LINE_AXIS);

        // Label Panel
        JPanel labelPane = new JPanel();
        labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.PAGE_AXIS));
        labelPane.add(name);
        labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
        labelPane.add(nameField);
        labelPane.add(Box.createRigidArea(new Dimension(0, 2)));
        labelPane.add(grade);
        labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
        labelPane.add(gradeField);

        // Add all panels to the main panel
        add(labelPane, BorderLayout.NORTH);
        add(ButtonPane, BorderLayout.CENTER);
        add(GradePanel, BorderLayout.SOUTH);

        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(400, 300));
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SkyPlus
  • 105
  • 1
  • 9
  • 3
    And the errors you are seeing would be...? – KevinO Mar 24 '16 at 01:38
  • 1
    `setPreferredSize(new Dimension(400, 300));` is a bad idea – MadProgrammer Mar 24 '16 at 01:39
  • 1
    `ButtonPane` is using a `FlowLayout`, but you seem to be trying to apply `BoxLayout` constraints to it which is confusing the API making it think you're trying to insert a component at a given location within it's list, which doesn't exist, hence the *"Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position"* exception – MadProgrammer Mar 24 '16 at 01:40
  • Maybe you should take a closer look at [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer Mar 24 '16 at 01:41
  • 2
    BTW - quite often compiler errors on line N will **cause** compiler errors on lines >N. So deal with the first one, first. Speaking of which: Always copy/paste error and exception output! – Andrew Thompson Mar 24 '16 at 01:41
  • 2
    Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Mar 24 '16 at 01:42
  • Added errors. @MadProgrammer I will certainly have a read, like I said Im doing all this to try and get fluent with it so any resources are a great help. Could you explain where about I have made ButtonPane run as a FlowLayout? – SkyPlus Mar 24 '16 at 01:45
  • @SkyPlus The default layout for a `JPanel` is `FlowLayout`, so the moment you did `new JPanel()` you started using `FlowLayout` – MadProgrammer Mar 24 '16 at 01:46
  • @MadProgrammer I see, that makes sense. So I have probably created errors in the other parts such as labels etc too then? Would a .setLayout help in this instance to fix the problem? Thanks – SkyPlus Mar 24 '16 at 01:50
  • @SkyPlus IMHO, I'd be looking towards `GridBagLayout`, but I'm more familiar with it then I am with `BoxLayout` – MadProgrammer Mar 24 '16 at 01:57
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Mar 24 '16 at 11:10

1 Answers1

1

You're applying BoxLayout constraints to a JPanel which is using a FlowLayout by default...

JPanel ButtonPane = new JPanel();
ButtonPane.add(addEntry, BoxLayout.LINE_AXIS);
ButtonPane.add(calculate, BoxLayout.LINE_AXIS);

which is causing a

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position
    at java.awt.Container.addImpl(Container.java:1085)
    at java.awt.Container.add(Container.java:465)
    at javaapplication866.JavaApplication866$GradePanel.<init>(JavaApplication866.java:98)

Perhaps you should start by haiving a look at How to Use BoxLayout

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Still getting an error after making some changes, any idea whats causing it now? – SkyPlus Mar 24 '16 at 02:09
  • Will come back and try it tomorrow with the GridBagLayout and start over but should try and fix this too to learn my errors. Thanks for the help – SkyPlus Mar 24 '16 at 02:15