Change the gap between components in FlowLayout in Java

Description

The following code shows how to change the gap between components in FlowLayout.

Example


import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//from   w  w  w  .  ja  va 2s  . c  o  m
public class Main {
  public static void main(String[] args) {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 30));
    JTextArea area = new JTextArea("text area");
    area.setPreferredSize(new Dimension(100, 100));

    JButton button = new JButton("button");
    panel.add(button);


    panel.add(new JScrollPane(area));

    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}

The code above generates the following result.

Change the gap between components in FlowLayout in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer