3

In a project of mine I have been trying to add a JConsonle to a JPanel witch is contained by another JPanel.

The problem is that the JConsole keeps being set 5px from the top of the JPanel.At first I tought it was the container that wasent beeing set up right but after giving it a red background I realised that the console is being set 5px from the top.

I've also tried to use BorderLayout to set it in the NORTH or CENTER of the JPanel but that dosent work either.

This is my code:

public class MonopolyPanel extends JPanel {

  JPanel consoleP = new JPanel();
  JConsole console = new JConsole();

  MonopolyPanel(){

   this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
   console.setPreferredSize(new Dimension(530, 300));  
   consoleP.add(console);
   this.add(consoleP);

  }
}
3
  • Where does the JConsole control come from? Commented Jan 14, 2012 at 13:32
  • It's an external library I found it while reading around stackoverflow beanshell.org Commented Jan 14, 2012 at 13:35
  • I have tryed with all the layout type Commented Jan 14, 2012 at 14:00

3 Answers 3

3

The console is added to consoleP which has FlowLayout by default, which by default has a vertical and horizontal gap of 5px. Instantiating that with a FlowLayout with zero gaps should do the trick

consoleP == new JPanel(new FlowLayout(align, 0, 0));
Sign up to request clarification or add additional context in comments.

1 Comment

by the end of the project another 2 panels will exist in the monopolyPanel so it isent unnecesary your solutiion did the trick thanks
0

From my experience, the best and most flexible layout is GridBagLayout.

99% of panels in Swing I make are GridBagLayout, otherwise its impossible to get it all right.

Because with that layout, you can set weights, exact padding and spacing parameters.

The other layoutmanagers are to limited and not very configurable.

Comments

-1

Have you looked at Borders?

It's possible you need to set the JPanel's or the JConsole's border to an EmptyBorder, like so:

component.setBorder(BorderFactory.createEmptyBorder());

You might also look for something about insets in the Javadocs.

2 Comments

no, the "free area" is not set by the border but by the layoutManager - as you could have quickly checked by writing a small sscce to verify your assumption ;-)
@kleopatra Yeah, probably should have :). When I answered this question there was no code sample, and in the past I've found that using EmptyBorder has solved problems with strange insets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.