Question

I am attempting to make a JTabbedPane that can show all the field pertaining to an object (in this case a person) and all the inherent fields that go with it. In doing so i am attempting to have the first panel(the one I am currently having trouble with) contain name, phone number and email.

As you can see below, I placed the JLabels for the title of the row in one panel, and the JLabels for the information in the right panel. The issue with this is that they will not separate from each other. I would like the left labels to be left aligned and together they should take up the entirety of the available panel size. setAlignment didn't seem to have any effect.

This is the code for the text panel creation:

    private static JComponent makeTextPanel(String text){
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1,1));
    panel.add(filler);
    return panel;
}

This is the code for TabbedPane as of this second:

    public static void tabbedReadMenu(){
    JFrame readMenu = new JFrame("Student Records");
    JTabbedPane tabbedPane = new JTabbedPane();
    Dimension d = new Dimension(300,300);
    tabbedPane.setPreferredSize(d);

    JComponent basicInfoPane = makeTextPanel("Basic Info");
    tabbedPane.addTab("Basic Info", BasicInfoTab());
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    readMenu.add(tabbedPane);

    readMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    readMenu.pack();
    readMenu.setLocationRelativeTo(null);
    readMenu.setVisible(true);

}

And this is the information for the actual tab along with the nested panels:

   private static JPanel BasicInfoTab(){
    JPanel basicInfoTab = new JPanel();
    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();
    JPanel buttons = new JPanel();
    JPanel spacing = new JPanel();
    JPanel title = new JPanel();
    JPanel mainPanel = new JPanel();

    LayoutManager singleLine = new GridLayout(5,1);
    leftPanel.setLayout(singleLine);
    rightPanel.setLayout(singleLine);
    buttons.setLayout(new FlowLayout());
    mainPanel.setLayout(new SpringLayout());

    spacing.setLayout(new GridLayout(3,1));


    JLabel jspacing = new JLabel("          ");
    JLabel jspacing1 = new JLabel("          ");
    JLabel jspacing2 = new JLabel("          ");
    spacing.add(jspacing);
    spacing.add(jspacing1);
    spacing.add(jspacing2);
    spacing.setLayout(new FlowLayout());
    JLabel header = new JLabel("Basic Student Information");
    title.add(header);

    JLabel FNLabel = new JLabel("First Name: ");                //Creates all Labels
    JLabel MILabel = new JLabel("Middle Initial: ");
    JLabel LNLabel = new JLabel("Last Name: ");
    JLabel IDLabel = new JLabel("Student ID: ");
    JLabel EmailLabel = new JLabel("E-mail Address: ");


    leftPanel.add(FNLabel);
    leftPanel.add(MILabel);
    leftPanel.add(LNLabel);
    leftPanel.add(IDLabel);
    leftPanel.add(EmailLabel);

    selectedStudent = SaveFunction.passStudent();
    JLabel FNInfo = new JLabel(selectedStudent.getFirstName());
    JLabel MIInfo = new JLabel(selectedStudent.getMidInitial());
    JLabel LNInfo = new JLabel(selectedStudent.getLastName());
    JLabel IDInfo = new JLabel(selectedStudent.getID());
    JLabel EmailInfo = new JLabel(selectedStudent.getEmail());

    rightPanel.add(FNInfo);
    rightPanel.add(MIInfo);
    rightPanel.add(LNInfo);
    rightPanel.add(IDInfo);
    rightPanel.add(EmailInfo);
    FNLabel.setHorizontalAlignment(JLabel.LEFT);
    MILabel.setHorizontalAlignment(JLabel.LEFT);
    LNLabel.setHorizontalAlignment(JLabel.LEFT);
    IDLabel.setHorizontalAlignment(JLabel.LEFT);
    EmailLabel.setHorizontalAlignment(JLabel.LEFT);

    JButton edit = new JButton("Edit");
    JButton close = new JButton("Close");
    buttons.add(edit);
    buttons.add(close);

    basicInfoTab.add(title, BorderLayout.NORTH);
    basicInfoTab.add(spacing, BorderLayout.CENTER);
    basicInfoTab.add(leftPanel, BorderLayout.WEST);
    basicInfoTab.add(rightPanel, BorderLayout.EAST);
    basicInfoTab.add(buttons, BorderLayout.SOUTH);  



    return basicInfoTab;
}

Any help would be very much appreciated (including any more general coding tips on how I should improve).

Was it helpful?

Solution

Basically, for form design, I pretty much stick to GridBagLayout. If you don't mind a 3rd party layout, you should also consider MigLayout

Check out How to use GridBagLayout for more details...

For example...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FormLayout {

    public static void main(String[] args) {
        new FormLayout();
    }

    public FormLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JLabel FNLabel = new JLabel("First Name: ");
            JLabel MILabel = new JLabel("Middle Initial: ");
            JLabel LNLabel = new JLabel("Last Name: ");
            JLabel IDLabel = new JLabel("Student ID: ");
            JLabel EmailLabel = new JLabel("E-mail Address: ");

            JLabel FNInfo = new JLabel("My first name");
            JLabel MIInfo = new JLabel("My initial");
            JLabel LNInfo = new JLabel("My last name");
            JLabel IDInfo = new JLabel("My student id");
            JLabel EmailInfo = new JLabel("My Email");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.anchor = GridBagConstraints.WEST;

            add(FNLabel, gbc);
            gbc.gridy++;
            add(MILabel, gbc);
            gbc.gridy++;
            add(LNLabel, gbc);
            gbc.gridy++;
            add(IDLabel, gbc);
            gbc.gridy++;
            add(EmailLabel, gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;

            add(FNInfo, gbc);
            gbc.gridy++;
            add(MIInfo, gbc);
            gbc.gridy++;
            add(LNInfo, gbc);
            gbc.gridy++;
            add(IDInfo, gbc);
            gbc.gridy++;
            add(EmailInfo, gbc);                
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top