Search code examples
javaswingframejscrollpanegridbaglayout

JScrollPane not properly stretching horizontal distance in GridBagLayout


I've seen other posts on this subject, but the solutions they found do not apply to me. I am setting a weighted value and using the c.fill = GridBagConstraints.BOTH constraints as well.

I'm including the whole GUI code I have, just in case my mistake is coming form something other than the GridBagLayout.

I want the scrollable text block on the right to expand the remaining space within the GUI and I have set all the variables that should be attributed to that and yet it still isn't working. What am I doing wrong?

My result:

GridBagLayout result

import java.awt.*;
import javax.swing.*;

public class TestCode extends JFrame {
    JTextArea textArea = new JTextArea ();
    JComboBox <String> typeComboBox;
    JTextField searchField;
    JTextField fileField;

    public TestCode(){

        setTitle ("GUI Test");
        setSize (600, 300);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setVisible (true);

        JScrollPane scrollPane = new JScrollPane(textArea);

        JButton readButton = new JButton("Read File");
        JButton displayButton = new JButton("Display");
        JButton searchButton = new JButton("Search");


        searchField = new JTextField(10);
        fileField = new JTextField(15);

        typeComboBox = new JComboBox <String> ();
        typeComboBox.addItem("Index");
        typeComboBox.addItem("Type");
        typeComboBox.addItem("Name");




        JPanel container = new JPanel();
            container.setLayout(new GridBagLayout());
            container.setPreferredSize(new Dimension(250, 100));
        JPanel filePanel = new JPanel();
            filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
            filePanel.add(new JLabel("Source file", SwingConstants.LEFT));
                JPanel filePanelTop = new JPanel();
                    filePanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
                    filePanelTop.add(fileField);
                JPanel filePanelBottom = new JPanel();
                    filePanelBottom.setLayout(new FlowLayout(FlowLayout.RIGHT));
                    filePanelBottom.add(readButton);
                    filePanelBottom.add(displayButton);
            filePanel.add(filePanelTop);
            filePanel.add(filePanelBottom);
            filePanel.setMaximumSize(filePanel.getPreferredSize());
            filePanel.setBorder(BorderFactory.createTitledBorder("Import File"));
        JPanel searchPanel = new JPanel(); 
            searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS));
            searchPanel.add(new JLabel("Search target", SwingConstants.LEFT));
                JPanel searchPanelTop = new JPanel();
                    searchPanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
                    searchPanelTop.add(searchField);
                    searchPanelTop.add(typeComboBox);
            searchPanel.add(searchPanelTop);
            searchPanel.add(searchButton);
            searchPanel.setMaximumSize(searchPanel.getPreferredSize());
            searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects"));

          GridBagConstraints c = new GridBagConstraints();
          c.gridx = 0;
          c.gridy = 0;
          container.add(filePanel, c);
          c.gridx = 0;
          c.gridy = 1;
          container.add(searchPanel, c);
          c.gridx = 1;
          c.gridy = 0;
          c.weightx = 1.0;
          c.weighty = 1.0;
          c.gridwidth = GridBagConstraints.REMAINDER;
          c.gridheight = GridBagConstraints.REMAINDER;
          c.fill = GridBagConstraints.BOTH;
          c.anchor = GridBagConstraints.NORTHWEST;
          container.add(scrollPane, c);

        add(container, BorderLayout.WEST);

        validate();

    } // end method toString

    public static void main(String[] args){
        TestCode run = new TestCode();
    }
} // end class Treasure

Solution

  • //add(container, BorderLayout.WEST);
    add(container);
    

    The West contrains the components to their preferred width. The default is the CENTER which allows components to expand to fill the space available.

    Also, the main structure of you code is wrong. You should be adding all the component to the frame first and then invoke:

    frame.pack();
    frame.setVisible(true);
    

    Then there is no need for the validate().