Why JComboBox ignore PrototypeDisplayValue

会有一股神秘感。 提交于 2019-11-28 01:17:30

I debugged your SSCCE, and the value passed to setPrototypeDisplayValue is the empty string. Changing the line to

cbox.setPrototypeDisplayValue(cbox.getSelectedItem());

Makes everything work as expected. Removing the call to setPrototypDisplayValue also makes the program behave as expected.

EDIT:

The other problem is that no event is fired for the prototype display value because you set it to the previous value as before, and an event is only fired if the value actually changes. Adding cbox.setPrototypeDisplayValue(""); before cbox.setPrototypeDisplayValue(cbox.getSelectedItem().toString()) makes everything behave as expected, even on JDK 1.6. I agree that given that the font is changed, the preferred size should be recomputed, but at least this change is a workaround.

I tried what JB Nizet said. But for me the comboBox size didnt change. How about you?

So i tried the following and the combobox size increased as i increased the font size.

cbox.setFont(font);
cbox.updateUI();

I also removed the line

cbox.setPrototypeDisplayValue(text.getText()); 

For reference, a GridLayout and eight clicks gave this result on Mac OS X:

panel.setLayout(new GridLayout(0, 1, 10, 10));

Combo:

Popup:

As an aside, cbox.updateUI() restored the defaults prescribed by the Aqua UI delegate, com.apple.laf.AquaComboBoxUI.

Here is the code from the BasicComboBoxUI:

        else if ( propertyName == "font" ) {
            listBox.setFont( comboBox.getFont() );
            if ( editor != null ) {
                editor.setFont( comboBox.getFont() );
            }
            isMinimumSizeDirty = true;
            comboBox.validate();
        }
        else if ( propertyName == JComponent.TOOL_TIP_TEXT_KEY ) {
            updateToolTipTextForChildren();
    }
        else if ( propertyName == BasicComboBoxUI.IS_TABLE_CELL_EDITOR ) {
            Boolean inTable = (Boolean)e.getNewValue();
    isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false;
        }
    else if (propertyName == "prototypeDisplayValue") {
            isMinimumSizeDirty = true;
            isDisplaySizeDirty = true;
            comboBox.revalidate();
        }

For some reason a Font change only resets the "minimum size" not the "display size".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!