2009-06-01 6 views

答えて

1

あなたはおそらく、ここにSunのチュートリアルをチェックアウトし、あなたのJComboBoxのためのカスタムレンダラを提供する必要があります:

http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer

を(リンクの不足のため申し訳ありませんが、リンクを投稿し、まだ私ので、することはできません

class Renderer extends JLabel implements ListCellRenderer { 

と、このメソッドを実装します:「あなたのようなカスタムListCellRendererを作成する必要が

2

)新しいメンバーをm個

public Component getListCellRendererComponent(JList list, Object value, 
      int index, boolean isSelected, boolean cellHasFocus) { 
     // Get the selected index. (The index param isn't 
     // always valid, so just use the value.) 

     if (isSelected) { 
      setBackground(list.getSelectionBackground()); 
      setForeground(list.getSelectionForeground()); 
     } else { 
      setBackground(list.getBackground()); 
      setForeground(list.getForeground()); 
     } 

     // Display the text 
     String text = (String) value; 
     setText(text); 

     // Get the source 

次に、ソースに応じて、this.setForeground(色の色)を使用してテキストの色を設定します。最後に、

return this; 

}

1

あなたはListCellRendererを使用することができます。このためにカスタムクラスを作成する必要があります。ここでは、重複を避けるために、インデックスに基づいてフォアグラウンドを設定する完全なコードを示します。このためにカスタム選択の背景と背景を設定することもできます。コード内のコメントを参照してください。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
class ListCellRendererDemo2 extends JFrame 
{ 
Hashtable<Integer,Color> table; 
JComboBox<String> c; 

    public ListCellRendererDemo2() 
    { 
     createAndShowGUI(); 
    } 

    private void createAndShowGUI() 
    { 
     setTitle("JComboBox Demo"); 
     setLayout(new FlowLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     table=new Hashtable<Integer,Color>(); 
     table.put(1,Color.RED); 
     table.put(2,Color.BLUE); 
     table.put(3,Color.GREEN); 
     table.put(4,Color.GRAY); 


     c=new JComboBox<String>(); 
     c.addItem("Item 1"); 
     c.addItem("Item 2"); 
     c.addItem("Item 3"); 
     c.addItem("Item 4"); 
     c.addItem("Item 5"); 
     c.addItem("Item 6"); 
     c.addItem("Item 7"); 
     c.addItem("Item 8"); 

     c.setRenderer(new MyListCellRenderer(table)); 

     add(c); 
     setSize(400,400); 
     setVisible(true); 
    } 

    public static void main(String args[]) 
    { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() 
      { 
       new ListCellRendererDemo2(); 
      } 
     }); 
    } 
} 
class MyListCellRenderer extends DefaultListCellRenderer 
{ 
Hashtable<Integer,Color> table; 

    public MyListCellRenderer(Hashtable<Integer,Color> table) 
    { 
     this.table=table; 

     // Set opaque for the background to be visible 
     setOpaque(true); 
    } 

    public Component getListCellRendererComponent(JList jc,Object val,int idx,boolean isSelected,boolean cellHasFocus) 
    { 
     // Set text (mandatory) 
     setText(val.toString()); 

     // Set the foreground according to the selected index 
     setForeground(table.get(idx)); 

      // Set your custom selection background, background 
      // Or you can get them as parameters as you got the table 
      if(isSelected) setBackground(Color.LIGHT_GRAY); 
      else setBackground(Color.WHITE); 

    return this; 
    } 
} 
+0

何か古いスレッドはありません、ゾンビを元気づける理由はありますか? – mKorbel

関連する問題