Border Around JPanel

J

Jason Cavett

I have created a JPanel that uses a FlowLayout to hold a series of
buttons. However, the height of the JPanel is too tall. It's not as
simple as setting the dimensions or anything like that. Essentially,
I want to remove the "border" (or the buffer) that surrounds the edges
of the JPanel to bring the edges as close as possible to the edges of
the buttons but without touching.

If I do just try to set the JPanel's dimensions, the distance between
the top of the buttons is farther away than the distance between the
bottom.

Is this possible to do? Thanks.
 
J

John B. Matthews

Jason Cavett said:
I have created a JPanel that uses a FlowLayout to hold a series of
buttons. However, the height of the JPanel is too tall. It's not as
simple as setting the dimensions or anything like that. Essentially,
I want to remove the "border" (or the buffer) that surrounds the edges
of the JPanel to bring the edges as close as possible to the edges of
the buttons but without touching.

If I do just try to set the JPanel's dimensions, the distance between
the top of the buttons is farther away than the distance between the
bottom.

Is this possible to do? Thanks.

Without code it's hard to say. JPanel defaults to FlowLayout, but an
errant BorderLayout can add unexpected height. Adding a matte from
BorderFactory in a contrasting color may help show where the problem
originates. Alternatively, switch to BoxLayout.
 
J

Jason Cavett

Without code it's hard to say. JPanel defaults to FlowLayout, but an
errant BorderLayout can add unexpected height. Adding a matte from
BorderFactory in a contrasting color may help show where the problem
originates. Alternatively, switch to BoxLayout.

Here is the code.

/**
* The tree button panel sits above the project tree and provides
various
* buttons that perform different functions on the tree.
*
* @return javax.swing.JPanel
*/
private JPanel getTreeButtonPanel() {
if (treeButtonPanel == null) {
treeButtonPanel = new JPanel();
FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
treeButtonPanel.setLayout(layout);

treeButtonPanel.add(this.getExpandAllButton());
treeButtonPanel.add(this.getCollapseAllButton());
treeButtonPanel.add(this.getSortTreeButton());
}

return treeButtonPanel;
}
 
J

Jason Cavett

What results did you get from these suggestions?
Here is the [meaningless code fragment].

A Short, Self-Contained, Correct/Compilable Example would be more
instructive: <http://pscode.org/sscce.html>.

I didn't get any results from your suggestions as I am not using a
BorderLayout (which is what the code was trying to show). I'm just
using a single panel with a FlowLayout and throwing some various
things on it.

Thanks anyway.
 
J

John B. Matthews

Jason Cavett said:
 Jason Cavett <[email protected]> wrote:
I have created a JPanel that uses a FlowLayout to hold a series of
buttons.  However, the height of the JPanel is too tall.  It's not as
simple as setting the dimensions or anything like that.  Essentially,
I want to remove the "border" (or the buffer) that surrounds the edges
of the JPanel to bring the edges as close as possible to the edges of
the buttons but without touching.
If I do just try to set the JPanel's dimensions, the distance between
the top of the buttons is farther away than the distance between the
bottom.
Is this possible to do?  Thanks.
Without code it's hard to say. JPanel defaults to FlowLayout, but an
errant BorderLayout can add unexpected height. Adding a matte from
BorderFactory in a contrasting color may help show where the problem
originates. Alternatively, switch to BoxLayout.

What results did you get from these suggestions?
Here is the [meaningless code fragment].

A Short, Self-Contained, Correct/Compilable Example would be more
instructive: <http://pscode.org/sscce.html>.

I didn't get any results from your suggestions as I am not using a
BorderLayout (which is what the code was trying to show). I'm just
using a single panel with a FlowLayout and throwing some various
things on it.

Ah, I see. Same with using colored borders? It looks like GridLayout
gets JButtons pretty close to the edges, but there's some space, even
with zero h/vgap. Setting the buttons' background color shows that the
extra space belongs to the buttons.
 
J

Jason Cavett

I have created a JPanel that uses a FlowLayout to hold a series of
buttons.  However, the height of the JPanel is too tall.  It's not as
simple as setting the dimensions or anything like that.  Essentially,
I want to remove the "border" (or the buffer) that surrounds the edges
of the JPanel to bring the edges as close as possible to the edges of
the buttons but without touching.
If I do just try to set the JPanel's dimensions, the distance between
the top of the buttons is farther away than the distance between the
bottom.
Is this possible to do?  Thanks.
Without code it's hard to say. JPanel defaults to FlowLayout, but an
errant BorderLayout can add unexpected height. Adding a matte from
BorderFactory in a contrasting color may help show where the problem
originates. Alternatively, switch to BoxLayout.
What results did you get from these suggestions?
Here is the [meaningless code fragment].
A Short, Self-Contained, Correct/Compilable Example would be more
instructive: <http://pscode.org/sscce.html>.
I didn't get any results from your suggestions as I am not using a
BorderLayout (which is what the code was trying to show).  I'm just
using a single panel with a FlowLayout and throwing some various
things on it.

Ah, I see. Same with using colored borders? It looks like GridLayout
gets JButtons pretty close to the edges, but there's some space, even
with zero h/vgap. Setting the buttons' background color shows that the
extra space belongs to the buttons.

Ahhh...okay. Well, I guess it doesn't matter too much. I'm somewhat
picky about the GUIs I design, but it doesn't look too bad.

Thanks for your help, John. Appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top