当内容动态更改时调整 Swing 组件的大小
我对两个组件 JTextField 和 JComboBox 有同样的问题,我假设我正在寻找的解决方案可以解决所有组件的问题。
我已将组件的大小设置为默认值,因此它们的大小适合我提供给它的初始内容。当我将组件的内容更改为超出组件的区域时,我看不到整个文本,并且我希望调整组件的大小以适合文本。
我怎样才能做到这一点?
更新:
框架上的 pack() 仅放大了文本字段,我怎样才能做同样的事情并放大组合框?
更新:
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;
}
}
这有效,不是很有效,但这是第一步,事情是,它没有考虑按钮图像的大小,所以一些文本仍然没有显示,但组件调整大小,你有什么建议吗?
亚当.
答案:
这与 pack() 一起完成了任务,不需要重新验证。
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;
}
}
确保你设计的东西比这个更有效...
更新:
并且不需要 pack()!
亚当.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JComboBox
有setPrototypeDisplayValue(Object)
方法,用于根据参数的长度计算组件的首选宽度。尝试一下。并使用
doLayout()
和一些revalidate()
或repaint()
来代替pack()
JComboBox
hassetPrototypeDisplayValue(Object)
method that is used to calculate component's preferred width based on the length of the parameter. Try that.And instead of
pack()
usedoLayout()
together with somerevalidate()
orrepaint()
在框架上执行 pack()
Do a pack() on the frame
要调整组合框的大小,您可以尝试:
我相信这应该会导致重新计算组合框的首选大小。当然,您需要再次执行 pack() 。
编辑:
添加了一个简单的 SSCCE 来显示此功能:
To resize the combo box you can try:
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: