2009-06-25 16 views
0

私はCustomerのリストをJComboBoxから選択する必要があります。私が読んだところから、カスタムレンダラを実装して、必要なフィールドをリストに表示する必要があります。Java JComboBoxカスタムレンダラーとGTK

私はJComboBoxが、このようなとしてフォーマットのエントリを持つようにしたい:

+----------------------------------------------+ 
| Customer Name - Contact - City, State V | 
+==============================================+ 
| Customer #2 Name - Contact - City, State | 
| Customer #3 Name - Contact - City, State | 
| Customer #4 Name - Contact - City, State | 
| Customer #5 Name - Contact - City, State | 
+----------------------------------------------+ 

私はこのコードを使用:

パブリッククラスCustomerListCellRendererはDefaultListCellRenderer {

@Override 
public Component getListCellRendererComponent(
     JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
    if (value instanceof Customer) { 
     Customer c = (Customer) value; 

     StringBuffer sb = new StringBuffer(); 
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getCompany()); 
     } 
     sb.append(" - "); 
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getContact()); 
     } 
     sb.append(" - "); 
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getCity()); 
      sb.append(", "); 
     }    
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getState()); 
     } 

     setText(sb.toString()); 
    } 
    return this; 
    } 
} 

これは正しく動作しませんを拡張システムGTKLookAndFeelを使用してSolaris/Unix/Linuxの下で実行します。コンボボックスの入力領域の背景は描画されず、周囲には境界線が描画されません。 (下記のスクリーンショットを参照)。 3つの主要なプラットフォーム(Win/Mac/GTK)で正しく動作する別の方法がありますか?これを行うコンバータを実行して、GUIではなくデータのみを操作できますか?

私の現在の回避策は、CustomerオブジェクトのtoString()をオーバーライドして、必要な形式で各レコードを表示し、他のアイデアを探します。

alt text http://i40.tinypic.com/5dw1hd.jpg

ニック

+0

この例でカスタムレンダラーが必要な理由はありません。 –

+1

@ammoQ:Stringを使用するのではなく、ComboBoxModelにCustomerを格納する必要があるため、これが必要です。 これは、getSelectedItem()を実行すると、文字列ではなくCustomerを取得するためです。 –

答えて

2

同じ問題、私は示すアイコンに合わせてカスタマイズするためにこれをしませんでした。

0

DefaultListCellRendererがJLabelのを拡張し、JLabelのように見えます。 ComboBoxを編集できない場合、getRendererを介して返されたレンダラーは、ドロップダウンリスト領域と「入力」領域のペイントに使用されます。 ComboBoxとレンダラーのボーダー/フォアグラウンド/バックグラウンドの設定を試してみてください。

public Component getListCellRendererComponent(
     JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
    if (value instanceof Customer) { 
     Customer c = (Customer) value; 

     StringBuffer sb = new StringBuffer(); 
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getCompany()); 
     } 
     sb.append(" - "); 
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getContact()); 
     } 
     sb.append(" - "); 
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getCity()); 
      sb.append(", "); 
     }    
     if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getState()); 
     } 

     value = sb.toString(); 
    } 
    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
    } 
} 

またStringBuilderのではないのStringBufferを(これはシングルスレッドの状況です)を使用します。

1

はこれを試してみてください。

 if (c.getCompany() != null && c.getCompany().length() > 0) { 
      sb.append(c.getState()); 
     } 

は、当社のメンバーをチェックしている状態のメンバーを使用している:あなたが切断され、例えば、コード内のエラーを貼り付けているよう

もまた、それが見えます。これまでのところ、これは私のために罰金を働いた

comboBox.setRenderer(new CustomComboBoxRenderer(comboBox.getRenderer())); 

private static class CustomComboBoxRenderer extends DefaultListCellRenderer 
{ 
    private final ListCellRenderer backend; 

    public CustomComboBoxRenderer(ListCellRenderer backend) 
    { 
     this.backend = backend; 
    } 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
    { 
     Component component = backend.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
     if(component instanceof JLabel == false) 
      component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
     JLabel label = (JLabel)component; 
     label.setIcon(Icons.COMPONENT); 
     return label; 
    } 
} 

次に、このようにレンダラを割り当て: