同じテーブルで実行されるアクションに基づいて、JTable内の特定のセルにコンポーネント(JComboBox)を設定する必要があります。JTable内の単一のセルでJComboBoxを更新する
私はJTableをセットアップしました。列AのすべてのセルにJComboBoxが設定されます。
私はこのコードを次のように変更したいと思います。ユーザーが特定の行のJComboBoxでオプションを選択すると、同じ行(B列)の次のフィールドが異なる項目を持つ新しいJComboBoxが設定されています。
ここで問題となるのは、B列全体に同じJComboBoxを挿入したくないということです。テーブルの各行は、列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);
}
あなたは 'table.setValueAt(jcb、row、column);でテーブルに直接設定することができます。 – XtremeBaumer