Hey everyone,

Im having trouble with the placement of GUI components and need you help getting the things in the right place.
The problem is on the left side where the elements are all misplaced including:
The JMenuBar - should be up against the left wall
JLabel "content for..." - should be underneath the menubar
The other label and the scrollpane should be under the course content label.

Basically everything need to be gridy++.

The Jpanel had ist own grid bag layout and is good the way it is, however, if you look you'll notice that the spaces are actually empty labels. How can I have the space without having those labels.

Thanks

package gui;

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

public class IndividualCourse extends JFrame{

    JMenuBar contactsAndMainJmb;
    JMenu contactsJm, mainMenuJm;
    JButton addItemBtn, deleteItemBtn;
    JLabel contentForXLbl, permaCurrentLbl, sortByLbl, permaAddItemLbl, itemNameLbl, dueDateLbl, spaceFillerLbl;
    JScrollPane currentItemsJsp;
    JPanel itemAdditionJpl;
    JRadioButton sortDueSoonJBtn, dueLaterJrBtn;
    JTextField itemNameJtf, dueDateJtf;
    GridBagConstraints gbc = new GridBagConstraints();

    String courseName = "courseNameHere";

    public IndividualCourse(){

        setTitle("Course View for " + courseName);
        setSize(550, 400);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        gbc.insets = new Insets(5,5,5,5);


        contactsAndMainJmb = new JMenuBar();
        contactsJm = new JMenu("Contacts");
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        contactsAndMainJmb.add(contactsJm);

        mainMenuJm = new JMenu("Main Menu");
        gbc.gridx = 1;
        contactsAndMainJmb.add(mainMenuJm);
        add(contactsAndMainJmb);

        contentForXLbl = new JLabel("Content for Course: "+ courseName);
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(contentForXLbl, gbc);

        permaCurrentLbl = new JLabel("Current Active Items:");
        gbc.gridy= 2;
        add(permaCurrentLbl, gbc);

        currentItemsJsp = new JScrollPane();
        //currentItemsJsp.setEditable(false);
        currentItemsJsp.setPreferredSize(new Dimension(50, 75));
        currentItemsJsp.setBorder(BorderFactory.createLineBorder(Color.black));
        gbc.gridy = 3;
        //gbc.gridheight = 3;
        add(currentItemsJsp, gbc);
////////////////////////////////////Side 1//////////////////////////////////////

        itemAdditionJpl = new JPanel();
        itemAdditionJpl.setLayout(new GridBagLayout());
        GridBagConstraints gbcJp = new GridBagConstraints();


        permaAddItemLbl = new JLabel("Add a study item:");
        gbcJp.gridx = 0;
        gbcJp.gridy = 0;
        itemAdditionJpl.add(permaAddItemLbl, gbcJp);

        itemNameLbl = new JLabel("Item Name: ");
        gbcJp.gridy = 1;
        itemAdditionJpl.add(itemNameLbl, gbcJp);

        itemNameJtf = new JTextField(10);
        gbcJp.gridx = 1;
        itemAdditionJpl.add(itemNameJtf, gbcJp);

        dueDateLbl = new JLabel("Due Date: ");
        gbcJp.gridx = 0;
        gbcJp.gridy = 2;
        itemAdditionJpl.add(dueDateLbl, gbcJp);

        dueDateJtf = new JTextField(10);
        gbcJp.gridx = 1;
        itemAdditionJpl.add(dueDateJtf, gbcJp);

        spaceFillerLbl = new JLabel("  ");
        gbcJp.gridx = 0;
        gbcJp.gridy = 3;
        itemAdditionJpl.add(spaceFillerLbl, gbcJp);

        spaceFillerLbl = new JLabel("  ");
        gbcJp.gridx = 1;
        gbcJp.gridy = 3;
        itemAdditionJpl.add(spaceFillerLbl, gbcJp);

        spaceFillerLbl = new JLabel("  ");
        gbcJp.gridx = 0;
        gbcJp.gridy = 4;
        itemAdditionJpl.add(spaceFillerLbl, gbcJp);

        spaceFillerLbl = new JLabel("  ");
        gbcJp.gridx = 1;
        gbcJp.gridy = 4;
        itemAdditionJpl.add(spaceFillerLbl, gbcJp);

        addItemBtn = new JButton("Add Item");
        gbcJp.gridx = 0;
        gbcJp.gridy = 5;
        itemAdditionJpl.add(addItemBtn, gbcJp);

        deleteItemBtn = new JButton("Delete Item");
        gbcJp.gridx = 1;
        itemAdditionJpl.add(deleteItemBtn, gbcJp);

        gbc.gridx = 2;
        gbc.gridy = 1;
        //gbc.gridheight = 3;
        itemAdditionJpl.setBorder(BorderFactory.createLineBorder(Color.black));
        add(itemAdditionJpl, gbc);

        pack();
    }
    public static void main(String[] args){
        IndividualCourse newCourse = new IndividualCourse();
        newCourse.setVisible(true);
    }
}

Update: I solved the issue of left side by putting those lables and the scroll pane in another JPanel. Still no luck with the bar and the spacing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.