#1 - I'm a newb to java :(
#2 Im having a problem getting getting my JTable to update when the user edits a cell. Im using a abstract model which is basically what the sun tutorial did word for word( i used a abstractmodel so i could make columns uneditable). Also another problem im having is my code to generate stats on row values uses Math Commons library but I have to repeat my code 25 times to do all the rows in the table
public class tables extends JPanel
{
public tables() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.getModel().addTableModelListener(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
// ROW 1 STATS ---------| I repeat this 25x to fill my rows :(
DescriptiveStatistics stats = new DescriptiveStatistics();
for (int colx = 1; colx<5; colx++){
double data[] = {(Double)table.getValueAt(0,colx)};
for( int i = 0; i < data.length; i++) {
stats.addValue(data[i]);
}}
double mean = stats.getMean();
double std = stats.getStandardDeviation();
double cv = std/mean * 100;
double setcv = 5.10;
String accept = "Accepted";
String unacc = "Unaccepted";
table.setValueAt(mean,0,5);
table.setValueAt(std,0,6);
table.setValueAt(cv,0,7);
table.setValueAt(setcv,0,8);
System.out.print(mean);
if (cv <= setcv){
table.setValueAt(accept,0,9);
}
else{
table.setValueAt(unacc,0,9);
}
class MyTableModel extends AbstractTableModel {
Object data[][] = {
{"Acet", new Double(2.1), new Double(2.2),new Double(2.9),new Double(2.3),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0) },
---- Repeated for more rows
} ;
String columnNames[] = { "Assay", "C8000-1", "C8000-2", "C8-STR", "C8-LHC", "Mean", "SD", "%CV", "Set %CV", "Acceptability" };
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 1) {
return false;}
else if (col >4){
return false;
}
else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
repaint();
// Do something with the data...
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame ui = new JFrame("Correlation Manager");
ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
tables newContentPane = new tables();
newContentPane.setOpaque(true); //content panes must be opaque
ui.setContentPane(newContentPane);
//Display the window.
ui.pack();
ui.setVisible(true);
ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ui.setSize(800,600);
ui.setVisible(true);
ui.setResizable( false );
ui.setLocation(200,100);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}