Adding Components to JPanel not working correctly

I'm trying to build a JFrame that contains a parent JPanel that will hold a JPanel used to display a message view, a vertical strut and a JPanel that holds VCR-like buttons to cycle through the messages.

My parent JPanel uses a BorderLayout and the Border is a TitledBorder which tells which product you are viewing (i.e., Message 1 of 5). I build the message JPanel, vertical strut and button JPanel and add them all in order to the parent JPanel which then gets added to the rootContentPane of the JFrame. All that appears is the parent JPanel's TitledBorder, the strut and the button JPanel. Using JSwat, I've been able to determine that the message JPanel has 0 for both its height and width after adding the message components to the JPanel.

I create the message JPanel with a BorderLayout and an OvalBorder as copied from Manning Press's Swing book (which works fine in other JFrames that I have built), then add other components as necessary to the individual messages (mostly items around the edges with a central display for the actual message). What I can't figure out is why the height and width of the message JPanel isn't growing as I add components.

I had previously used the same code to display a single message (minus the parent JPanel, strut and button JPanel) where I added the border panels (northPanel, eastPanel, southPanel and westPanel) created in createMsgPanel() directly to the contentPane and it worked perfectly, so I know that the code that adds the message works fine. Then, the requirements changed (go figure) and I had to display multiple messages from the same screen.

Here's what I've got:

