How can I add a gap around the edge of a Jbutton? - java

I want to leave the default border on my JButtons, but put empty space around them as well. I'm using a vertical BoxLayout.
I originally said nothing about the borders, and got single pixel LineBorders, which I want, but the buttons all butted up against each other.
I then tried button[i].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)). Rather than adding blank space around the button, it made the buttons' areas expand. It also removed the LineBorder.
I then tried: button[i].setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5), button.getBorder()))
This gave me back the LineBorder, but rather than adding blank space outside the line, it just extended the buttons' areas beyond the line!
I realise I can add blank boxes to space my buttons out, but I want space on the sides of them as well, which is why I want to add an EmptyBorder. I'm new to Swing, so maybe there's an entirely better way of doing this that I don't know about :)
I'm using Jython, but the API should be the same as from Java.

Conceptually, the "empty borders" you want to add are not really part of the button (for example, they should not be clickable).
This is actually a matter of layout, so you should probably check the documentation of the layout manager you are using. For example:
Some layout managers, such as FlowLayout, BorderLayout, or GridLayout, have hgap and vgap properties to specify the horizontal and vertical gaps between components.
With GridBagLayout you would set the insets of a GridBagConstraints object.
With BoxLayout you would add "rigid areas", "glue", and "fillers" (see the Box class).
And so on.

I think it's simpler to add the button to a panel and set the empty border to the panel.

Related

Java GUI place layout under boxlayout

I have used borderlayout to specify where the content of my java GUI shall be placed, I have then chosen to place it on EAST and then made two boxlayouts to show two columns of buttons. I now have to place something underneath it and not beside it. How would you suggest or advice me to do so, using any layout but preferably boxlayout and not absolute layout(null). Thanks in advance.
Image:
The arrow points to the place I want another JPanel to be.
You could...
Wrap both of the button panels in a JPanel
Whatever component goes at the arrow, wrap in a JPanel with GrigbagLayout (just to center it).
Create another JPanel with BorderLayout that will hold the above panels. Use CENTER and SOUTH.
Give an EmptyBorder to the SOUTH panel, only specifying the top region and space it accordingly.
Really there are many ways to accomplish this. The key though is to nest JPanels and make use of the different layout managers with each, use EmptyBorders or stuts for empty spaces til you get your desired effect. The possibilities are endless. I don't think there's one right answer. Since we don't have a runnable example, I say just try the above, and mix and match will you get what you want.

Choosing the layout managers and number of panels for Java GUI

I would appreciate, If anyone here is kind enough to recommend me what are the layout managers to choose
and how to separate the attached GUI into Jpanels.
Thanks so much in advance.
What I have tried so far:
Jframe - BorderLayout
Map (the grid and clear button from another class extending jpanel) - so I have put it in jframe.CENTER
the buttons to the right: jframe.EAST
I put them in a jpanel in a gridlayout (but I cannot get the spacing between the components)
buttons at the bottom: jframe.SOUTH
I put them in a jpanel in a gridlayout (but I cannot get the spacing between the components)
When trying to determine what layout(s) you should use, you should start by trying to determine areas of responsibility...
For example...
Based on your needs, I might start with a GridBagLayout. This might seem complex, but if you break the UI down into seperate components, focusing on their individual needs, it should become simpler...
For the panel on the left...
I would be temptered to use a GridBagLayout, simply because it allows the components to use there preferred sizes, but still allows you to set up a grid like pattern...
For the arrow buttons...
This becomes a little more complicated, but I would use a GridLayout(2, 3) (2 rows, 3 columns). This will require to add a filler panel at the first and third position along the top row, but still maintain the buttons at a equal size...
For this panel...
I would be tempted to either use a GridBagLayout, because it will allow you to span the rows or even split it again into two separate panels, with the controls on the left in a GridLayout(2, 1) and the control on the right in something like a BorderLayout as required...
For "progress" panel...
I would be tempted to use...GridBagLayout. Mostly because it would allow you to provide more weight to the progress bars then the labels.
For the main panel...
I would probably be tempted to either use a BorderLayout, with the Clear Map on another panel of it's own, allowing it maintain it's preferred size, in the NORTH position and the map panel in the CENTER or even a GridBagLayout depending on what the invidual components are...

Java Swing FlowLayout Alignments

