是否可以将 jtable 模型分成 5 个不同的模型?
Is it possible to separate the jtable model to 5 different models?
我创建了我的 table 模型,现在我想将它分开并在不同的 JTable 中显示它。最好的方法是什么?
这是我尝试过的:
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);
蓝图来自解析 json 文件,因此我从那里获取数据、持续时间和状态。
现在我想在 json 文件中的某些条件下将我创建的模型分成 5 个模型,但首先我也想听听您的意见,如果方法正确的话。
非常感谢您的宝贵时间
这是我的方法。
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;
}
}
此 class 允许将 table 模型拆分为两个子模型。但是你可以继续split,并提供split in 5 models。例如,您有一个包含 15 列的 table 模型:
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);
}
}
我创建了我的 table 模型,现在我想将它分开并在不同的 JTable 中显示它。最好的方法是什么?
这是我尝试过的:
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);
蓝图来自解析 json 文件,因此我从那里获取数据、持续时间和状态。
现在我想在 json 文件中的某些条件下将我创建的模型分成 5 个模型,但首先我也想听听您的意见,如果方法正确的话。
非常感谢您的宝贵时间
这是我的方法。
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;
}
}
此 class 允许将 table 模型拆分为两个子模型。但是你可以继续split,并提供split in 5 models。例如,您有一个包含 15 列的 table 模型:
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);
}
}