2012-01-10 72 views
6

嗨,如果你把JComboBox JTable和String []数组到JComboBox一切正常。如果您将自己的数据类型添加到JComboBox,则在同一列中选择值会变得很复杂。这里是official example。尝试改变以下部分:JTable,JComboBox使用自定义对象

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)); 

,并创造新的数据类型:

public class Test { 
    private String name; 

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

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

你会看到,当你在女巫点击表格单元格存在的JComboBox与自定义数据类型。第一列单元格的值被自动选择。如何解决这个问题?

编辑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/)。 – 2012-01-10 14:37:58

回答

8

的问题是,你的TableModel的是存储一个String对象和组合框包含一个测试对象。这些对象不相同,因此没有要选择的项目,并且它看起来第一个会自动突出显示。

你的代码更改为以下,你会看到同样的问题与一个未知的字符串:

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

要解决这个问题,我就猜你需要做到以下几点:

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

然后,您需要在Test类中实现equals()方法来比较两个组件的名称属性。同样,你需要实现hashcode()方法。

未来,正如Andrew建议的那样,将您的SSCCE纳入您的问题中,因为我们没有时间复制/粘贴/编辑和测试代码,因为我们永远不知道我们是否完全按照您的方式进行操作。

+0

也许我问了错,但帮助我的是,当我用'addRow(new Object [] {})'创建新的表格行时“。以前我是这样加入的 - “addRow(new Object [] {”“})'这是错误的。 – Minutis 2012-01-11 11:44:39

+0

谢谢!现在我明白了为什么我需要'@ Override' equals()'和'hashCode()'。 – Minutis 2012-01-18 06:32:39