public class Layout
extends JFrame
implements ActionListener
{
private MissionData missionData;
private JPanel messagePanel = null;
private int index = 0;
private int numMsgs = 0;
private JPanel mainPanel = null;
private JPanel buttonPanel = null;
private TitledBorder titledBorder = null;

Layout ()
{
}

public Layout (MissionData msn)
{
super ();

missionData = msn;

setSize (640, 640);
setTitle (((Message) (missionData.messages.elementAt(0))).name);

numMsgs = missionData.messages.size();

titledBorder = new TitledBorder (new LineBorder (Color.BLACK), "Message " + String.valueOf (index + 1) + " of " + String.valueOf (numMsgs), TitledBorder.LEFT, TitledBorder.TOP);

mainPanel = new JPanel ();
mainPanel.setLayout (new BorderLayout());
mainPanel.setBorder (new CompoundBorder (titledBorder, new EmptyBorder (new Insets (3, 3, 3, 3))));

messagePanel = new JPanel();
messagePanel.setLayout (new BorderLayout ());
messagePanel.setBorder (new CompoundBorder (new EmptyBorder (50, 50, 50, 50), new OvalBorder (20, 20, Color.white)));
messagePanel.setBackground (Color.black);

createButtonPanel ();

createMsgPanel ((Message) missionData.messages.elementAt (0));

mainPanel.add (messagePanel);
mainPanel.add (Box.createVerticalStrut (20));
mainPanel.add (buttonPanel);

Container mainContentPane = getContentPane();
mainContentPane.add (mainPanel);
}

private void createMsgPanel (Message msg)
{
MessageType msgType = null;

if (msg.getFunctionalAreaDesignator(0) == Message.GENERAL_INFO)
{
if (msg.getMessageNumber(0) == 1)
{
msgType = FREE_TEXT;
}
}
else if (msg.getFunctionalAreaDesignator(0) == Message.SUPPORT)
{
if (msg.getMessageNumber(0) == 33)
{
msgType = CAS;
}
else if (msg.getMessageNumber(0) == 34)
{
msgType = OSR;
}
}

// Setup NORTH Panel of Display
JPanel northPanel = new JPanel (new GridLayout (2, 6));
northPanel.setBackground (Color.black);
northPanel.add (new JTIMLabel ("", false));
northPanel.add (new JTIMLabel ("<html>RECV</html>", false));
northPanel.add (new JTIMLabel ("<html>SEND</html>", false));
northPanel.add (new JTIMLabel ("", false));
northPanel.add (new JTIMLabel ("", false));
messagePanel.add (northPanel, BorderLayout.NORTH);

// Setup EAST Box of Display
Box eastBox = new Box (BoxLayout.Y_AXIS);

eastBox.add (new JTIMLabel ("<html>W<br>L<br>C<br>O</html>", false));
eastBox.add (Box.createGlue());
eastBox.add (new JTIMLabel ("<html>C<br>N<br>T<br>C<br>O</html>",
false));
eastBox.add (Box.createGlue());

messagePanel.add (eastBox, BorderLayout.EAST);

// Setup SOUTH Panel of Display
JPanel southPanel = new JPanel (new GridLayout (2, 5));
southPanel.setBackground (Color.black);
southPanel.add (new JTIMLabel ("", false));
southPanel.add (new JTIMLabel ("", false));
southPanel.add (new JTIMLabel ("", false));
southPanel.add (new JTIMLabel ("", false));
southPanel.add (new JTIMLabel ("<html>ON</html>", false));
southPanel.add (new JTIMLabel ("", false));
southPanel.add (new JTIMLabel ("", false));
southPanel.add (new JTIMLabel ("", false));
southPanel.add (new JTIMLabel ("<html>MENU</html>", false));
southPanel.add (new JTIMLabel ("<html>VMF</html>", false));
southPanel.add (new JTIMLabel ("", false));
messagePanel.add (southPanel, BorderLayout.SOUTH);

// Setup WEST Box of Display
JTIMLabel incrLabel = null;
JTIMLabel decrLabel = null;

Box westBox = new Box (BoxLayout.Y_AXIS);
westBox.add (Box.createGlue());
westBox.add (new JTIMLabel ("<html>U<br>F<br>C</html>", false));
westBox.add (Box.createGlue());
if (msgType == CAS)
{
westBox.add (new JTIMLabel ("<html>/\\</html>", false));
westBox.add (Box.createGlue());
westBox.add (new JTIMLabel ("<html>\\/</html>", false));
westBox.add (Box.createGlue());
incrLabel = new JTIMLabel ("<html>I<br>N<br>C<br>R</html>", false);
westBox.add (incrLabel);
westBox.add (Box.createGlue());
decrLabel = new JTIMLabel ("<html>D<br>E<br>C<br>R</html>", false);
westBox.add (decrLabel);
westBox.add (Box.createGlue());
}

messagePanel.add (westBox, BorderLayout.WEST);

// Create CENTER Box to display message bodies
GriddedPanel centerBox = new GriddedPanel ();
centerBox.setBackground (Color.black);
messagePanel.add (centerBox, BorderLayout.CENTER);

if (msgType == CAS)
{
new CASDisplay (msg, centerBox, incrLabel, decrLabel);
}
else if (msgType == OSR)
{
new OSRDisplay (msg, centerBox);
}
else if (msgType == FREE_TEXT)
{
new FreeTextDisplay (msg, centerBox);
}
}

private void createButtonPanel ()
{
// build the button panel
buttonPanel = new JPanel ();
buttonPanel.setLayout (new BoxLayout (buttonPanel, BoxLayout.X_AXIS));
buttonPanel.setBorder (new LineBorder (Color.BLACK));

// Create and add the buttons
buttonPanel.add (createButton ("FIRST_BUTTON"));
buttonPanel.add (createButton ("PREV_BUTTON"));
buttonPanel.add (createButton ("NEXT_BUTTON"));
buttonPanel.add (createButton ("LAST_BUTTON"));
}

private JButton createButton (String buttonName)
{
JButton button = new JButton ();
button.addActionListener (this);
button.setActionCommand (buttonName);
Image image = null;

String tooltip = "Press to go to the ";

if (buttonName.equals ("FIRST_BUTTON"))
{
image = new ImageIcon ("firstArrowIcon.gif").getImage();
tooltip += "First";
}
else if (buttonName.equals ("PREV_BUTTON"))
{
image = new ImageIcon ("previousArrowIcon.gif").getImage();
tooltip += "Previous";
}
else if (buttonName.equals ("NEXT_BUTTON"))
{
image = new ImageIcon ("nextArrowIcon.gif").getImage();
tooltip += "Next";
}
else if (buttonName.equals ("LAST_BUTTON"))
{
image = new ImageIcon ("lastArrowIcon.gif").getImage();
tooltip += "Last";
}

tooltip += " message in the lst";
button.setToolTipText (tooltip);

button.setIcon (new ImageIcon (image.getScaledInstance (36, 36, Image.SCALE_FAST)));

return button;
}


public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("FIRST_BUTTON"))
{
index = 0;
}
else if (e.getActionCommand ().equals ("PREV_BUTTON"))
{
if (index > 0)
{
index--;
}
}
else if (e.getActionCommand ().equals ("NEXT_BUTTON"))
{
if (index < numMsgs - 1)
{
index++;
}
}
else if (e.getActionCommand ().equals ("LAST_BUTTON"))
{
index = numMsgs - 1;
}

titledBorder.setTitle ("Message " + String.valueOf (index + 1) +
" of " + String.valueOf (numMsgs));

createMsgPanel ((Message) missionData.messages.elementAt (index));
}
}
[10587 byte] By [mazeing] at [2007-11-19 11:09:35]
# 1 Re: Adding Components to JPanel not working correctly
when you add component to the container with the BorderLayout without constraints, BorderLayout.CENTER is used. So, only the last added component will be shown. You can test this with the simple example:

JFrame frame = new JFrame();
frame.setSize(1024, 768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.RED);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.GREEN);
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.show();

In your case you should use BoxLayout.
bolshik at 2007-11-10 2:23:00 >
# 2 Re: Adding Components to JPanel not working correctly
In your case you should use BoxLayout.
That was it. Thanks! :thumb: :thumb: :thumb:
mazeing at 2007-11-10 2:24:00 >
# 3 Re: Adding Components to JPanel not working correctly
welcome :)
bolshik at 2007-11-10 2:25:01 >