[英]JTable, JComboBox using custom Objects
嗨,如果您將JComboBox放在JTable和String []數組中給JComboBox一切正常。 Buf如果您將自己的數據類型放到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;
}
}
問題是您的TableModel存儲String對象,而ComboBox包含Test對象。 這些對象不相等,因此沒有要選擇的項目,看起來第一個會自動突出顯示。
將您的代碼更改為以下內容,您將看到與未知字符串相同的問題:
{"Joe", "Brown", "Pool?????", new Integer(10), new Boolean(false)}
要解決這個問題,我猜你需要做以下事情:
{"Joe", "Brown", new Test("Pool"), new Integer(10), new Boolean(false)}
然后,您需要在Test類中實現equals()方法,以比較兩個組件的name屬性。 同樣,您需要實現hashcode()方法。
將來,正如安德魯建議的那樣,請將您的SSCCE包含在您的問題中,因為我們沒有時間復制/粘貼/編輯和測試代碼,因為我們永遠不會知道我們是否完全按照您的方式執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.