Why JComboBox ignores PrototypeDisplayValue

In connection with these two posts by @iMohammad, Increasing / decreasing the font size inside textArea using JButton and Changing the font style when clicking on JButton Java ... I ran into a really funny problem that came from JComboBox, passing setPrototypeDisplayValueas an argument to JComboBox sizethe screen

please, how to resize JComboBoxdynamically depends on Font, just as it works correctly for other JComponents that I tried to use in sscce

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxFontChange extends JFrame {

    private static final long serialVersionUID = 1L;
    private JComboBox cbox = new JComboBox();
    private JTextField tfield = new JTextField("Change");
    private JLabel label = new JLabel("Cash");
    private JButton button = new JButton("++ Font");
    private JTextField text;
    private JPanel panel = new JPanel();

    public ComboBoxFontChange() {
        super("Combo Box Font change");
        text = (JTextField) cbox.getEditor().getEditorComponent();
        cbox.addItem("Change");
        cbox.addItem("Cash");
        cbox.addItem("Font");
        tfield.setColumns(5);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Font font = cbox.getFont();
                font = font.deriveFont((float) (font.getSize2D() * 1.10));
                cbox.setFont(font);
                // EDIT
                cbox.setPrototypeDisplayValue(cbox.getSelectedItem().toString());
                tfield.setFont(font);
                button.setFont(font);
                label.setFont(font);
                //panel.revalidate();
                //panel.repaint();
                pack();
            }
        });
        //panel.setLayout(new GridLayout(2, 2, 10, 10));
        panel.add(cbox);
        panel.add(label);
        panel.add(tfield);
        panel.add(button);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(panel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComboBoxFontChange frame = new ComboBoxFontChange();
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
+5
source share
4 answers

SSCCE, , setPrototypeDisplayValue, - .

cbox.setPrototypeDisplayValue(cbox.getSelectedItem());

, . setPrototypDisplayValue .

EDIT:

, , , , , . cbox.setPrototypeDisplayValue(""); cbox.setPrototypeDisplayValue(cbox.getSelectedItem().toString()) , , JDK 1.6. , , .

+4

, . . . comboBox . ?

, , combobox , .

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

cbox.setPrototypeDisplayValue(text.getText()); 
+1

, GridLayout Mac OS X:

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

: combo : popup

cbox.updateUI() , Aqua UI, com.apple.laf.AquaComboBoxUI.

+1
source

Here is the code from 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, changing the font only resets the "minimum size" and not the "screen size".

+1
source

All Articles