buckley.w.david
buckley.w.david

Reputation: 25

Trouble adding Jpanels correctly to a BoxLayout using Swing

I am attempting to create a GUI for a program I have previously written, but am having some trouble aligning some panels how I want them. Currently I have this code.

package javaapplication10;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.EmptyBorder;

public class GUITest extends JFrame {

    public static final int WIDTH = 425;
    public static final int HEIGHT = 250;

    JPanel addPanel;
    JComboBox referenceList;
    JTextField callNoText;

    public static void main(String[] args) {
        GUITest gui = new GUITest( );
        gui.setVisible(true);
    }

    public GUITest( )
    {
        super("Library Search");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

        addPanel = new JPanel( );
        addPanel.setBackground(Color.WHITE);
        addPanel.setVisible(true);
        addPanel.setLayout(new BorderLayout());

        JPanel info = new JPanel();
        info.setBackground(Color.WHITE);
        info.setLayout(new BoxLayout(info, BoxLayout.Y_AXIS));

        JLabel message_add = new JLabel("Adding a reference.");
        message_add.setBorder(new EmptyBorder(10, 10, 0, 0));
        message_add.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel typeSelect = new JPanel(new FlowLayout());
        typeSelect.add(new JLabel("Type: "));
        String[] references = {"Book", "Journal"};
        referenceList = new JComboBox(references);
        typeSelect.add(referenceList);
        typeSelect.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel callNoSelect = new JPanel();
        callNoText = new JTextField(20);
        callNoSelect.add(new JLabel("Call No:\t"));
        callNoSelect.add(callNoText);
        callNoSelect.setAlignmentX( Component.LEFT_ALIGNMENT );

        info.add(message_add);
        info.add(typeSelect);
        info.add(callNoSelect);

        addPanel.add(info, BorderLayout.CENTER);
        add(addPanel);

        JMenu commandMenu = new JMenu("Commands");
        JMenuItem addChoice = new JMenuItem("Add");
        commandMenu.add(addChoice);
        JMenuItem searchChoice = new JMenuItem("Search");
        commandMenu.add(searchChoice);
        JMenuItem quitChoice = new JMenuItem("Quit");
        commandMenu.add(quitChoice);

        JMenuBar bar = new JMenuBar( );
        bar.add(commandMenu);
        setJMenuBar(bar);
    }

}

This creates a GUI that looks like this: Current GUI

As you can see the two panels typeSelect and callNoSelect are added to the addPanel, but they look different than the label message_add that I added in before them. I've tested adding in another label, and it looks how I would like, but for some reason I can't get panels to do the same. I would like for the page to look something like this:

Correct GUI*

Also the reason I have this all in addPanel is that I will have other panels that I want to display to the user, which I will just turn visibility off and on for as needed. The reason I have addPanel as a BorderLayout is that I will eventually have some more controls in the eastern and southern quadrants once I work out how to get the center how I want it.

*I created the Correct GUI in paint in an attempt to better show what I want

Upvotes: 0

Views: 31

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

JLabel is transparent, where as JPanel is not. So the JLabel is showing the background color of the info panel, which is white, while the typeSelect and callNoText are using their own default colors. You could make these other panel's transparent, using setOpaque(false) or change their background color to match that of the info panel

As to your layout, you might consider using something more flexible, like GridBagLayout, see How to Use GridBagLayout and Laying Out Components Within a Container for some more details

Upvotes: 1

Related Questions