Domanda

Sto usando una tabella con un rendering di celle che mi consente di posizionare un pulsante "x" in ogni cella con un valore in modo da poter eliminare il valore della cella premendo il pulsante. L'editor di celle assegna l'ascoltatore di azioni al pulsante e le azioni appropriate vengono chiamate quando viene premuto il pulsante.

Ogni colonna nel modello di tabella che è un moto di moto è popolato da un elenco di valori. Sono riuscito a eliminare con successo il valore giusto dall'elenco degli array premendo il pulsante e ogni altra cella nella tabella si aggiorna dopo la modifica ma la cella che è il valore è stata eliminata rimane invariata. Se riavvio l'applicazione o aggiungo un valore all'elenco di array, la tabella si aggiorna come previsto e tutto sembra come dovrebbe.

Il problema MO quindi è che sembra che il rendering della cella non dimenticherà il valore iniziale della cella e quindi continua a stampare questo valore nella cella anche se la tabella è ridisegnata. Qualcuno ha un'idea di come potrei aggiornare correttamente la tabella in modo che i valori siano mostrati correttamente?

Classe di tabella:

public class Table extends JTable {

public static DefaultTableModel model = new DefaultTableModel();
private Days d;
private JFrame frame;
private AddCellRenderer renderer;
private AddCellEditor editor;

public Table(Days d, JFrame frame) {
    // Assign the table model to the table
    super(model);
    this.d = d;
    this.frame = frame;

    // Define dimensions of table
    model.setRowCount(11);
    model.setColumnCount(5);

    // Block resizing and reordering of headers
    this.setColumnSelectionAllowed(false);
    this.setRowSelectionAllowed(false);
    this.setCellSelectionEnabled(true);
    this.setTableHeader(null);

    // Define first row height
    this.setRowHeight(38);
    // Define all other row's heights
    this.setRowHeight(0, 45);

    this.d.setTable(this);

    for (int i = 0; i < 5; i++) {
        editor = new AddCellEditor(new JCheckBox(), d.getVisibleDays().get(i), this);
        renderer = new AddCellRenderer(d.getVisibleDays().get(i), editor);
        this.getColumnModel().getColumn(i).setCellRenderer(renderer);
        this.getColumnModel().getColumn(i).setCellEditor(editor);
    }

    this.refresh();
}

public void refresh() {

    // Empty table
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 11; j++) {
            this.getModel().setValueAt(null, j, i);
        }
    }

    // Populate table with entries for each day
    for (int i = 0; i < 5; i++) {

        Iterator<String> iterator = d.getVisibleDays().get(i).getEntryListIterator();

        int j = 0;

        while(iterator.hasNext()){
            String s = iterator.next();
            this.getModel().setValueAt(s, (j+1), i);
            j++;
        }

        this.getModel().setValueAt(d.getVisibleDays().get(i).getDayName(), 0, i);
    }
}

public void modelClearValueAt(int row, int column) {
    this.getModel().setValueAt(null, row, column);
}
}

Classe di celle Editor:

public class AddCellEditor extends DefaultCellEditor {

private JPanel headerPanel;
private JPanel entryPanel;
private JLabel dayName;
private JLabel entryValue;
protected JButton addButton;
private JButton removeButton;
private String label;
private int row;
private int column;
private Day day;
private String date;
private Table table;
private AddCellRenderer renderer;

public AddCellEditor(JCheckBox checkBox, Day day, Table table) {
    super(checkBox);
    this.day = day;
    this.table = table;
    date = day.getDayDate();
    headerPanel = new JPanel();
    headerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    entryPanel = new JPanel();
    entryPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    dayName = new JLabel();
    entryValue = new JLabel();
    addButton = new JButton();
    removeButton = new JButton();
    headerPanel.add(dayName);
    headerPanel.add(addButton);
    entryPanel.add(entryValue);
    entryPanel.add(removeButton);
    addButton.setOpaque(true);
    addButton.setPreferredSize(new Dimension(16, 16));
    removeButton.setOpaque(true);
    removeButton.setPreferredSize(new Dimension(16, 16));
    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Add pressed");
            addItem();
            fireEditingStopped();
        }
    });
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Remove pressed");
            removeItem();
            getTable().refresh();
            fireEditingStopped();
        }
    });

}

public void setRenderer(AddCellRenderer renderer) {
    this.renderer = renderer;
}

public Component getTableCellEditorComponent(JTable table, 
        Object value, boolean isSelected, int row, int column) {

    this.row = row;
    this.column = column;

    if (row == 0) {
         return this.headerPanel;
    }
    if (row != 0 && value != null) {
        return this.entryPanel;
    }
    return null;
}

