1 year ago
#341636
Droe
How to display an ArrayList in a JTable from a different class?
I'm making a numerical methods algorithms library, one of them is a false position method, where I return a double value. I want to show the iterations in a JTable, but I'm not sure what's the best way to approach this.
public static double falsePosition(String expression, double tolerance) {
double[] initialInterval = incrementalSearch(expression);
ArrayList<FalsePosition> rows = new ArrayList<>();
rows.add(new FalsePosition());
int i = 0;
rows.get(i).setA(initialInterval[0]);
rows.get(i).setB(initialInterval[1]);
do {
rows.get(i).setFa(evaluateExpression(expression, rows.get(i).getA()));
rows.get(i).setFb(evaluateExpression(expression, rows.get(i).getB()));
rows.get(i).setXi(nextSolucionReglaFalsa(
rows.get(i).getA(),
rows.get(i).getB(),
rows.get(i).getFa(),
rows.get(i).getFb()));
if (i == 0) {
rows.get(i).setError(0.0);
} else {
rows.get(i).setError(error(
rows.get(i).getXi(),
rows.get(i - 1).getXi()));
}
rows.get(i).setFxi(evaluateExpression(expression, rows.get(i).getXi()));
i++;
rows.add(new FalsePosition());
if ((rows.get(i - 1).getFa() * (rows.get(i - 1).getFxi()) < 0)) {
rows.get(i).setB(rows.get(i - 1).getXi());
rows.get(i).setA(rows.get(i - 1).getA());
} else {
rows.get(i).setA(rows.get(i - 1).getXi());
rows.get(i).setB(rows.get(i - 1).getB());
}
} while (i <= 1 || absolute(rows.get(i - 1).getError()) > tolerance);
return rows.get(i - 1).getXi();
}
An example of how the table would look:
(CHANGED) I have each "column" in a different ArrayList because I think that's more readable but I don't know if I should change it.
One of my ideas is to send the ArrayList of the rows to a setter method before the return statement, but I think that would mess with the method purpose, which is to just get a solution using the false position method. And then in the class where I want to create the table call the getter method to get the full ArrayList.
Here's my custom table model class
public class FalsePositionModel extends AbstractTableModel {
private String[] columnNames = {
"i",
"a",
"b",
"f(a)",
"f(b)",
"xi",
"error",
"f(xi)"
};
private List<FalsePosition> values;
public FalsePositionModel() {
values = new ArrayList<FalsePosition>();
}
public FalsePositionModel(List<FalsePosition> values) {
this.values = values;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getRowCount() {
return values.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
FalsePosition falsePosition = getFalsePosition(rowIndex);
switch (columnIndex) {
case 0 -> {
return falsePosition.getA();
}
case 1 -> {
return falsePosition.getB();
}
case 2 -> {
return falsePosition.getFa();
}
case 3 -> {
return falsePosition.getFb();
}
case 4 -> {
return falsePosition.getXi();
}
case 5 -> {
return falsePosition.getError();
}
case 6 -> {
return falsePosition.getFxi();
}
default -> {
return null;
}
}
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex){
FalsePosition falsePosition = getFalsePosition(rowIndex);
switch (columnIndex) {
case 0 -> falsePosition.setA((Double) value);
case 1 -> falsePosition.setB((Double) value);
case 2 -> falsePosition.setFa((Double) value);
case 3 -> falsePosition.setFb((Double) value);
case 4 -> falsePosition.setXi((Double) value);
case 5 -> falsePosition.setError((Double) value);
case 6 -> falsePosition.setFxi((Double) value);
}
fireTableCellUpdated(rowIndex, columnIndex);
}
public FalsePosition getFalsePosition(int row) {
return values.get(row);
}
}
java
swing
arraylist
jtable
numerical-methods
0 Answers
Your Answer