我正在使用JComboBox
和自定义ListCellRenderer
制作字体选择器。我想 JComboBox
显示所有可用的字体,每个字体名称显示在其自己的字体中。我目前使用大约500种字体。如何防止使用自定义ListCellRenderer时JComboBox无响应
ListCellRenerer
一个提供此功能的一个例子:
private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
private JLabel label = new JLabel("Test");
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Font tempFont = label.getFont();
setFont(new Font((String) value, tempFont.getStyle(),
tempFont.getSize()));
setText((String) value);
return this;
}
}
的问题是,使用此渲染器的情况下,变得JComboBox
程序执行过程中没有反应。第一次点击组合框来显示列表时,需要几秒钟才能加载列表。第二次点击时,该列表即时显示。
如果一个注释行
setFont(new Font((String) value, tempFont.getStyle(),tempFont.getSize()));
,组合框的工作就好了。
如何防止这种无反应?
您可能想尝试创建一个'字体'对象的缓存。用于在90年代缓存'Font'和'FontMetric'。你可以为每个'Font'创建一个'JLabel'。 – 2011-05-05 10:45:48
但用测试'if(isSelected){'或'cellHasFocus' – mKorbel 2011-05-05 10:45:50
只注意到你说它只是第一次慢,而且你正在使用500字体。我猜加载500个字体是一项相当艰巨的任务。 – 2011-05-05 10:49:02