views:

590

answers:

1

Hello there. I am writing a simple application in Java that does some particle simulation on a bunch of sheep (don't ask). For this I want a window with a JPanel for graphics (which will be resizable with a simple combobox that contains some standard resolutions) and some other elements like buttons to start and pause the simulation etc.

My question: I'm using the JFrame.pack method to pack everything nicely together using a borderLayout. But for some reason the JPanel is packed wrong, it seems like the packing ignores it, so the window is resized to fit the size of only the two buttons that I have now. What am I doing wrong?

This is the code so far (bit of a newbie, so no comments on my dumbness if there is any ;)):

public class Window {
 public Sheepness sheepness;

 public ButtonPanel buttonPanel;
 public PaintPanel paintPanel;
 public JFrame frame;

 public Window(Sheepness sheepness, int width, int height) {
  this.sheepness = sheepness;

  frame = new JFrame("Sheepness simulation");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.setSize(width, height);

  BorderLayout frameLayout = new BorderLayout();
  JPanel background = new JPanel(frameLayout);
  background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

  buttonPanel = new ButtonPanel(this);
  background.add(BorderLayout.SOUTH, buttonPanel.buttonBox);

  paintPanel = new PaintPanel(this);
  paintPanel.setSize(600, 600);
  background.add(BorderLayout.CENTER, paintPanel);

  frame.getContentPane().add(background);
  frame.pack();
  frame.setResizable(false);
  frame.setVisible(true);
 }
}

public class PaintPanel extends JPanel {
 public Window window;

 public PaintPanel(Window window) {
  this.window = window;
 }

 @Override
 public void paintComponent(Graphics g) {
  g.setColor(Color.blue);
  g.fillRect(0, 0, 300, 200);
 }
}

public class ButtonPanel {
 public Window window;
 public Box buttonBox;

 public JButton startButton;
 public JButton resetButton;

 public ButtonPanel(Window window) {
  this.window = window;

  buttonBox = new Box(BoxLayout.X_AXIS);

  startButton = new JButton("Start");
  startButton.addActionListener(new startButtonListener());
  buttonBox.add(startButton);

  resetButton = new JButton("Reset");
  resetButton.addActionListener(new resetButtonListener());
  buttonBox.add(resetButton);
 }
}
+4  A: 

Try:

paintPanel.setPreferredSize(600, 600);

As Window.pack() sizes to the preferred sizes of its subcomponents and JPanel gets its preferred size from its child components (in your case there are none).

Matthew Murdoch
It works! Thank you very much!
FinalArt2005
Same code, new problem: I'm getting somewhat of a border to the right and bottom of the window. I know there is another similar thread around here but the solution there was setting the resizable option of the frame to false, though this doesn't work for me. It's probably caused by the pack method, but why does it add the border? I just want it to pack tightly around all the components (with a border of 10 px but that's already done with setBorder). Also, there is a big 'whitespace' between the JPanel and the buttons, is this caused by the same problem and how do I remove it?
FinalArt2005
Sorry, not to the bottom, only to the right, but I guess it's bottom and right of the JPanel, so that it also causes the 'whitespace' between the JPanel and the buttons.
FinalArt2005
I tried filling it with more than the size of the JPanel, and now the border is gone. Why is the panel bigger than what I specified?
FinalArt2005
It's exactly 30 px higher and 10 px wider than what I specified.
FinalArt2005
I'm not sure I followed all that. If you have a separate problem, ask it as a new question (it sounds like including a screenshot may help in this case too).
Matthew Murdoch
Ok, I'm sorry, I'll put it in a new question. I just thought it would be easier this way since the code was already here.
FinalArt2005
Put it in a new question, if you have this problem of the border as well, check: http://stackoverflow.com/questions/1593683/unwanted-border-around-jpanel
FinalArt2005