2013-05-09 113 views
5

我希望知道如何更改JComboBox中顯示/選定值的字體。例如。JComboBox的自定義字體

Aharoni(的this answer圖像提供)的上渲染。

我可以用這樣的渲染器更改列表值:

public class JComboBoxItalic extends BasicComboBoxRenderer { 
    protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); 

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
     Font theFont = null; 
     JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected,cellHasFocus); 
     theFont = new Font("Courier", Font.ITALIC, 14); 
     renderer.setFont(theFont); 
     return renderer; 
    } 
} 

但是,當對話框關閉這並不影響選擇的值。有任何想法嗎?

+0

使用Html代替操縱字體類型和大小..只是一個sugegstion。 – ErrorNotFoundException 2013-05-09 12:12:32

+1

@Stanley *「只是一個sugegstion」*不是一個很好的。通過將HTML字符串作爲值,而不是像'Apple'那樣獲取值,我們得到' Apple' - 對API的可怕濫用。 – 2013-05-09 12:13:15

+0

如果每個元素都包含「如何顯示」變量,那麼爲什麼不簡單地改變這些「如何顯示」變量? – Infested 2013-05-09 12:17:27

回答

4
  • 我看不出什麼問題,問題必須在

  • 渲染器僅針對項目中(如果是可見的)彈出窗口應用於您的代碼的其餘部分,

  • 你可以使用基本和最簡單的渲染器

  • 你可以覆蓋所有參數從puramarsters - JList列表,對象值, INT索引,布爾isSelected,布爾cellHasFocus,每個可設置,可測試...,單獨

private class ComboBoxRenderer extends DefaultListCellRenderer { 

    private static final long serialVersionUID = 1L; 
    private Font baseFont = new JLabel("Test").getFont(); 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, 
      int index, boolean isSelected, boolean cellHasFocus) { 

     super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
     //if (value instanceof Font) { 
     // Font font = (Font) value; 
     // setFont(new Font(font.getName(), baseFont.getStyle(), baseFont.getSize())); 
       setFont(new Font("Courier", Font.ITALIC, 14)); 
     //} 
     return this; 
    } 
} 

編輯

我想,要知道如何更改字體在一個JComboBox,

顯示/選擇值 。

enter image description here

enter image description here

enter image description here

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.GraphicsEnvironment; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import javax.swing.DefaultListCellRenderer; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPopupMenu; 
import javax.swing.JScrollPane; 
import javax.swing.JViewport; 
import javax.swing.plaf.basic.BasicComboBoxRenderer; 

public class SystemFontDisplayer extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JComboBox fontsBox; 

    public SystemFontDisplayer() { 

     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     String[] fontFamilyNames = ge.getAvailableFontFamilyNames(); 
     fontsBox = new JComboBox(fontFamilyNames); 
     fontsBox.setSelectedItem(0); 
     fontsBox.setRenderer(new ComboRenderer(fontsBox)); 
     fontsBox.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if (e.getStateChange() == ItemEvent.SELECTED) { 
        final String fontName = fontsBox.getSelectedItem().toString(); 
        fontsBox.setFont(new Font(fontName, Font.PLAIN, 16)); 
       } 
      } 
     }); 
     fontsBox.setSelectedItem(0); 
     fontsBox.getEditor().selectAll(); 
     add(fontsBox, BorderLayout.CENTER); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setPreferredSize(new Dimension(400, 60)); 
     setLocation(200, 105); 
     pack(); 

     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       fontsBox.setPopupVisible(true); 
       fontsBox.setPopupVisible(false); 
      } 
     }); 
     setVisible(true); 
    } 

    public static void main(String arg[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer(); 
      } 
     }); 
    } 

    private class ComboRenderer extends BasicComboBoxRenderer { 

     private static final long serialVersionUID = 1L; 
     private JComboBox comboBox; 
     final DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); 
     private int row; 

     private ComboRenderer(JComboBox fontsBox) { 
      comboBox = fontsBox; 
     } 

     private void manItemInCombo() { 
      if (comboBox.getItemCount() > 0) { 
       final Object comp = comboBox.getUI().getAccessibleChild(comboBox, 0); 
       if ((comp instanceof JPopupMenu)) { 
        final JList list = new JList(comboBox.getModel()); 
        final JPopupMenu popup = (JPopupMenu) comp; 
        final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0); 
        final JViewport viewport = scrollPane.getViewport(); 
        final Rectangle rect = popup.getVisibleRect(); 
        final Point pt = viewport.getViewPosition(); 
        row = list.locationToIndex(pt); 
       } 
      } 
     } 

     @Override 
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
      if (list.getModel().getSize() > 0) { 
       manItemInCombo(); 
      } 
      final JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, row, isSelected, cellHasFocus); 
      final Object fntObj = value; 
      final String fontFamilyName = (String) fntObj; 
      setFont(new Font(fontFamilyName, Font.PLAIN, 16)); 
      return this; 
     } 
    } 
} 
+1

謝謝! fontsBox.setFont(new Font(fontName,Font.PLAIN,16)); 做到了! :D – Grains 2013-05-09 14:05:32

+0

歡迎您 – mKorbel 2013-05-09 18:42:57

+0

這對我無效,它將覆蓋下拉列表中的所有內容。 – 2rs2ts 2014-05-09 17:27:36