1

I create my table model and now i want to seperate it and show it in different JTables. What is the best way to do that?

Here is what i tried :

public List<DefaultTableModel> getProcedures(JPanel panel) {
   Object[] data = new Object[blueprint.size()];
   Object[] dur = new Object[blueprint.size()]
   Object[] status = new Object[blueprint.size()];

List<DefaultTableModel> parralelList = new ArrayList<DefaultTableModel>();
int counter = 0;

for (Object i : blueprint.keySet()) {
    data[counter] = blueprint.get(i);
    int intDuration = Math.round(blueprintparse.getDuration(i.toString()));
    dur[counter] = Integer.toString(intDuration) + "min";
    status[counter] = dScreen.fillTableStatus(data[counter].toString());
    counter++;
  }
parralelList.add(seperateworkFlows(data, dur, status));
return parralelList;
}
private DefaultTableModel seperateworkFlows(Object[] data, Object[] dur, Object[] status) {
        DefaultTableModel listModel = new DefaultTableModel();
        listModel.addColumn("Procedures", data);
        listModel.addColumn("Duration", dur);
        listModel.addColumn("Status", status);
        return listModel;
    }
...
DefaultTableModel model1 = execution.getProcedures(jProgressFlow1).get(0);
jTableFlow1.setModel(model1);

The blueprint is from parsing a json file so from there i am getting the data, duration and status.

Now i want to seperate the model that i created to 5 models under some condition that are inside the json file but at first place i want to hear your opinion also, if the approach is correct.

Thank you very much for your time

Aris
  • 984
  • 8
  • 22
  • 1
    Sure. It's not entirely clear what the actual question is, though... – Marco13 Jun 04 '19 at 13:05
  • Sorry for that @Marco13 , i just tried to make a list of models, and then get the models from that list. But is there an other way to do that? – Aris Jun 04 '19 at 13:07
  • One could imagine other approaches. After reading the title, I thought that you wanted something like `tableA.setModel(firstColumnOf(model))`, `tableB.setModel(secondColumnOf(model))` (i.e. using the **same** underlying model instance, and only pass a "delegating" model to the tables). But this does not seem to be the case here. So creating a list of 5 table model instances seems like a reasonable approach for me. – Marco13 Jun 04 '19 at 13:10
  • @Marco13 yes maybe you are right, my question missleading a little bit. Ok then i will continue my work like that, and if there will be a better recommandation maybe i will try it. Thank anyway for your time. – Aris Jun 04 '19 at 13:12
  • @AristotelisPozidis i've already developed a model, that can split another one in two or more, but only column based. If you need this, please let me know. I'll post it here. – Sergiy Medvynskyy Jun 04 '19 at 14:36
  • @SergiyMedvynskyy i think it will be really helpfull to see your approach. Thank you very much – Aris Jun 04 '19 at 15:21

1 Answers1

1

Here is my approach.

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 * Table model which allows to split another table model in 2 table models.
 * 
 * @author smedvynskyy
 */
public class SplitTableModel implements TableModel, TableModelListener {

    /** Delegate table model. */
    private final TableModel delegate;

    /** Column index used to split the model. */
    private final int splitColumn;

    /**
     * Part of table to represent (leading [0, {@link #splitColumn}), or trailing [{@link #splitColumn}, delegate.getColumnCount() -
     * splitColumn)).
     */
    private final boolean leading;

    private final Collection<TableModelListener> listeners = new LinkedHashSet<>();

    /**
     * Instantiates a new split table model.
     *
     * @param delegate the delegate model to split.
     * @param splitColumn the split position (column).
     * @param leading defines the part of table to represent (leading: [0, {@link #splitColumn}), or trailing: [{@link #splitColumn},
     *            delegate.getColumnCount() - splitColumn)).
     */
    public SplitTableModel(TableModel delegate, int splitColumn, boolean leading) {
        this.delegate = delegate;
        this.splitColumn = splitColumn;
        this.leading = leading;
        delegate.addTableModelListener(this);
    }

    /**
     * Splits the table model in 2 table models.
     * 
     * @param toSplit table model to be split.
     * @param splitColumn split column.
     * @return array of two models which split the given one (first is the left model, second is the right).
     */
    public static TableModel[] split(TableModel toSplit, int splitColumn) {
        return new TableModel[] {new SplitTableModel(toSplit, splitColumn, true), new SplitTableModel(toSplit, splitColumn, false)};
    }

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

    @Override
    public int getColumnCount() {
        return leading ? Math.min(splitColumn, delegate.getColumnCount()) : Math.max(0, delegate.getColumnCount() - splitColumn);
    }

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

    @Override
    public void addTableModelListener(TableModelListener l) {
        listeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        listeners.remove(l);
    }

    /**
     * Gets all the registered listeners (as unmodifiable collection.
     * 
     * @return all the registered listeners.
     */
    public Collection<TableModelListener> getModelListeners() {
        return Collections.unmodifiableCollection(listeners);
    }

    @Override
    public String getColumnName(int columnIndex) {
        return delegate.getColumnName(toModelColumnIndex(columnIndex));
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return delegate.getColumnClass(toModelColumnIndex(columnIndex));
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return delegate.isCellEditable(rowIndex, toModelColumnIndex(columnIndex));
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        delegate.setValueAt(aValue, rowIndex, toModelColumnIndex(columnIndex));

    }

    @Override
    public void tableChanged(TableModelEvent e) {
        int col = e.getColumn() == -1 ? e.getColumn() : toSplitColumnIndex(e.getColumn());
        TableModelEvent evt = new TableModelEvent(this, e.getFirstRow(), e.getLastRow(), col, e.getType());
        listeners.forEach(l -> l.tableChanged(evt));
    }

    /**
     * Gets the delegate table model.
     * 
     * @return delegate table model.
     */
    public TableModel getDelegate() {
        return delegate;
    }

    private int toSplitColumnIndex(int columnIndex) {
        return leading ? columnIndex : columnIndex - splitColumn;
    }

    private int toModelColumnIndex(int columnIndex) {
        return leading ? columnIndex : columnIndex + splitColumn;
    }
}

This class allows to split a table model in two submodels. But you can continue split, and provide split in 5 models. For example, you have a table model with 15 columns:

TableModel myModel = ...;
List<TableModel> resultModels = new ArrayList<>(5);
TableModel[] split = SplitTableModel.split(myModel, 3);
for (int i = 0; i < 4; i++) {
    resultModels.add(split[0]);
    if (i == 3) {
        resultModels.add(split[1]);
    } else {
        split = SplitTableModel.split(split[1], 3);
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • Thank you very much @SergiyMedvynskyy, i will take your approach as an example and build my own. It is really helpfull. – Aris Jun 05 '19 at 07:11