为什么JComboBox忽略PrototypeDisplayValue [英] Why JComboBox ignore PrototypeDisplayValue

查看:191
本文介绍了为什么JComboBox忽略PrototypeDisplayValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与这两个帖子@iMohammad的联系中,
使用JButton
增加/减少textArea内的字体大小https://stackoverflow.com/questions/8679088/changing-font-style-when-clicking-on-a-jbutton当我点击一个JButton Java的时候改变字体样式 ...,我正在面对来自 JComboBox 通过传递<$

请将c $ c> setPrototypeDisplayValue 作为 JComboBox大小的参数我怎样才能调整大小 JComboBox 动态依赖于字体,同样适用于我在 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);
私人JTextField文本;
private JPanel panel = new JPanel();
$ b $ 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(){
$ b $ @Override
public void actionPerformed(ActionEvent e){
Font font = cbox.getFont();
$ b / font.deriveFont((float)(font.getSize2D()* 1.10));
cbox.setFont(font);
//编辑
cbox.setPrototypeDisplayValue(cbox。 getSelectedItem()。toString());
tfield.setFont(font);
button.setFont(font);
label.setFont(font);
//面板。 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(){
$ b $ @Override
)public void run(){
ComboBoxFontChange frame = new ComboBoxFontChange();
frame.pack();
frame.setVisible(true);
}
} );



$ div $解析方案

调试了您的SSCCE,并且传递给 setPrototypeDisplayValue 的值是空字符串。将行更改为

  cbox.setPrototypeDisplayValue(cbox.getSelectedItem()); 

使一切按预期工作。删除对 setPrototypDisplayValue 的调用也会使程序按预期工作。



编辑:



另一个问题是,没有事件被激发,因为原型显示值与之前一样设置为之前的值,并且只有当值实际发生变化时才触发事件。在 cbox.setPrototypeDisplayValue(cbox.getSelectedItem()。toString())之前添加 cbox.setPrototypeDisplayValue(); 使得所有的行为都像预期的那样,即使在JDK 1.6上。我同意,鉴于字体已更改,首选大小应重新计算,但至少此更改是一种解决方法。


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 JComboBoxby 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);
            }
        });
    }
}

解决方案

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.

这篇关于为什么JComboBox忽略PrototypeDisplayValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