I'm fairly new to Java and I'm trying to create a GUI application with some labels, buttons, and textfields. The program is pretty simple and I just wanted to use a default layout, which is FlowLayout. I managed to place and size everything fine, but the only thing seem to be not working is the alignment. I want to place buttons and textfields with certain alignments, but whenever I set an alignment, it moves the text inside of whatever the object rather than the object itself. For example, I wrote:
button.setHorizontalAlignment(JButton.RIGHT);
but it seems like it aligns the text inside the button instead of the button itself.
Is there any way to align the button itself rather than the text inside of it?
I know the alignment stuff could be easier with some other type of layout (e.g. BoxLayout), but I just want to use the FlowLayout for this one, unless it is impossible to align them using the FlowLayout (which I don't think so).
Thanks in advance.
See the constructor FlowLayout(int align).
Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.
It seems you are after a FlowLayout.RIGHT as seen in this answer (the combo and check box at the top).
I don't think you can do this with a FlowLayout alone.
My suggestions would be:
Consider switching to MigLayout which is a much more powerful layout mechanism. MigLayout basically lets you position you components within a flexible grid, and you can set the specific alignment of a component within each grid cell.
When you want alignment of subcomponents, it also often makes sense to put them inside a nested JPanel. You can then use a separate layout for this JPanel (BorderLayout perhaps?) which will enable you to get the exact alignment that you want.
setHorizontalAlignment of AbstractButton sets the horizontal alignment of the icon and text not the position of the button. AbstractButton's default is SwingConstants.CENTER.
If you want to align the button..set the position while adding it to the panel or frame..something like this....
p.add(button, BorderLayout.SOUTH);//using `BorderLayout`
Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line.

How can I add a space in between two buttons in a boxLayout?

I have four buttons in a BoxLayout group. This is just a sample of two because it's all repeated code. I want to create a slight space between each button so they don't run into each other. I have tried practically every method in the .add(Box.Create....) and nothing worked.
enter.add(Box.createVerticalGlue());
enter.add(Box.createHorizontalGlue());
//enter.add(new JSeparator(SwingConstants.HORIZONTAL));
JButton float = new JButton("LOWER");
float.add(Box.createVerticalGlue());
float.add(Box.createHorizontalGlue());
If you want to have space between components, you can either add an empty border to one or both components, or insert invisible components to provide the space. You can create invisible components with the help of the Box class.
since you already used glue with no success (I doubt why?), you may try something like Rigid area,
// Horizontal spacer
container.add(firstComponent);
container.add(Box.createRigidArea(new Dimension(5, 0)));
container.add(secondComponent);
Have a look at Using Invisible Components as Filler which gives you a lot of options and explanations.
ADDITIONAL INFORMATION, From Putting Space Between Components,
Three factors influence the amount of space between visible components in a container:
The layout manager
Some layout managers automatically put space between components; others do not. Some let you specify the amount of space between components. See the how-to page for each layout manager for information about spacing support.
Invisible components
You can create lightweight components that perform no painting, but that can take up space in the GUI. Often, you use invisible components in containers controlled by BoxLayout. See How to Use BoxLayout for examples of using invisible components.
Empty borders
No matter what the layout manager, you can affect the apparent amount of space between components by adding empty borders to components. The best candidates for empty borders are components that typically have no default border, such as panels and labels. Some other components might not work well with borders in some look-and-feel implementations, because of the way their painting code is implemented. For information about borders, see How to Use Borders .

My JComponents are hogging too many pixels

I am trying to prune off unecessary pixels on an existing Java Swing UI. It uses GridbagConstrains mostly, but some other LayoutManagers too. A lot of the components are just added to containers and by default they'll pad themselves with 10 extra pixels on each side. For example, with the GridBagConstrains, I have a checkBox that is padded with way too many pixels on each side by default. I set the ipadxy and insets to 0, but it had no effect. Can anyone offer some general tips?
Your JCheckBox may have a default border that pads the component out some. Try setting a zero-width empty border on the checkbox, I think that will help:
checkBox.setBorder(BorderFactory.createEmptyBorder());
Edit
For JPanels, make sure their layout manager is not adding padding. A common case is new JPanel(). By default the layout for a JPanel is a BorderLayout with some horizontal and vertical gap. Instead, use new JPanel(new BorderLayout()) which still uses a border layout, but the horizontal and vertical gap will be zero
Also, JPanels and JToolBars are both containers, keep in mind their children may have non-empty borders by default as well (for example, a JButton added to a JToolBar)
Using GridBagConstraints set weightx and weighty of the components to 0.
Then add an invisible component e.g. new JLabel() to your panel with weightx and weighty of 1.
Some L&Fs (e.g. Nimbus, Aqua) support a JComponent.sizeVariant, as discussed in Resizing a Component and Using Client Properties.
At the same time, be wary of overcrowding. Also consider alternate designs, including tabbed pane, card layout, tool bar, palette and modeless dialog. See also this Q&A.
Have you set the Insets too?

Categories