2012-01-10 64 views
6

Xin chào nếu bạn đặt JComboBox trong mảng JTable và String [] cho JComboBox mọi thứ hoạt động tốt. Buf nếu bạn đặt kiểu dữ liệu của riêng bạn để JComboBox chọn các giá trị trong cùng một cột trở nên phức tạp. Đây là official example. Hãy thử thay đổi sau phần:JTable, JComboBox sử dụng các đối tượng tùy chỉnh

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

Into:

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

Và tạo kiểu mới dữ liệu:

public class Test { 
    private String name; 

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

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

Bạn sẽ thấy, đó là khi bạn click vào ô trong bảng ở phù thủy có JComboBox với kiểu dữ liệu tùy chỉnh. Giá trị của ô đầu tiên được chọn automaticlly. Làm thế nào để giải quyết vấn đề này?

CHỈNH SỬA 1: Tôi đã thêm SSCCE.

lớp chính:

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

} 

Renderer:

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

Để được trợ giúp tốt hơn sớm hơn, hãy đăng [SSCCE] (http://sscce.org/). –

Trả lời

8

Vấn đề là TableModel của bạn được lưu trữ một đối tượng String và ComboBox chứa một đối tượng thử nghiệm. Các đối tượng này không bằng nhau vì vậy không có mục nào để chọn và có vẻ như mục đầu tiên được đánh dấu tự động.

Thay đổi mã của bạn như sau và bạn sẽ thấy cùng một vấn đề với một chuỗi vô danh:

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

Để khắc phục vấn đề này, tôi sẽ đoán bạn cần phải làm như sau:

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

Sau đó, bạn sẽ cần phải thực hiện phương thức equals() trong lớp Test của bạn để so sánh thuộc tính name của cả hai thành phần. Đồng thời, bạn sẽ cần phải triển khai phương thức hashcode(). Trong tương lai, như Andrew đã đề xuất, hãy đưa SSCCE của bạn vào câu hỏi của bạn vì chúng tôi không có thời gian để sao chép/dán/chỉnh sửa và kiểm tra mã vì chúng tôi không bao giờ biết chúng tôi có thực hiện chính xác như bạn làm hay không.

+0

Có lẽ tôi đã hỏi sai, nhưng điều đã giúp tôi, đó là khi tôi tạo hàng mới với 'addRow (new Object [] {})'. Trước đây tôi đã thêm như thế này - 'addRow (new Object [] {" "})' và nó đã sai. – Minutis

+0

Cảm ơn bạn !!! Bây giờ tôi đã hiểu tại sao tôi cần '@ Override'' equals() 'và' hashCode() '. – Minutis

Các vấn đề liên quan