Swing 如何在更改事件时在表格单元格编辑器中的java combobox上设置相同的值
我有一个表,在同一列索引的每一行上都有一个组合框。所以问题是我有 当我在combobox上选择一个值时,我尝试在每一行上设置相同的值。我在表格单元格编辑器上读到了之前关于combobox的问题,我试图实现代码的答案,但我无法实现。我可以看到我所希望的,但我的动作监听器在后面发送了许多异常。如何修复它 此表的代码Swing 如何在更改事件时在表格单元格编辑器中的java combobox上设置相同的值,swing,jcombobox,tablecelleditor,Swing,Jcombobox,Tablecelleditor,我有一个表,在同一列索引的每一行上都有一个组合框。所以问题是我有 当我在combobox上选择一个值时,我尝试在每一行上设置相同的值。我在表格单元格编辑器上读到了之前关于combobox的问题,我试图实现代码的答案,但我无法实现。我可以看到我所希望的,但我的动作监听器在后面发送了许多异常。如何修复它 此表的代码 tblRadars= new JTable(); tblRadars.setModel(new DefaultTableModel(
tblRadars= new JTable();
tblRadars.setModel(new DefaultTableModel(
new Object[][] {},
new String[] {
"Radar", "Multicast IP", "Port", "Period", "Size", "Start", "Stop"
}
) {
/**
*
*/
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] {
false, false, false, true, false, true, true};
public boolean isCellEditable(int row, int column) {
if (column == PERIOD_COLUMN_INDEX ){
return ((JButton)getValueAt(row, START_COLUMN_INDEX)).isEnabled();
}
return columnEditables[column];
}
});
TableColumn periodColumn = tblRadars.getColumnModel().getColumn(3);
final JComboBox comboBox = new JComboBox(new Object[]{"1 min","30 min","1 hr" , "2 hr","4 hr","6 hr", "12 hr"});
comboBox.setSelectedIndex(-1);
DefaultCellEditor ed=new DefaultCellEditor(comboBox);
periodColumn.setCellEditor(ed);
我的滑稽动作听众是
ed.addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
// TODO Auto-generated method stub
String value=(String)tblRadars.getValueAt(tblRadars.getSelectedRow(), PERIOD_COLUMN_INDEX);
int row=tblRadars.getRowCount();
for(int i=0;i<row;i++){
tblRadars.setValueAt(value, i, PERIOD_COLUMN_INDEX);
}
}
ed.addCellEditorListener(新CellEditorListener(){
@凌驾
公共作废编辑停止(变更事件e){
//TODO自动生成的方法存根
字符串值=(字符串)tblRadars.getValueAt(tblRadars.getSelectedRow(),句点\列\索引);
int row=tblRadars.getRowCount();
对于(inti=0;i我真的很傻。有时候答案很简单,但我们总是把问题看得很严重
ActionListener comboBoxAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("period clicked.");
int row=tblRadars.getSelectedRow();
int comboboxColumn=3 ;
String selectedvalue=comboBox.getSelectedItem().toString();
for(int i=0;i<tblRadars.getRowCount();i++){
tblRadars.setValueAt(selectedvalue, i, comboboxColumn);
}
((DefaultTableModel)tblRadars.getModel()).fireTableDataChanged();
}
};
ActionListener ComboxAction=new ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
System.out.println(“点击句点”);
int row=tblRadars.getSelectedRow();
int comboboxColumn=3;
String selectedvalue=comboBox.getSelectedItem().toString();
对于(inti=0;i我真的很傻。有时候答案很简单,但我们总是把问题看得很严重
ActionListener comboBoxAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("period clicked.");
int row=tblRadars.getSelectedRow();
int comboboxColumn=3 ;
String selectedvalue=comboBox.getSelectedItem().toString();
for(int i=0;i<tblRadars.getRowCount();i++){
tblRadars.setValueAt(selectedvalue, i, comboboxColumn);
}
((DefaultTableModel)tblRadars.getModel()).fireTableDataChanged();
}
};
ActionListener ComboxAction=new ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
System.out.println(“点击句点”);
int row=tblRadars.getSelectedRow();
int comboboxColumn=3;
String selectedvalue=comboBox.getSelectedItem().toString();
对于(int i=0;这是错误的:tableModel有责任保持自身的一致性!此外,永远不要从模型外部调用任何fireXX方法。不太确定要将actionListener添加到哪个组合中,但如果编辑器是无效的实现:编辑器不能直接更改模型。这是错误的:它是e tableModel有责任保持自身的一致性!此外,永远不要从模型外部调用任何fireXX方法。不太确定要将actionListener添加到哪个组合,但如果编辑器是无效的实现:编辑器不得直接更改模型。