In connections with these two post @iMohammad,
Increasing/Decreasing Font Size inside textArea using JButton and
Changing Font Style when Clicking on a JButton Java ..., I'm facing with really funny issue that came from JComboBox
by passing setPrototypeDisplayValue
as an argument for JComboBox's size
on the screen
please how can I resize JComboBox
dynamically depends of Font
, same as works correctly for another JComponents that I tried in my 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);
}
});
}
}
Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box. Initializes the editor with the specified item. Sets the properties on this combobox to match those in the specified Action . This method is public as an implementation side effect.
JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer . Constructor of the JComboBox are: JComboBox() : creates a new empty JComboBox .
Only one item at a time can be selected in a combo box, so when the user makes a new selection the previously selected item becomes unselected.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With