# Java Swing question

**URL:** <https://boards.straightdope.com/t/java-swing-question/268521>\
**Category:** Factual Questions\
**Created:** [October 10, 2004, 10:13pm UTC](https://boards.straightdope.com/t/java-swing-question/268521 "2004-10-10T22:13:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MindWanderer](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@MindWanderer](https://boards.straightdope.com/u/MindWanderer)\
**Post date:** [October 10, 2004, 10:13pm UTC](https://boards.straightdope.com/t/java-swing-question/268521/1 "2004-10-10T22:13:08Z")

</div>

I’m creating a prototype user interface for a program, which currently contains a menubar, a JPanel, and a JTextArea.

When I run it, all that gets displayed is the Text Area but not the JPanel with all the labels.

Originally I was using ImageIcons for the labels, but for debugging purposes I changed to using text only labels, and commented out the image icons being created.

I am using pack, and set visible and I’ve tried validate. This isn’t an applet just an application. Anyone got any idea why my JPanel isn’t showing up?

…  
paintPanel = new JPanel(new GridLayout(20, 20));

```
// populate the grid with dummy icons until full implementation is underway
   // then the JPanel will be filled from Simulation

```

/\* ImageIcon icon1 = new ImageIcon("~/documents/source/politician.jpg");  
ImageIcon icon2 = new ImageIcon("~/documents/source/lobbyist.jpg");  
ImageIcon icon3 = new ImageIcon("~/documents/source/dollar.jpg");\*/

```
for(int i = 0; i &lt; 20; i++)
{
	for(int j = 0; j &lt; 20; j++)
	{
		if((20 * i + j) % 6 == 0)
			paintPanel.add(new JLabel("A"));
		else if((20 * i + j) % 6 == 1)
			paintPanel.add(new JLabel("B"));
		else if((20 * i + j) % 6 == 2)
			paintPanel.add(new JLabel("C"));
		else
			paintPanel.add(new JLabel(" "));
	}
}
	
paintPanel.validate();
paintScroll = new JScrollPane(paintPanel);
frame.getContentPane().add(paintScroll);
	
log = new JTextArea("Political Predator Prey System

```

A group two production.");  
textScroll = new JScrollPane(log);  
frame.getContentPane().add(textScroll);  
…

---

<div class="post-metadata">

**Author:** ![MindWanderer](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@MindWanderer](https://boards.straightdope.com/u/MindWanderer)\
**Post date:** [October 10, 2004, 11:18pm UTC](https://boards.straightdope.com/t/java-swing-question/268521/2 "2004-10-10T23:18:28Z")

</div>

I figured it out. The default layout for a content pane of a JFrame must be bad or something. Finally I figured out that I needed to set that layout as well as the JPanel layout and everything finally showed up on the screen.

Like all bugs there is a :smack: :smack: :smack: moment at how simple it is in the end.

---

<div class="post-metadata">

**Author:** ![minor7flat5](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/minor7flat5/32/258_2.png) [@minor7flat5](https://boards.straightdope.com/u/minor7flat5)\
**Post date:** [October 11, 2004, 12:07am UTC](https://boards.straightdope.com/t/java-swing-question/268521/3 "2004-10-11T00:07:56Z")

</div>

You got it. Just so you don’t wonder what the “must be bad or something” part of the problem is, here goes:

First your code:

```auto

paintPanel.validate();
paintScroll = new JScrollPane(paintPanel);
frame.getContentPane().add(paintScroll);

log = new JTextArea("Political Predator Prey System
A group two production.");
textScroll = new JScrollPane(log);
frame.getContentPane().add(textScroll);

```

You have called “add” on the **JFrame** ’s content pane twice with no constraints.

The default layout manager of a **JFrame** is **BorderLayout**.

From the JDK docs on **BorderLayout** :

> [@](#):
>
> As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER

Hence, you have added one component to CENTER, followed by adding another component to CENTER. In essence, you have overwritten **paintScroll** with **textScroll**.
