Reputation: 10323
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?
Upvotes: 1
Views: 99
Reputation: 9584
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)
Upvotes: 2
Reputation: 205765
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.
Upvotes: 1
Reputation: 30022
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.
Upvotes: 1