Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a number of ways in a Swing GUI to provide a separation between components, and white space around components:</p> <ul> <li><code>JToolBar</code> has the methods <a href="http://docs.oracle.com/javase/8/docs/api/javax/swing/JToolBar.html#addSeparator--" rel="nofollow noreferrer"><code>addSeparator()</code></a> &amp; <a href="http://docs.oracle.com/javase/8/docs/api/javax/swing/JToolBar.html#addSeparator-java.awt.Dimension-" rel="nofollow noreferrer"><code>addSeparator(Dimension)</code></a>.</li> <li><code>JMenu</code> uses a spacing component better suited to menus, available through <a href="http://docs.oracle.com/javase/8/docs/api/javax/swing/JMenu.html#addSeparator--" rel="nofollow noreferrer"><code>addSeparator()</code></a>. </li> </ul> <p>But more generally, look to:</p> <ul> <li>The spacing as can be defined in the layout constructors.</li> <li>Borders. </li> </ul> <p>Here is an example of using the layout separator <code>hGap</code> &amp; <code>vGap</code> values &amp; borders (specifically an <code>EmptyBorder</code>) to provide 'white' (actually shown as <strong>red</strong> to make it very obvious) space. Adjust the spinners to see the result.</p> <p><img src="https://i.stack.imgur.com/lYyOZ.png" alt="With no GUI white space"></p> <p><img src="https://i.stack.imgur.com/NVtKI.png" alt="With GUI white space"></p> <pre><code>import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.event.*; public class WhiteSpace { private JPanel gui = null; private BorderLayout mainLayout; private FlowLayout buttonLayout; private EmptyBorder border; public Container getGui() { if (gui==null) { mainLayout = new BorderLayout(0,0); gui = new JPanel(mainLayout); gui.setBackground(Color.RED); border = new EmptyBorder(0,0,0,0); JTree tree = new JTree(); tree.setVisibleRowCount(10); for (int ii = tree.getRowCount(); ii&gt;-1; ii--) { tree.expandRow(ii); } gui.add(new JScrollPane( tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.LINE_START); gui.add(new JScrollPane(new JTextArea(10,30))); buttonLayout = new FlowLayout(FlowLayout.CENTER,0,0); JPanel buttonPanel = new JPanel(buttonLayout); gui.add(buttonPanel, BorderLayout.PAGE_START); buttonPanel.add(new JLabel("H Gap")); final JSpinner hSpinner = new JSpinner(new SpinnerNumberModel(0,0,15,1)); buttonPanel.add(hSpinner); buttonPanel.add(new JLabel("V Gap")); final JSpinner vSpinner = new JSpinner(new SpinnerNumberModel(0,0,15,1)); buttonPanel.add(vSpinner); buttonPanel.add(new JLabel("H Border")); final JSpinner hBorderSpinner = new JSpinner(new SpinnerNumberModel(0,0,15,1)); buttonPanel.add(hBorderSpinner); buttonPanel.add(new JLabel("V Border")); final JSpinner vBorderSpinner = new JSpinner(new SpinnerNumberModel(0,0,15,1)); buttonPanel.add(vBorderSpinner); ChangeListener changeListener = new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int hGap = ((Integer)hSpinner.getValue()).intValue(); int vGap = ((Integer)vSpinner.getValue()).intValue(); int hBorder = ((Integer)hBorderSpinner.getValue()).intValue(); int vBorder = ((Integer)vBorderSpinner.getValue()).intValue(); adjustWhiteSpace(hGap,vGap,hBorder,vBorder); } }; hSpinner.addChangeListener(changeListener); vSpinner.addChangeListener(changeListener); hBorderSpinner.addChangeListener(changeListener); vBorderSpinner.addChangeListener(changeListener); } return gui; } private void adjustWhiteSpace(int hGap, int vGap, int hBorder, int vBorder) { mainLayout.setHgap(hGap); mainLayout.setVgap(vGap); buttonLayout.setHgap(hGap); gui.setBorder(new EmptyBorder(vBorder,hBorder,vBorder,hBorder)); Container c = gui.getTopLevelAncestor(); if (c instanceof Window) { Window w = (Window)c; w.pack(); } } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { WhiteSpace ws = new WhiteSpace(); // the GUI as seen by the user (without frame) Container gui = ws.getGui(); JFrame f = new JFrame("White (OK Red) Space"); f.add(gui); // Ensures JVM closes after frame(s) closed and // all non-daemon threads are finished f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // See http://stackoverflow.com/a/7143398/418556 for demo. f.setLocationByPlatform(true); // ensures the frame is the minimum size it needs to be // in order display the components within it f.setResizable(false); f.pack(); // should be done last, to avoid flickering, moving, // resizing artifacts. f.setVisible(true); } }; SwingUtilities.invokeLater(r); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload