-1

Normally adding a customized JPanel with a specific size to another JPanel (container) without a specified size works like a charm. You can't see the container at all. In this example, the red visible border is actually the background color of my container. The blue is the border of the container. Why do the red area appear/why don't it normally appear? I'm pretty sure that:

Jpanel panel = new JPanel;
panel.setBackground(new Color(Color.BLACK));
JPanel panel2 = new JPanel;
panel2.setBackground(new Color(Color.RED));
panel2.setPrefferedSize(new Dimension(200,200));
panel.add(panel2);

will be a fully red window, no black border visible. I can't see what I do very different?

enter image description here

3 classes to run the code:

public class Center extends JPanel {

JPanel centerFrame = new JPanel();

public Center() {
    setLayout(new BorderLayout());
    centerFrame.setBackground(Color.RED);
    centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
    centerFrame.add(panel1());
    add(centerFrame, BorderLayout.CENTER);
    add(new Buttons(), BorderLayout.PAGE_END);
}

public JPanel panel1() {
    JPanel pane = new JPanel(new BorderLayout());
    JPanel content = new JPanel();
    content.setPreferredSize(new Dimension(400,200));
    pane.add(content, BorderLayout.CENTER);
    return pane;
}
}

public class Buttons extends JPanel {

public Buttons() {
    setLayout(new GridLayout(2, 3));
    add(new JButton("Button 1"));
    add(new JButton("Button 2"));
    add(new JButton("Button 3"));
    add(new JButton("Button 4"));
    add(new JButton("Button 5"));
    add(new JButton("Button 6"));
}
}

public class Run extends JFrame {

public Run() {
    add(new TestClass());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] _) {
    new Run();
}
}
user2651804
  • 1,464
  • 4
  • 22
  • 45
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Mar 28 '14 at 18:01
  • Well I did! All the parts are there(complete) and I tested the minimal version before i posted it? Also I'm well aware that many of you advanced users advocate against the use of setXXXSize, but seeing how the docs trail use it, I think it's quite outside my area of concern, as a novice programmer, to wonder if I shouldn't be using it. – user2651804 Mar 28 '14 at 18:11
  • Well you did a poor job of it. There is no `TestClass` above. A Further problem is 'parts'. An MCVE can have more than one class, but is ideally **1 source file** as opposed to 3 with bits of another and entire classes missing. – Andrew Thompson Mar 28 '14 at 19:52

2 Answers2

3

Yours is almost a minimal example program. To truly comply, it must compile (yours doesn't -- new TestClass()?), and it should be in one file with imports. This for example is closer:

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

public class Run extends JFrame {

   public Run() {
      // !!?? add(new TestClass());
      add(new Center());  // !! this is better
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);
      setVisible(true);
   }

   public static void main(String[] _) {
      new Run();
   }
}

class Buttons extends JPanel {

   public Buttons() {
      setLayout(new GridLayout(2, 3));
      add(new JButton("Button 1"));
      add(new JButton("Button 2"));
      add(new JButton("Button 3"));
      add(new JButton("Button 4"));
      add(new JButton("Button 5"));
      add(new JButton("Button 6"));
   }
}

class Center extends JPanel {

   JPanel centerFrame = new JPanel();

   public Center() {
      setLayout(new BorderLayout());
      centerFrame.setBackground(Color.RED);
      centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
      centerFrame.add(panel1());
      add(centerFrame, BorderLayout.CENTER);
      add(new Buttons(), BorderLayout.PAGE_END);

      System.out.println(centerFrame.getLayout());  // !! hm, this may be important
   }

   public JPanel panel1() {
      JPanel pane = new JPanel(new BorderLayout());
      JPanel content = new JPanel();
      content.setPreferredSize(new Dimension(400, 200));
      pane.add(content, BorderLayout.CENTER);
      return pane;
   }
}

But more importantly, if you run this program, you'll see that I've added a line of code which will answer your question for you. :)

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hint: not only does it print out the layout, but it also prints out the layout's default state, one that has a direct bearing on your question of why the 5 point gap is seen. Solution, either use a different layout, or assign 0 gaps to this one. – Hovercraft Full Of Eels Mar 28 '14 at 18:22
  • I figured it would be easier in one file, however I haven't much knowledge on using inner classes. I thought for one, though, that they actually had to be declared INSIDE the closing bracket of the file-class (Run). Is there any difference between defining inner classes inside and outside of the closing bracket? – user2651804 Mar 28 '14 at 20:34
  • @user2651804: these are not ***inner*** classes. They are stand alone classes, all in one single file, with just only one that is allowed to be declared `public`, that being the class with the same name as the file. But that's not as important as the concept on how to debug your program. If you analyze and run my code, it should spit out for you the cause of your problem, and I wanted to try to show you both the solution and the process you should use to solve similar problems in the future. – Hovercraft Full Of Eels Mar 28 '14 at 20:36
  • Yeah, I did run your code naturally, I see the hint and solution. I just fully intentionally assumed that the problem wasn't with the LayoutManager. Actually I thought I already tested it. But there is however a thing here I'm interested to learn! If I can declare additional stand-alone classes in a single file, can I also access them from outside the file, in the same package? – user2651804 Mar 28 '14 at 20:43
  • @user2651804: same package -- yes you can access them, outside packages no. It is not a good thing to do in general except for these minimal example programs that you post here. – Hovercraft Full Of Eels Mar 28 '14 at 20:48
3

The problem is that JPanels come with a default FlowLayout that in turn come equipped with default 5 pixel gaps. If you change the gaps to 0, you won't see it

centerFrame.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

-Or-

centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
FlowLayout flow = (FlowLayout)centerFrame.getLayout();
flow.setHgap(0);
flow.setVgap(0);

See the FlowLayout API

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Hm, I was hoping he'd discover this for himself. – Hovercraft Full Of Eels Mar 28 '14 at 18:31
  • @HovercraftFullOfEels I noticed that when I saw your answer :/ – Paul Samsotha Mar 28 '14 at 18:31
  • Wow... well I could've sworn that my red/black background example in the start wouldn't have shown black at all... I thought I did it a billion times.. Guess I always added a BorderLayout to my panels by coincidence. I could've saved myself so many hours of troubleshooting had I not made as many assumptions... >. – user2651804 Mar 28 '14 at 19:14