Reputation: 363
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);
}
}
Upvotes: 3
Views: 146
Reputation: 11
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.
Upvotes: 0
Reputation: 51524
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));
Upvotes: 3
Reputation: 794
Have you looked at Border
s?
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.
Upvotes: -1