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?
Related
I am designing a JFrame that contains two ScrollPanes ordered sequentially, one below the other. I am using the inbuilt form editor of NetBeans (Matisse?) which uses GroupLayout.
What I observe: When resizing the frame, first the ScrollPane at the bottom is resized until it vanishes then the buttons above this ScrollPane and finally the other Scrollpane.
What I would like: When resizing the Frame, both ScrollPanes should be resized equally, so if the frame changes size by X, I want both Scrollpanes to change their size by X/2.
Is this possible and how? (Solutions for GroupLayout preferred)
P.S.: The total Layout includes Buttons and Labels placed in a table like manner. So GroupLayout is already quite useful for it.
P.S.: It seems the best option beyond simple Layouts (Grid, Box) is MigLayout.
Let me cite from the white paper
Growing and Shrinking
What should happen when a component isn't given the preferred size is extremely customizable. There is the option to divide components into grow and shrink priority groups where a higher priority will shrink/grow before components/rows/columns with lower priorities are resized. Grow weight within those groups can be set to specify how the free space or shrinkage should be divided between the components/rows/columns in that priority group. ...
P.S.2: I am now using MigLayout and it is so powerful that I want to recommend it again. Btw. I find their cheat sheet very useful (all options explained). Also the demos on their website contain many example and the source code. The problem here and many others can be easily solved with MigLayout.
Is this possible and how?
by using GridLayout, GridBagLayout, BoxLayout (have to override all Min, Max and PreferredSize) and for MigLayout
Is this possible and how? (Solutions for GroupLayout preferred)
I woudln't be going this way, even could be possible
A grid layout ensures that the elements are evenly sized. Use:
JPanel panel = new JPanel(new GridLayout(2,1));
Since you want them vertically aligned, that is after each other, 2 denotes the number of rows, and 1 denotes the number of columns.
Then simply add the ScrollPanes to the panel:
panel.add(scrollPane1);
panel.add(scrollPane2);
For documentation on Grid Layout, look at this
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 .
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.
On my Java Swing application I have two components. On the left side is a navigation (JList) and on the right side is a JTable. I would like to leave the possibility to increase the size of the window, without increasing the size of both components.
The proportion of 50/50 is kept, through ought the whole sizing. I use GridLayout. Is this behavior rooted into the LayoutManager or is a property which has to be set?
GridbagLayout will allow you to achieve this. However, have you also considered using a JSplitPane where the left-hand side contains your navigation panel and the right hand side contains the table? You could configure it so that all additional space is allocated to the right hand side by calling setResizeWeight(0.0). However, you still retain the flexibility of allowing the user to manually resize the navigation area if required. You also have the option to hide your navigation panel completely by calling setOneTouchExpandable(true) on the split pane.
From what I know the GridLayout manager resizes all cells to the same size. Knowing this you might use it anyway just add the component you want to stay unchanged to a panel and then add this panel to a cell instead.
Or use a different layout manager mine favourite is TableLayout, where you can set which columns/rows should fill the empty space where the rest will stay in their preferred size.
Good luck, Boro
Suggestion: Don't us GridLayout. Instead use other layouts such Borderlayout or GridBagLayout or a combination of layouts. For instance if you used BorderLayout, you could but the JTable BorderLayout.CENTER and the JList in one of the other positions. Or if you use GridBagLayout, then by setting your GridBagConstraint weightx and weighty values correctly and the fill values (only you know what you currently desire), would allow selective enlargement of the components added to the container.
I'm trying to put two buttons inside a panel using Swing widgets. Inside the NetBeans IDE, my JSeparator border property is set to (No border) in the properties pane.
Nevertheless a line appears. This is not what I would expect from a separator object. Am I doing something wrong? Coming from a background in Delphi, and C# WinForms, I expect to find some oddities in Swing. But how exactly do you make a transparent gap of a particular size, between two buttons in a panel? Do I have to play with layouts and avoid the JSeparator?
Update: It should be trivial to do this with a layout and without any separator object. So how do you do that? I am looking into the NetBeans layout customizer and properties inspector and finding no way to do it. (Answer: Layouts with Insets, instead of separators.)
You should take a look at the static utility methods on the Box class. They can be used to manufacture fixed struts that act as invisible separators; e.g.
JPanel pnl = new JPanel(new FlowLayout());
pnl.add(new JButton("Hello"));
pnl.add(Box.createHorizontalStrut(10)); // Fixed width invisible separator.
pnl.add(new JButton("Goodbye");
This produces more compact code than creating / configuring a JPanel yourself with appropriate minimum, maximum and preferred dimensions.
JSeparator is meant to be a visible separator between components.
From the javadoc for JSeparator:
JSeparator provides a general purpose component for implementing divider lines - most commonly used as a divider between menu items that breaks them up into logical groupings.
If you want to put a component in between two components that is invisible just use an JPanel instead. Then set the size of the panel with setPreferedSize() and setMin/MaxSize().
You don't need JSeparator. Most layouts allow you to set gap (space) between compoponents. And Box class can be particularly useful.
Using addSeparator with a value of 1 for height makes it invisible for me, for example:
MyJToolBar.addSeparator(new Dimension(20, 1));