Search code examples
javaswingjtextfield

Creating a text field that dynamically resizes its width


I'm making what is basically a toolbar that contains a search field and some buttons. I'd like for the search field to grow in size (just the width) when the parent container gets wider, like when the user adjusts the split pane. Currently, the text field and the buttons will remain the same size and whitespace is added on either side as the container is widened. I can achieve this growing effect by using a BorderLayout in the container and putting the buttons on LINE_END and the text field in the CENTER. The problem I have with this is that the text field now becomes taller than a standard text field and it looks ugly. This behavior makes sense as the BorderLayout manager will give all the extra space (this includes vertical and hortizontal space) to the CENTER text field. I've tried to restrict this vertical growth by placing a maximum size on the text field, but BorderLayout will not honor it.

Here's what I've got:

final JTextField searchField = new JTextField("Enter your search terms");
searchField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
final JPanel controls = new JPanel(new BorderLayout());
controls.add(searchField, BorderLayout.CENTER);
controls.add(new JPanel(){{
    add(new JButton("Search")); add(new JButton("I'm Feeling Lucky"));
}}, BorderLayout.LINE_END);

It would seem that this behavior is a commonly desired one and it should be easy to implement, but I've had no luck after looking through all the Oracle/Sun tutorials and Google search results.

Anybody have any solutions to this? I need to stick with standard Java Swing components - no third party libraries please.

Thank you!


Solution

  • I would suggest you to use GridBagLayout , it is complicated but it is the most powerful layout.. When you learn it, you would not have any layout issue.

    Here is the sample use of gridbaglayout for this question...

    import java.awt.BorderLayout;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class GridBagLayoutExample extends JFrame {
    
        GridBagLayoutExample() {
            initUI();
        }
    
    
        private void initUI() {
            final JTextField searchField = new JTextField("Enter your search terms"); 
            final JPanel controls = new JPanel(new GridBagLayout()); 
            GridBagConstraints c = new GridBagConstraints();
            c.weightx=1.0;
            c.fill=GridBagConstraints.HORIZONTAL;
            controls.add(searchField,c); 
            controls.add(new JPanel(){
                {     
                add(new JButton("Search")); add(new JButton("I'm Feeling Lucky")); }}); 
            setLayout(new BorderLayout());
            add(controls, BorderLayout.NORTH);
            pack();
        }
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new GridBagLayoutExample().setVisible(true);
    
                }
            });
        }
    }