From e080a37e3f5d1d0ba2c737f768813c93b0bdd70c Mon Sep 17 00:00:00 2001 From: oleg20111511 Date: Thu, 2 Jun 2022 11:11:39 +0300 Subject: [PATCH] Data Table Model code refactor --- src/DataTableModel.java | 92 +++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/src/DataTableModel.java b/src/DataTableModel.java index 5d4ae33..f8a8e17 100644 --- a/src/DataTableModel.java +++ b/src/DataTableModel.java @@ -21,6 +21,53 @@ public class DataTableModel extends AbstractTableModel { } } + public String[] getColumnNames() { + return columnNames; + } + + public Data[][] getRowData() { + return rowData; + } + + public void setColumnNames(String[] columnNames) { + this.columnNames = columnNames; + } + + @Override + public String getColumnName(int column) { + return columnNames[column]; + } + + @Override + public int getRowCount() { + return rowData.length; + } + + @Override + public int getColumnCount() { + return columnNames.length; + } + + @Override + public Object getValueAt(int row, int col) { + return rowData[row][col]; + } + + @Override + public boolean isCellEditable(int row, int column) { + return true; + } + + @Override + public void setValueAt(Object value, int row, int col) { + rowData[row][col] = new Data(value.toString()); + fireTableCellUpdated(row, col); + } + + /* + * Menu action functions: + */ + public void addRow(int newRowPos) { int newRowCount = getRowCount() + 1; Data[][] newRowData = new Data[newRowCount][getColumnCount()]; @@ -137,7 +184,7 @@ public class DataTableModel extends AbstractTableModel { // Go through all cells in table for (int row = 0; row < getRowCount(); row++) { for (int col = 0; col < getColumnCount(); col++) { - Data cell = rowData[row][col]; + Data cell = rowData[row][col]; // 1) Check if cell contains target tree // 2) Compare tree output with max, save if higher if (cell.getTree().equals(treeName) && cell.getHarvest() > maxHarvest) { @@ -172,47 +219,4 @@ public class DataTableModel extends AbstractTableModel { return harvest; } - - public String[] getColumnNames() { - return columnNames; - } - - public Data[][] getRowData() { - return rowData; - } - - public void setColumnNames(String[] columnNames) { - this.columnNames = columnNames; - } - - @Override - public String getColumnName(int column) { - return columnNames[column]; - } - - @Override - public int getRowCount() { - return rowData.length; - } - - @Override - public int getColumnCount() { - return columnNames.length; - } - - @Override - public Object getValueAt(int row, int col) { - return rowData[row][col]; - } - - @Override - public boolean isCellEditable(int row, int column) { - return true; - } - - @Override - public void setValueAt(Object value, int row, int col) { - rowData[row][col] = new Data(value.toString()); - fireTableCellUpdated(row, col); - } }