如果列类型为JComboBox
,我想在JTable
单元格中显示ArrayList
。我的代码显示JComboBox
,但我无法与JComboBox
进行互动。我无法打开它看到它有的物品等。
如果你想试试,这是完整的清单。这有什么问题?
package testproj.jtabletest;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
public class JTableTest extends AbstractTableModel implements TableCellRenderer{
private static final long serialVersionUID = 1703021728506400345L;
public static void main(String[] args) {
JFrame frame = new JFrame();
JTable table = new JTable();
JScrollPane tableHolder = new JScrollPane(table);
frame.add(tableHolder, BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTableTest test = new JTableTest();
table.setModel(test);
table.setDefaultRenderer(ArrayList.class, test);
table.setRowHeight(30);
table.setRowMargin(4);
}
@Override
public int getRowCount() {
return 3;
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ArrayList values = new ArrayList();
values.add(5);
values.add("TEST");
values.add(new Object());
return values;
}
@Override
public boolean isCellEditable(int row, int column){
return true;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
this.fireTableCellUpdated(rowIndex, columnIndex);
}
@Override
public Class getColumnClass(int colIndex){
return ArrayList.class;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JComboBox<Object> re = new JComboBox<Object>();
re.addItem(5);
re.addItem("TEST");
re.addItem(new Object());
re.setEnabled(true);
return re;
}
}