我想在 JTable 中显示一个组合框。对于 JTable,我使用 TableModel。我已成功显示组合框(不是其 toString())。问题是我无法查看其内容。组合框必须不可编辑。我读过很多教程,但没有找到 TableModel 的教程。我找到了这段代码:
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));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
我无法使用它有两个原因。我的 Combobox 的内容不一样,并且它们不使用 TableModel。这是我的代码:
公共(public)类 TProfModel 扩展 DefaultTableModel 实现 TableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private String[] names = { "Nom", "Prénom", "Matière", "Classes" };
private Model model;
public TProfModel(Model model){
this.model = model;
fireTableDataChanged();
}
public Class<?> getColumnClass(int col) {
return getValueAt(0,col).getClass();
}
public int getColumnCount() {
return names.length;
}
public String getColumnName(int col) {
return names[col];
}
public int getRowCount() {
if(model != null)
return model.getProfs().size();
return 0;
}
public Object getValueAt(int line, int col) {
switch (col) {
case 0:
return model.getProfs().get(line).getNom();
case 1:
return model.getProfs().get(line).getPrenom();
case 2:
return model.getProfs().get(line).getMatiere();
case 3:
return getClasses(line);
}
return null;
}
private JComboBox<String> getClasses(int line) {
JComboBox<String> comboBox = new JComboBox<String>();
for(String classe : model.getProfs().get(line).getClasses())
comboBox.addItem(classe);
comboBox.setSelectedIndex(0);
return comboBox;
}
public boolean isCellEditable(int arg0, int arg1) {
if(arg1 == 3)
return false;
return true;
}
public void removeTableModelListener(TableModelListener arg0) {
// TODO Auto-generated method stub
}
@Override
public void setValueAt(Object object, int line, int col) {
switch (col) {
case 0:
model.getProfs().get(line).setNom((String) object);
break;
case 1:
model.getProfs().get(line).setPrenom((String) object);
break;
case 2:
model.getProfs().get(line).setMatiere((String) object);
}
model.writeData();
}
}
最佳答案
您可以重写 getCellEditor(...) 方法以返回不同的编辑器。像这样的东西:
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableComboBoxByRow extends JPanel
{
List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);
public TableComboBoxByRow()
{
setLayout( new BorderLayout() );
// Create the editors to be used for each row
String[] items1 = { "Red", "Blue", "Green" };
JComboBox<String> comboBox1 = new JComboBox<String>( items1 );
DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
editors.add( dce1 );
String[] items2 = { "Circle", "Square", "Triangle" };
JComboBox<String> comboBox2 = new JComboBox<String>( items2 );
DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
editors.add( dce2 );
String[] items3 = { "Apple", "Orange", "Banana" };
JComboBox<String> comboBox3 = new JComboBox<String>( items3 );
DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
editors.add( dce3 );
// Create the table with default data
Object[][] data =
{
{"Color", "Red"},
{"Shape", "Square"},
{"Fruit", "Banana"},
{"Plain", "Text"}
};
String[] columnNames = {"Type","Value"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model)
{
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
if (modelColumn == 1 && row < 3)
return editors.get(row);
else
return super.getCellEditor(row, column);
}
};
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Table Combo Box by Row");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableComboBoxByRow() );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
关于java - ComboBox 未在 JTable 中显示其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238871/