1

With the code below I am making a JPanel with JTextFields and JLabels and adding that panel to another JPanel. How can I adjust the spacing between the JTextFields on infoPanel?

I have tried GridBagLayout and GridLayout with different and undesired results. The way it is right now at least gets them aligned vertically, but I can't seem to add space above and below them. Any gurus on this subject able to help out?

public DrawPanelMain() {
    JPanel btnPanel = new JPanel(); //Creates a new Panel for the buttons
    JPanel infoPanel = new JPanel();
    JPanel fields = new JPanel();

    //Text boxes for infoPanel
    JTextField textField1 = new JTextField(20);
    JTextField textField2 = new JTextField(20);
    JTextField textField3 = new JTextField(20);
    JTextField textField4 = new JTextField(20);
    JTextField textField5 = new JTextField(20);
    JTextField textField6 = new JTextField(20);

    //JLabels for infoPanel
    JLabel jLabel1 = new JLabel("Serial Number: ");
    JLabel jLabel2 = new JLabel("Information: ");
    JLabel jLabel3 = new JLabel("Information: ");
    JLabel jLabel4 = new JLabel("Information: ");
    JLabel jLabel5 = new JLabel("Information: ");
    JLabel jLabel6 = new JLabel("Information: ");

    //These are the buttons that will  be added to the btnPanel
    btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
    btnPanel.add(new JButton(new PushConfigAction("Push Config")));
    btnPanel.add(new JButton(new ActivateAllAction("Activate All")));
    btnPanel.add(new JButton(new DeactivateAllAction("Deactivate All")));

    //Fields that will be added to infoPanel
    fields.add(jLabel1);
    fields.add(textField1);
    fields.add(jLabel2);
    fields.add(textField2);
    fields.add(jLabel3);
    fields.add(textField3);
    fields.add(jLabel4);
    fields.add(textField4);
    fields.add(jLabel5);
    fields.add(textField5);
    fields.add(jLabel6);
    fields.add(textField6);

    //Sets border padding for the infoPanel
    fields.setBorder(new EmptyBorder(20, 20, 0, 20));

    //Draws border for the infoPanel
    infoPanel.setBorder(BorderFactory.createRaisedBevelBorder());

    //Sets layout for the fields panel
    fields.setLayout(new GridLayout(6, 1));

    //Add fields to infoPanel
    infoPanel.add(fields);

    //Add panels to tabbedPane
    setLayout(new BorderLayout());
    add(tabbedPane, BorderLayout.CENTER);
    add(btnPanel, BorderLayout.PAGE_END);
    add(infoPanel, BorderLayout.EAST);

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
feltersnach
  • 406
  • 3
  • 20
  • 2
    Please have a look at this post, regarding [providing whitespaces in a Swing GUI](http://stackoverflow.com/q/17874717/1057230). Hopefully it will be of some help on the topic :-) In simple terms, use the overloaded constructors for each `Layout` concern. – nIcE cOw Jul 19 '15 at 05:54
  • 2
    @nIcEcOw you da man, that's exactly what I have been looking for! – feltersnach Jul 19 '15 at 06:00

1 Answers1

0

create a compound border

field.setBorder(BorderFactory.createCompoundBorder(
        field.getBorder(), 
        BorderFactory.createEmptyBorder(int top, int left, int bottom, int right)));

Or put some insets

field.setMargin(new java.awt.Insets(int top, int left, int bottom, int right));
Rahul
  • 289
  • 2
  • 7