我需要根据在同一表中执行的操作,使用组件 (JComboBox) 填充 JTable 中的特定单元格。
我有 JTable 设置。列 A 中的所有单元格都填充了一个 JComboBox。
我想为此调整我的代码(可以在下面找到),以便当用户在特定行的 JComboBox 中选择一个选项时,同一行(在 B 列中)的下一个字段将填充一个新的 JComboBox,其中包含不同的项目。
这里的问题是我不想使用相同的 JComboBox 填充整个 B 列。根据在 A 列中所做的选择,表中的每一行在 B 列的 JComboBox 中可以有不同的选项。
如何更改我的代码来执行此操作?
String sql = "SELECT * from tblDepartment ORDER BY deptName";
int size = 0;
int count = 0;
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
List<String> list = new ArrayList<>();
while (rs.next()) {//adds each item to the list for the combo box
list.add(rs.getString("deptName"));
}
count = list.size();
String[] items = list.toArray(new String[list.size()]);
JComboBox<String> jcb = new JComboBox<>(items);
TableColumn tc = tblCon.getColumnModel().getColumn(2);
TableCellEditor tce = new DefaultCellEditor(jcb);//adds the combo box to the relevant cell in the table.
tc.setCellEditor(tce);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
您可以重写 JTable
的 getCellEditor(...)
方法以返回特定的编辑器。
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableComboBoxByRow extends JPanel
{
List<String[]> editorData = new ArrayList<String[]>(3);
public TableComboBoxByRow()
{
setLayout( new BorderLayout() );
// Create the editorData to be used for each row
editorData.add( new String[]{ "Red", "Blue", "Green" } );
editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
editorData.add( new String[]{ "Apple", "Orange", "Banana" } );
// 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)
{
JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
return new DefaultCellEditor( comboBox1 );
}
else
return super.getCellEditor(row, column);
}
};
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
// table.getColumnModel().getColumn(1).setCellRenderer(new ComboBoxRenderer2() );
}
/*
class ComboBoxRenderer2 extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
label.setIcon(UIManager.getIcon("Table.descendingSortIcon"));
return label;
}
}
*/
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();
}
});
}
}
在上面的示例中,编辑器仅由行确定。
在您的情况下,编辑器将由前一列中的行和数据确定。
您是否尝试过使用自定义 TableCellEditor
?您可以使用名为 getTableCellEditorComponent(JTable table、Object value、boolean isSelected、int row、int column)
的可重写方法,您可以在其中显式定义 JComboBox
应出现的行和列。
下面是一个JCheckBox
的例子:
public class EditBoolean extends AbstractCellEditor implements TableCellEditor {
private JCheckBox box;
public EditBoolean() {
// This is the component that will handle the editing of the cell value
box = new JCheckBox();
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
@Override
public Object getCellEditorValue() {
return box.isSelected();
}
// This method is called when a cell value is edited by the user.
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if(column == 2 && row == 4){
if (value instanceof Boolean) {
box.setSelected((Boolean) (value));
} else {
box.setSelected(Boolean.getBoolean((String) value));
}
return box;
}
}
}