开发者

Creating a text field that dynamically resizes its width

开发者 https://www.devze.com 2023-02-22 11:30 出处:网络
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 t

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 开发者_C百科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!


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);

            }
        });
    }
}


it's a two part problem:

a) all xxSize (xx = min/max/pref) are mere hints to the LayoutManager. Each and every LayoutManager has its own behaviour if/how/when it respects those hints and if/how/when it violates one or more if the overall space is more or less than would fit the sum of pref for all components in the container. No way but learn which does what (documentation is ... ehem ... suboptimal)

b) JTextField is a bit crazy in allowing its height to grow indefinitely if space allows. Implying that even LayoutManagers which respect max (like f.i. BoxLayout) have no chance to do so, max is Integer.MAX_VALUE (or Short.MAX? forgot). To make it behave, subclass and override maxSize to return the pref height:

@Override
Dimension getMaximumSize() {
   Dimension max = super.getMaximumSize();
   max.height = getPreferredSize().height; 
   return max;
}


You should be able to use a horizontal BoxLayout.

searchField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25)); 

You should not guess at the height of the text field.

Dimension d = searchField.getPreferredSize();
d.width = Integer.MAX_VALUE;
searchField.setMaximumSize(d);

I know you said you don't want to use 3rd party libraries, but as a side note you might want to look at Text Field Prompt. It allows you to display a message that will disapear when you start typing in the text field.


Using BoxLayout.

JTextField search_field = new JTextField("Enter search term");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(search_field);

This will dynamically change the size of textfield when resizing the frame.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号