jcombobox,根据所选行具有不同的值

zvms9eto  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(262)

我正在制作一个jtable,它显示关于足球比赛的信息,在一个特定的列中用户可以选择结果。但在我的代码中,组合框不起作用,当我选择一个元素时,它不会留在单元格中。这里是jtable模型的代码(部分代码,比如变量名是用意大利语写的,因为我来自这里啊哈):

public class ModelloTabellaMatch extends AbstractTableModel {

private List<Match> listaMatch = new ArrayList<Match>();

public ModelloTabellaMatch(List<Match> listaMatch) {
    this.listaMatch = listaMatch;
}

@Override
public int getRowCount() {
    return listaMatch.size();
}

@Override
public int getColumnCount() {
    return 6;
}

@Override
public String getColumnName(int column) {
    if (column == 0) {
        return "Data piazzamento";
    } else if (column == 1) {
        return "Nome match";
    } else if (column == 2) {
        return "Profitto/Perdita";
    } else if (column == 3) {
        return "Inizio evento";
    } else if (column == 4) {
        return "Esito";
    } else if (column == 5) {
        return "Descrizione";
    }
    return "";
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Match match = listaMatch.get(rowIndex);
    if (columnIndex == 0) {
        return match.getDataPiazzamentoScommesse();
    } else if (columnIndex == 1) {
        return match.getNome();
    } else if (columnIndex == 2) {
        return match.getProfittoPerdita();
    } else if (columnIndex == 3) {
        return match.getDataInizioEvento();
    } else if (columnIndex == 4) {
        return match.getContoVincente();
    }else if (columnIndex == 5) {
        return match.getDescrizione();
    }
    return null;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if (!(aValue instanceof Conto)) return;
    Conto conto = (Conto) aValue;
    listaMatch.get(rowIndex).setEsito(conto);
    aggiornaTabella();
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
}

public void aggiornaTabella() {
    this.fireTableStructureChanged();
}

public List<Match> getListaMatch() {
    return listaMatch;
}

public void setListaMatch(List<Match> listaMatch) {
    this.listaMatch = listaMatch;
}

无论如何,应该显示组合框的列是第4列,这是我的tablecelleditor:

public class CellEditorComboConti extends DefaultCellEditor {

private DefaultComboBoxModel model;
private static final Logger logger = LoggerFactory.getLogger(CellEditorComboConti.class);

public CellEditorComboConti() {
    super(new JComboBox<Conto>());
    this.model = this.model = (DefaultComboBoxModel)((JComboBox)getComponent()).getModel();
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
   if (!(value instanceof JComboBox)) return null;
   model.removeAllElements();
   for (int i = 0; i < 5; i++) {
       model.addElement(i + "");
   }
   return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}

还有我的table:

public class RendererTabellaMatch extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (!(value instanceof JComboBox)) return null;
    JComboBox<Conto> comboBox = (JComboBox<Conto>) value;
    Match match = ((ModelloTabellaMatch)table.getModel()).getListaMatch().get(row);
    comboBox.removeAllItems();
    for (Conto conto : match.getListaConti()) {
        comboBox.addItem(conto);
    }
    return comboBox;
}

但结果是一个空白的白色(标签状)无用的可编辑单元格。。。有人能帮我吗?谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题