简体   繁体   English

JGoodies FormLayout拒绝调整我的JComboBox的大小

[英]JGoodies FormLayout refuses to resize my JComboBox

It seems that the JComboBox is one Java component that really, really hates having its height adjusted...I tried countless combinations of set[Preferred|Minimum|Maximum]Size() and various different layout managers until the following GroupLayout code finally worked: 似乎JComboBox是一个Java组件, 确实非常讨厌调整其高度...我尝试了set[Preferred|Minimum|Maximum]Size()和各种不同的布局管理器的无数组合,直到以下GroupLayout代码最终起作用:

JComboBox cmbCategories = new JComboBox(new String[] { "Category 1", "Category 2" });
...
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addComponent(cmbCategories, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
...
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addComponent(cmbCategories, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)

But I've now switched over to JGoodies FormLayout which once again refuses to resize the damn combobox! 但是我现在切换到了JGoodies FormLayout ,它再次拒绝调整该死的组合框的大小! I've currently got the following code: 我目前有以下代码:

JPanel contentPane = new JPanel();
contentPane.setLayout(new FormLayout("50dlu, $lcgap, 110dlu, $glue, " +
    "default, 1dlu, 45dlu, 1dlu, 45dlu", "2*(default, 0dlu), default, " +
    "$lgap, fill:30dlu, $lgap, default:grow"));
...
contentPane.add(cmbPanel, CC.xy(1, 7, CC.FILL, CC.FILL));

which displays what I want in the JFormDesigner Editor, but upon running the program it simply gets set back to the default! 它在JFormDesigner编辑器中显示我想要的内容,但是在运行该程序时,它只是被设置回默认值!

So what kind of magical hocus-pocus do I need to conjure up to get this to work?! 那么,我需要想像一下什么样的魔法轨迹才能使它起作用?! I really don't want to have to go back to defining everything twice in a GroupLayout , but after 5 hours of trying to resize a damn combobox I'm on the verge of baldness! 我真的不想回到在GroupLayout两次定义所有内容的GroupLayout ,但是在尝试调整可恶组合框的大小5个小时后,我濒临秃顶!

MTIA to anyone that can help :) MTIA对任何可以帮助的人:)

First of all we have to avoid setting hard-coded sizes in our components, because Swing is designed to be used with Layout Managers and our application must be capable to be executed in different platforms, different screen resolutions, different PLaF 's and different font sizes. 首先,我们必须避免在组件中设置硬编码的大小,因为Swing设计为与布局管理器一起使用,并且我们的应用程序必须能够在不同的平台,不同的屏幕分辨率,不同的PLaF和不同的字体下执行。大小。 Components sizing and positioning is layout managers' responsibility not developers'. 组件的大小和位置是布局经理的责任,而不是开发人员的责任。

Now, generally speaking when we want to set a preferred size to our Swing components we don't use any of setXxxSize() methods but do override getPreferredSize() method instead: 现在,通常来说,当我们想为Swing组件设置首选大小时,我们不使用任何setXxxSize()方法,而是改写getPreferredSize()方法:

JComboBox comboBox = new JComboBox() {
    @Override
    public Dimension getPreferredSize() {
        return isPreferredSizeSet() ? 
                super.getPreferredSize() : new Dimension(100, 40);
    }
};

However doing this doesn´t affect the size of the items listed when the pop up becomes visible: cells still having the preferred size determined by the combo box cell renderer . 但是,这样做不会影响弹出窗口可见时列出的项目的大小:仍具有由组合框单元格渲染器确定的首选大小的单元格 So, to avoid this undesirable behavior, a better solution would be: 因此,为避免这种不良行为,更好的解决方案是:

For example: 例如:

JComboBox comboBox = new JComboBox();
comboBox.setPrototypeDisplayValue("This is a cell's prototype text");
comboBox.setRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        int width = c.getPreferredSize().width; // let the preferred width based on prototype value
        int height = 40;
        c.setPreferredSize(new Dimension(width, height));
        return c;
    }
});

Once again I'd like to emphasize that this is kind of hacky/dirty way to size our combo box. 我想再次强调一下,这是一种用于组合框大小的方法。 IMHO it would be better don't mess with combo box height at all and just play with setPrototypeDisplayValue(...) to set a preferred width in a PLaF safe way. 恕我直言,最好不要弄乱组合框的高度,而只是使用setPrototypeDisplayValue(...)来以PLaF安全的方式设置首选宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM