2012-01-10 12 views
6

こんにちは、JComboBoxをJTableに、String []をJComboBoxに配置しても問題ありません。同じカラムの値を選択するJComboBoxにあなた自身のデータ型を入れた場合、Bufは複雑になります。ここにはofficial exampleがあります。あなたは魔女で表のセルをクリックしたときJComboBoxがあることを、あなたが表示されますJTable、カスタムオブジェクトを使用するJComboBox

public class Test { 
    private String name; 

    public Test(String name) { 
     this.name = name; 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 
} 

JComboBox comboBox = new JComboBox(); 
comboBox.addItem("Snowboarding"); 
comboBox.addItem("Rowing"); 
comboBox.addItem("Knitting"); 
comboBox.addItem("Speed reading"); 
comboBox.addItem("Pool"); 
comboBox.addItem("None of the above"); 
sportColumn.setCellEditor(new DefaultCellEditor(comboBox)); 

中に、:次の一部を変更してみてください

JComboBox comboBox = new JComboBox(); 
comboBox.addItem(new Test("Snowboarding")); 
comboBox.addItem(new Test("Rowing")); 
comboBox.addItem(new Test("Knitting")); 
comboBox.addItem(new Test("Speed reading")); 
comboBox.addItem(new Test("Pool")); 
comboBox.addItem(new Test("None of the above")); 
sportColumn.setCellEditor(new DefaultCellEditor(comboBox)); 

また、新しいデータ型を作成カスタムデータ型。最初の列セルの値が自動的に選択されます。この問題を解決するには?

EDIT 1: SSCCEを追加しました。

メインクラス:

import java.awt.BorderLayout; 

public class windw extends JFrame { 

    private JPanel contentPane; 
    private JTable table; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        windw frame = new windw(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public windw() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 

     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 

     table = new JTable(); 
     String[] grupes2 = new String[3]; 
     grupes2[0] = "first"; 
     grupes2[1] = "second"; 
     grupes2[2] = "third"; 

     table.setModel(new DefaultTableModel(
      new Object[][] { 
       {new JComboBox<String>(grupes2)}, 
       {new JComboBox<String>(grupes2)}, 
       {new JComboBox<String>(grupes2)}, 
       {new JComboBox<String>(grupes2)}, 
       {new JComboBox<String>(grupes2)}, 
       {new JComboBox<String>(grupes2)}, 
       {new JComboBox<String>(grupes2)}, 
      }, 
      new String[] { 
       "Column name" 
      } 
     )); 
     TableColumn sportColumn = table.getColumnModel().getColumn(0); 
     sportColumn.setCellEditor(new DefaultCellEditor(new JComboBox<String>(grupes2))); 
     sportColumn.setCellRenderer(new Renderer(grupes2)); 
     contentPane.add(table, BorderLayout.CENTER); 
    } 

} 

レンダラ:

import java.awt.Component; 

import javax.swing.JComboBox; 
import javax.swing.JTable; 
import javax.swing.table.TableCellRenderer; 

public class Renderer extends JComboBox implements TableCellRenderer { 
    public Renderer(String[] items) { 
     super(items); 
    } 

    public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int row, int column) { 
     if (isSelected) { 
      setForeground(table.getSelectionForeground()); 
      super.setBackground(table.getSelectionBackground()); 
     } else { 
      setForeground(table.getForeground()); 
      setBackground(table.getBackground()); 
     } 

     // Select the current value 
     setSelectedItem(value); 
     return this; 
    } 
} 
+1

すぐに役立つように、[SSCCE](http://sscce.org/)を投稿してください。 –

答えて

8

問題は、あなたのTableModelがStringオブジェクトを格納して、コンボボックスは、テストオブジェクトが含まれていることです。これらのオブジェクトは等しくないので、選択する項目がなく、最初のものが自動的に強調表示されます。

は、次のようにコードを変更し

、あなたは未知の文字列と同じ問題が表示されます。

{"Joe", "Brown", new Test("Pool"), new Integer(10), new Boolean(false)} 

{"Joe", "Brown", "Pool?????", new Integer(10), new Boolean(false)} 

は、問題を解決するために、私はあなたが次の操作を行う必要があると思います

次に、両方のコンポーネントのnameプロパティを比較するために、Testクラスにequals()メソッドを実装する必要があります。同様に、hashcode()メソッドを実装する必要があります。

今後Andrewが提案したように、コピー/貼り付け/編集/テストの時間がないので、あなたの質問にSSCCEを含めるようにしてください。

+0

私は間違って尋ねたかもしれませんが、私を助けたのは、 'addRow(new Object [] {}) 'で新しいテーブル行を作成するときでした。以前は、このように 'addRow(new Object [] {" "})'を追加していましたが、それは間違っていました。 – Minutis

+0

ありがとうございました!今、なぜ@ @ Override''がequals()とhashCode()が必要なのか理解しました。 – Minutis

関連する問題