0

I'm trying to lay out a basic JPanel using JGoodies.FormLayout and I'm running into an issue where the JTextField components I place remain at minimum size regardless of anything I do to attempt to resolve the issue. Without further ado,

Program.java:

package example;

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import example.gui.*;

public class Program {

    private static JTabbedPane tabbedViewControl;
    private static TabPanel someTabPanel;

    private static void createAndShowGUI() {
        JFrame mainFrame = new JFrame("Stack Overflow Example");
        mainFrame.setResizable(true);
        mainFrame.setSize(1200, 800);
        mainFrame.setTitle("Example");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainFrame.getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
        tabbedViewControl = new JTabbedPane(JTabbedPane.TOP);
        someTabPanel = new TabPanel();
        tabbedViewControl.addTab("Example Tab", someTabPanel);
        mainFrame.getContentPane().add(tabbedViewControl);

        //Display the window.
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }
}

example.gui.TabPanel.java:

package example.gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class TabPanel extends JPanel {

    private static Border       textFieldBorderStyle;
    private static ButtonGroup  userChoiceButtonGroup;
    private static PanelBuilder builder;
    private static FormLayout   layout;
    private static JLabel       descriptionLabel;
    private static JLabel       userChoiceLabel;
    private static JLabel       nameLabel;
    private static JLabel       data2Label;
    private static JLabel       data1Label;
    private static JRadioButton userChoice1RadioButton;
    private static JRadioButton userChoice3RadioButton;
    private static JRadioButton userChoice4RadioButton;
    private static JRadioButton userChoice2RadioButton;
    private static JRadioButton userChoice5RadioButton;
    private static JTextArea    descriptionField;
    private static JTextField   nameField;
    private static JTextField   data2Field;
    private static JTextField   data1Field;
    private static JPanel       subPanel;

    public TabPanel() {
        super(new GridBagLayout());
        initComponents();
    }

    private void initComponents() {
        nameLabel               = new JLabel("Name");
        nameField               = new JTextField();
        descriptionLabel        = new JLabel("Description");
        descriptionField        = new JTextArea("");
        data1Label             = new JLabel("Data No. 1");
        data1Field             = new JTextField();
        data2Label           = new JLabel("Data No. 2");
        data2Field           = new JTextField();
        userChoiceLabel        = new JLabel("User choices");
        userChoiceButtonGroup  = new ButtonGroup();
        userChoice1RadioButton     = new JRadioButton("Choice I");
        userChoice3RadioButton     = new JRadioButton("Choice II");
        userChoice4RadioButton     = new JRadioButton("Choice III");
        userChoice2RadioButton = new JRadioButton("Choice IV");
        userChoice5RadioButton = new JRadioButton("Choice V");

        subPanel = constructPanel();
        this.add(subPanel);
    }

    private JPanel constructPanel() {
        layout = new FormLayout(
            "r:p, 3dlu, l:max(1in;p), 7dlu, r:p, 3dlu, l:p, 3dlu, l:p, 3dlu, l:p",
            "p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 3dlu, p"
        );
        builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();

        textFieldBorderStyle = nameField.getBorder();
        descriptionField.setBorder(textFieldBorderStyle);

        userChoiceButtonGroup.add(userChoice1RadioButton);
        userChoiceButtonGroup.add(userChoice3RadioButton);
        userChoiceButtonGroup.add(userChoice4RadioButton);
        userChoiceButtonGroup.add(userChoice2RadioButton);
        userChoiceButtonGroup.add(userChoice5RadioButton);

        builder.addSeparator("Name & Description", cc.xyw(1, 1, 3));
        builder.add(nameLabel,        cc.xy  (1, 3));
        builder.add(nameField,        cc.xy  (3, 3));
        builder.add(descriptionLabel, cc.xy  (1, 5));
        builder.add(descriptionField, cc.xywh(3, 5, 1, 4));
        builder.addSeparator("Other Data for the User to Enter", cc.xyw(5, 1, 5));
        builder.add(data1Label,             cc.xy(5,  3));
        builder.add(data1Field,             cc.xy(7,  3));
        builder.add(data2Label,           cc.xy(5,  5));
        builder.add(data2Field,           cc.xy(7,  5));
        builder.add(userChoiceLabel,        cc.xy(5,  7));
        builder.add(userChoice2RadioButton, cc.xy(7,  7));
        builder.add(userChoice5RadioButton, cc.xy(7,  9));
        builder.add(userChoice3RadioButton,     cc.xy(7, 11));
        builder.add(userChoice4RadioButton,     cc.xy(9,  7));
        builder.add(userChoice1RadioButton,     cc.xy(9,  9));

        return builder.getPanel();
    }
}

The following is the result:

Unusual JTextField behavior

Ideally, I'd like for three things to occur, rather than above:

  1. The JPanel returned from PanelBuilder to occupy the full dimensions of its parent
  2. The JTextFields and JTextArea to be of reasonable width (and not resize*)
  3. The two aspects to be more proportional - I'm not expecting 50%/50%, but I don't want the right side to squash the left side up against the border

I've done a fair amount of homework on this, but mostly came up with the following non-results:

  1. Attempting to search on SO mainly yields issues with "native" AWT layout systems
  2. Going through the JGoodies Forms whitepaper, while giving me the use of max(constantSize;preferredSize), only assisted in the resizing of the separator, and did nothing re: the JTextFields

So, in short, is there a way to force JTextFields to occupy the given width of their column in a FormLayout?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ecfedele
  • 306
  • 3
  • 15

1 Answers1

-1

here is the thing, it is better to use borders to put components in,

//create title border so later on you can set it to your panel
TitledBorder topBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Title message To the border");// you can use more or border type in BorderFactory.class

then you put

// initialize the frame layout 
frame.setLayout(new BorderLayout(20, 30));

and foreach borders you can define as.

//create panel and set panel layout depend on whatever you want to choose
//then set panels border to that TitledBorder you have created before
JPanel topPanel= new JPanel();
topPanel.setLayout(new GridLayout(2, 3, 10, 10)); // grid layout can be managed easier and better 
topPanel.setBorder(topBorder); // you have defined topBorder before

and finally

// initialize textfiled and add it to toppanel 
JTextField yourFiled = new JTextField();
topPanel.add(messageLabel);

you can define all the properties to the fields in JTextFiled that you want as well as you can skip first step to create TitledBorder but i always do bcause it is better and more organized

Mohammed Fataka
  • 150
  • 1
  • 8