Question

I have, the same issue with two components JTextField and JComboBox, I assume the solution I'm looking for would solve it for all components.

I have set the size of the components to default thus their size fits the initial content I supplied to it. when I change the content of a component to exceeds the region of the component, I cannot see the whole text, and I would like my component to resize to fit the text.

How can I accomplish that?

Update:

The pack() on the frame only enlarged the text field, how can I do the same and enlarge the combo box?

Update:

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    private static final long serialVersionUID = 752379460716217273L;
    Dimension maxSize=new Dimension();
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());
        Dimension size = getPreferredSize();
        if(maxSize.width<size.width)
            maxSize.width=size.width;
        if(maxSize.height<size.height)
            maxSize.height=size.height;

        resolutionDescriptor_ComboBox.setPreferredSize(maxSize);
        return this;
    }

}

this works, not very efficient, but it is a first step, thing is, it does not take the button image into size considerations, so some of the text is still not shown, but the component resizes, do you have any suggestions?

Adam.

Answer:

This did the trick together with a pack(), no revalidation needed.

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    private static final long serialVersionUID = 752379460716217273L;
    Dimension maxSize=new Dimension();
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());
        Dimension size = getPreferredSize();
        if(maxSize.width<size.width) {
            maxSize.width=size.width;
            resolutionDescriptor_ComboBox.setPrototypeDisplayValue(value.toString());
        }
        if(maxSize.height<size.height)
            maxSize.height=size.height;

        return this;
    }

}

make sure you design something more efficient then this...

Update:

and there is no need for the pack()!

Adam.

Was it helpful?

Solution

JComboBox has setPrototypeDisplayValue(Object) method that is used to calculate component's preferred width based on the length of the parameter. Try that.

And instead of pack() use doLayout() together with some revalidate() or repaint()

OTHER TIPS

Do a pack() on the frame

To resize the combo box you can try:

comboBox.setModel( comboBox.getModel() );

I believe this should cause the preferred size of the combo box to be recalculated. Of course you would then need to do the pack() again.

Edit:

Added a simple SSCCE that shows this works:

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

public class ComboBoxTest3 extends JFrame implements ActionListener
{
    JComboBox comboBox;
    JTextField textField;

    public ComboBoxTest3()
    {
        String[] tabs = {"one", "two", "three", "four", "five", "six", "seven" };
        DefaultComboBoxModel model = new DefaultComboBoxModel(tabs);
        comboBox = new JComboBox( model );

        textField = new JTextField("hello");

        add(comboBox, BorderLayout.WEST );
        add(textField, BorderLayout.EAST );

        JButton button = new JButton("Pack");
        button.addActionListener( this );
        add(button, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e)
    {
        textField.setText("hello there!");
        comboBox.addItem("some longer text");
        comboBox.setModel( comboBox.getModel() );
        pack();
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxTest3();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
     }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top