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.