2

I have the structure as you can see in the picture.

For both panels, GridBagLayout is used. The problem is that the text field inside the scrollpane is not entirely visible. The parent panel stretches only for the buttons to become visible, but when the scroll bar appears, it just overlap with the text field. Is there an easy solution to fix this (don't want to deal with setting custom / preferred / minimum heights)?

Panel structure :

enter image description here

Problem :

enter image description here

Ok, here is an SSCCE

public class Main {
JFrame frame;
private JPanel mainPanel;
private JButton button1;
private JButton button2;
private JTextField someTextTextField;


public static void main(String[] args) {
    Main main = new Main();
    main.show();
}

private void show() {
    frame = new JFrame("Frame");
    frame.setContentPane(mainPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();

    frame.setVisible(true);
}


{

    $$$setupUI$$$();
}

/**
 * Method generated by IntelliJ IDEA GUI Designer
 * >>> IMPORTANT!! <<<
 * DO NOT edit this method OR call it in your code!
 *
 * @noinspection ALL
 */
private void $$$setupUI$$$() {
    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());
    mainPanel.setPreferredSize(new Dimension(209, 30));
    final JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    GridBagConstraints gbc;
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weightx = 0.5;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    mainPanel.add(panel1, gbc);
    button1 = new JButton();
    button1.setText("Button");
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel1.add(button1, gbc);
    button2 = new JButton();
    button2.setText("Button");
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel1.add(button2, gbc);
    final JScrollPane scrollPane1 = new JScrollPane();
    scrollPane1.setAlignmentX(0.0f);
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 0.5;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    mainPanel.add(scrollPane1, gbc);
    someTextTextField = new JTextField();
    someTextTextField.setText("some text");
    scrollPane1.setViewportView(someTextTextField);
}

/**
 * @noinspection ALL
 */
public JComponent $$$getRootComponent$$$() {
    return mainPanel;
}

}

Bax
  • 4,260
  • 5
  • 43
  • 65

1 Answers1

1

Try and use a JTextArea instead of a JTextField. And if you don't want to set custom/preferred/minimum sizes, you can use the JTextArea.setRows(int rows) method. I hope that helps.

athspk
  • 6,722
  • 7
  • 37
  • 51
Bill Tsagkas
  • 530
  • 1
  • 4
  • 15