public void addItem() {
    String input = JOptionPane.showInputDialog("Add entry for " + label + ":");

    if ((input != null) && (input.length() > 0)) {
        System.out.println("Added: " + input + "Item");
        day.addEntry(input, column);
    }
}

public void removeItem() {
    table.modelClearValueAt(row, column);
    day.removeEntry(row-1);
    System.out.println("Item removed");
}

public Table getTable() {
    return this.table;
}

public boolean stopCellEditing() {
    return super.stopCellEditing();
}

protected void fireEditingStopped() {
    super.fireEditingStopped();
}
}

Grazie in anticipo

È stato utile?

Soluzione

Ok, non potrei resistere (l'alternativa è strofinare il bagno :-)

Di seguito è riportato uno schema molto grezzo (quanto basta per darti un'idea) su come risolvere il tuo obiettivo con editor ben educato e tablemodel personalizzato.

  • MyTableModel è un'implementazione personalizzata sostanzialmente supportata da un elenco di voci arbitrarie
  • Questo tablemodel supporta la rimozione/aggiunta di elementi. Privatamente per ora, quindi non sei tentato di chiamarli dall'editore :-) Sono abbastanza sicuri da esporre con l'ambito pubblico, se necessario, FI Se altre parti del programma devono anche modificare le voci
  • Il modello è in grado di gestire diversi tipi di valore in setValueat: un marcatore modifica, un nuovoentry o un valore semplice
  • L'editor personalizzato è un pannello con pulsanti per rimuovere, aggiungere una riga.
  • Le azioni del pulsante impostano l'editorvalue come appropriato prima di chiamare l'editingStop/annullata secondo necessità

Il modello:

public static class MyTableModel extends AbstractTableModel {

    public enum Modify {
        ADD,
        REMOVE
    }

    public static class NewItem {
        public final Object entry;

        public NewItem(Object entry) {
            this.entry = entry;
        }
    }

    private List entries;

    public MyTableModel(List entries) {
        this.entries = entries;
    }

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

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


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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return entries.get(rowIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (aValue instanceof NewItem) {
            addEntry(((NewItem) aValue).entry);
        } else if (Modify.REMOVE == aValue) {
            removeEntry(rowIndex);
        } else {
            entries.set(rowIndex, aValue);
        }    
    }

    private void removeEntry(int rowIndex) {
        entries.remove(rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }

    private void addEntry(Object aValue) {
        int last = getRowCount();
        entries.add(aValue);
        fireTableRowsInserted(last, last);
    }


}

L'editore:

public static class MyCellEditor extends AbstractCellEditor implements TableCellEditor {

    private Object editorValue;
    private JLabel entryView;
    JComponent editor;

    public MyCellEditor() {
        editor = new JPanel();
        entryView = new JLabel();
        editor.add(entryView);
        Action add = createAddAction();
        editor.add(new JButton(add));
        Action remove = createRemoveAction();
        editor.add(new JButton(remove));
    }

    public Action createRemoveAction() {
        Action remove = new AbstractAction("Remove Entry") {

            @Override
            public void actionPerformed(ActionEvent e) {
                editorValue = MyTableModel.Modify.REMOVE;
                fireEditingStopped();
            }

        };
        return remove;
    }

    public Action createAddAction() {
        Action add = new AbstractAction("Add Entry") {

            @Override
            public void actionPerformed(ActionEvent e) {
                String input = JOptionPane.showInputDialog("Add entry: ");

                if ((input != null) && (input.length() > 0)) {
                    editorValue = new MyTableModel.NewItem(input);
                    fireEditingStopped();
                } else {
                    fireEditingCanceled();
                }

            }

        };
        return add;
    }

    @Override
    public Object getCellEditorValue() {
        return editorValue;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
        entryView.setText(value != null ? value.toString() : "");
        return editor;
    }


}

L'utilizzo:

    List entries = new ArrayList();
    for (int i = 0; i < 5; i++) {
        entries.add("entry " + i);
    }
    TableModel model = new MyTableModel(entries);
    JTable table = new JTable(model);
    table.getColumnModel().getColumn(0).setCellEditor(new MyCellEditor());
    table.getColumnModel().getColumn(0).setPreferredWidth(200);
    table.setRowHeight(50);

Altri suggerimenti

Se stai utilizzando DefaultTableModel, dovresti eliminare il valore detenuto dal modello stesso, non il valore nell'arraylist utilizzato per costruire il modello. Lo faresti chiamando il setValueAt(...) Metodo dell'oggetto DefaultAbleModel.

Potresti voler pubblicare un SSCCE per un aiuto più specifico.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top