views:

213

answers:

3

Hey,

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.

+1  A: 

Do a pack() on the frame

Maurice Perry
it is changing the size of the text field but not the combo box...
TacB0sS
A: 

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

}
camickr
Nope, nothing changed, it stayed the same... I added this before the pack() I tried earlier.Perhaps setting my own renderer and define new size to it according to the largest component in the list... I'll see how this works.
TacB0sS
Works fine for me.
camickr
+1  A: 

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

tulskiy
worked, I had to supply the String as the prototype, Thanks.
TacB0sS
pack() is the proper method to use. You should not use doLayout(). doLayout may increase the size of the components, but the frame size won't change so the components will become truncated.
camickr
there is no need to use the pack or the doLayout, it work fine without them
TacB0sS