Alright, I have this code here which is my replacement implementation of the standard Swing TableModel. Which I think is an absolute nightmare, my question is, I have many rowIndex and columIndex parameters, is there a way I can share a description between them for a more standardized, and less finger working way? Thank You!!
package atablemodel;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
/**
* My custom swing TableModel version 1.x will represent a complete table model
* while version 2.x adds several methods for updating an existing table version
* 0.x is an incomplete version
* Completely changed everything to work with arraylist instead of array, which means
* that ATableModel Object can now simply be updated instead of recreated
* @author alex
* @version 2.0
* @see AbstractTableModel
* @see TableModel
*/
@SuppressWarnings("serial")
public class ATableModel extends AbstractTableModel {
private ArrayList<String> cn = new ArrayList<String>();
private ArrayList<ArrayList<RowDataObject>> rd = new ArrayList<ArrayList<RowDataObject>>();
/**
* Creates my custom TableModel
*
* @param columnames The names for the columns
* @param rowdata The data for the rows
*/
public ATableModel(String[] columnames, Object[][] rowdata) {
for (int i = 0; i < columnames.length; i++) {
cn.add(columnames[i]);
}
for (int i = 0; i < rowdata.length; i++) {
ArrayList<RowDataObject> rdot1 = new ArrayList<RowDataObject>();
for (int i2 = 0; i2 < rowdata[i].length; i2++) {
rdot1.add(new RowDataObject(rowdata[i][i2]));
}
rd.add(rdot1);
}
}
/**
* Looks up the column name for the specified column
*
* @param column The number of the column to look up
* @return The name (string) of the column
*/
public String getColumnName(int column) {
return cn.get(column);
}
/**
* This method will simply tell you if a cell is editable
* This is only here because it is in the Default TableModel
* @deprecated use {@link #getCellEditable(int, int)} instead
* @param columnIndex The column
* @param rowIndex The row
* @return A boolean indicating if the cell is editable or not
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
return getCellEditable(rowIndex, columnIndex);
}
/**
* Sets the value at a certain cell; <b>TAKE NOTE!:</b> this set the cell to
* editable before changing the value, and returns it to either uneditable,
* or editable depending on what the cell was before in {@link #rde}
*
* @param aValue The value to set
* @param rowIndex The row
* @param columnIndex The column
*/
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
boolean editable = getRDO(rowIndex, columnIndex).getEditable();
getRDO(rowIndex, columnIndex).setEditable(true);
getRDO(rowIndex, columnIndex).setData(aValue);
getRDO(rowIndex, columnIndex).setEditable(editable);
}
/**
* gets the number of columns; returns the length of columnnames, so if your
* rowdata and columnnames don't match up, it might not give you the info
* you want
*
* @return Number of columns
*/
public int getColumnCount() {
return cn.size();
}
/**
* gets the number of rows
*
* @return number of rows
*/
public int getRowCount() {
return rd.size();
}
/**
* gets the value for a specified cell
*
* @param rowIndex The row
* @param columnIndex The column
* @return the value (if any) at the specified cell
*/
public Object getValueAt(int rowIndex, int columnIndex) {
return getRDO(rowIndex, columnIndex).getData();
/* getValueAt(0, 3); returns d */
}
/**
* do not use, useless unless absolutely necessary for some reason
*
* @deprecated
*/
public TableModel returnTableModel() {
return this;
}
/**
* gets a boolean indicating if the cell is editable or not
*
* @param rowIndex The row
* @param columnIndex The column
* @return A boolean indicating whether the cell is editable
*/
public boolean getCellEditable(int rowIndex, int columnIndex) {
return getRDO(rowIndex, columnIndex).getEditable();
}
/**
* sets all cells editable
*
* @param rowIndex The row
* @param columnIndex The column
* @param editable set all cells to this value
*/
public void setCellEditable(int rowIndex, int columnIndex, boolean editable) {
getRDO(rowIndex, columnIndex).setEditable(editable);
}
/**
* Sets all cells in the current rowdata to {@code editable}
*
* @param editable What to set all of the cells editable values to
*/
public void setAllCellsEditable(boolean editable) {
int x, y;
for (x = 0; x < rd.size(); x++) {
for (y = 0; y < rd.get(x).size(); y++) {
getRDO(x, y).setEditable(editable);
}
}
}
/**
* Set all the cells of a single row to editable or not
*
* @param rowIndex The row to set editable
* @param editable what to set the cells editable values to
*/
public void setRowEditable(int rowIndex, boolean editable) {
for (RowDataObject rdo : getRDORow(rowIndex)) {
rdo.setEditable(editable);
}
}
/**
* Set all the cells in one column to editable
*
* @param columnIndex The column to set editable
* @param editable what to set all the cells editable values to
*/
public void setColumnEditable(int columnIndex, boolean editable) {
for (RowDataObject rdo : getRDOCol(columnIndex)) {
rdo.setEditable(editable);
}
}
public RowDataObject getRDO(int rowIndex, int columnIndex) {
return rd.get(rowIndex).get(columnIndex);
}
public ArrayList<RowDataObject> getRDORow(int rowIndex) {
//ArrayList<RowDataObject> rdor;
/*for (int i = 0; i < rd.get(rowIndex).size(); i++) {
}*/
return rd.get(rowIndex);
}
public ArrayList<RowDataObject> getRDOCol(int columnIndex) {
ArrayList<RowDataObject> rdoc = new ArrayList<RowDataObject>();
for (int i = 0; i < rd.size(); i++) {
rdoc.add(rd.get(i).get(columnIndex));
}
return rdoc;
}
}
/*
* rd[2][5] 0 1 2 3 4 0 {a b c d e} 1 {f g h i j}
*
* {a b c d e}, {f g h i j}
*/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…