Re: JTable help
Well I've made an object to put the ResultSet data into, as suggested,
which definetly makes things better Unfortunately I am still getting
duplicated rows. According to the console output it goes in fine, and
comes out of the vector fine.. here's the ResultSet db stuff, the
interActiveJatable and the resultObject:
public class resultObject {
public static String TIME = null;
public static String CLASS_CODE = null;
public static String DAY_OF_WEEK = null;
public static String MEMBERS = null;
public static String MINUTES_NONTEACHING= null;
public static String MINUTES_OUTSIDE= null;
public static String MINUTES_TEACHING= null;
public void setTime (String t){
TIME = t;
}
public void setClassCode(String c){
CLASS_CODE = c;
System.out.println("SetClassCode: "+ CLASS_CODE);
}
public void setDayOfWeek(String d){
DAY_OF_WEEK = d;
}
public void setMembers(String m){
MEMBERS = m;
}
public void setMinutesNonTeaching(String mnt){
MINUTES_NONTEACHING = mnt;
}
public void setMinutesOutside(String mo){
MINUTES_OUTSIDE = mo;
}
public void setMinutesTeaching(String mt){
MINUTES_TEACHING = mt;
}
public String getTime() throws NullPointerException{
return TIME;
}
public String getClassCode(){
return CLASS_CODE;
}
public String getDayOfWeek(){
return DAY_OF_WEEK;
}
public String getMembers(){
return MEMBERS;
}
public String getMinutesNonTeaching(){
return MINUTES_NONTEACHING;
}
public String getMinutesOutside(){
return MINUTES_OUTSIDE;
}
public String getMinutesTeaching(){
return MINUTES_TEACHING;
}
}
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
//modify this for our purposes
public class InteractiveTableModel extends AbstractTableModel {
public static final int TIME_INDEX = 0;
public static final int CLASS_INDEX = 1;
public static final int DAY_INDEX = 2;
public static final int MEMBERS_INDEX = 3;
public static final int MINUTES_NONTEACHING_INDEX = 4;
public static final int MINUTES_OUTSIDE_INDEX = 5;
public static final int MINUTES_TEACHING_INDEX = 6;
protected String[] columnNames;
protected Vector dataVector = new Vector();
public static String teacherName;
public static String day;
public InteractiveTableModel(String[] columnNames, Vector table)
{
this.columnNames = columnNames;
dataVector = (Vector)table.clone();
System.out.println("Length of dataVector is:
"+dataVector.size());
}
public String getColumnName(int column) {
return columnNames[column];
}
public boolean isCellEditable(int row, int column) {
return true;
}
public Class getColumnClass(int column) {
switch (column) {
case TIME_INDEX:
case CLASS_INDEX:
case DAY_INDEX:
case MEMBERS_INDEX:
case MINUTES_NONTEACHING_INDEX:
case MINUTES_OUTSIDE_INDEX:
case MINUTES_TEACHING_INDEX:
return String.class;
default:
return Object.class;
}
}
public String getValueAt(int row, int column) {
//dataVector =
(Vector)CreateDBSchedule.resultFinder(teacherName, day).clone();
resultObject rowData =
(resultObject)dataVector.elementAt(row);
System.out.println("We've made it to get value at"+row
+column);
try {
switch (column) {
case TIME_INDEX:
return rowData.getTime();
case CLASS_INDEX:
return rowData.getClassCode();
case DAY_INDEX:
return rowData.getDayOfWeek();
case MEMBERS_INDEX:
return rowData.getMembers();
case MINUTES_NONTEACHING_INDEX:
return rowData.getMinutesNonTeaching();
case MINUTES_OUTSIDE_INDEX:
return rowData.getMinutesOutside();
case MINUTES_TEACHING_INDEX:
return rowData.getMinutesTeaching();
default:
System.out.println("Nothing here?");
return "Nothing";
}
} catch(NullPointerException e){
System.out.println("Nothing here: "+e);
return null;
}
}
public void setValueAt(Object value, int row, int column) {
//TODO
Object[] rowData = (Object[])dataVector.get(row);
switch (column) {
case TIME_INDEX:
rowData[TIME_INDEX] = value;
case CLASS_INDEX:
rowData[CLASS_INDEX] = value;
case DAY_INDEX:
rowData[DAY_INDEX] = value;
case MEMBERS_INDEX:
rowData[MEMBERS_INDEX] = value;
case MINUTES_NONTEACHING_INDEX:
rowData[MINUTES_NONTEACHING_INDEX] = value;
case MINUTES_OUTSIDE_INDEX:
rowData[MINUTES_OUTSIDE_INDEX] = value;
case MINUTES_TEACHING_INDEX:
rowData[MINUTES_TEACHING_INDEX] = value;
default:
System.out.println("something bad happened changing
the table");
}
fireTableCellUpdated(row, column);
}
public int getRowCount() {
System.out.println("Get row count: "+dataVector.size());
return dataVector.size();
}
public int getColumnCount() {
System.out.println("Column Count: "+columnNames.length);
return columnNames.length;
}
}
public static Vector resultFinder(String tableName, String
searchTerms) {
Vector tableDataFiller = new Vector();
String query = "SELECT *" +
" FROM "+ tableName +
" WHERE "+tableName+".DAY_OF_WEEK LIKE '"+
searchTerms+"'";
try {
con = DriverManager.getConnection(protocol + dB +
";create=true", "app", "app");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int rowCount = 0;
while (rs.next()) {
resultObject rowData = new resultObject();
rowData.setTime(rs.getString("TIME"));
rowData.setClassCode(rs.getString("CLASS_CODE"));
rowData.setDayOfWeek(rs.getString("DAY_OF_WEEK"));
rowData.setMembers(rs.getString("MEMBERS"));
rowData.setMinutesNonTeaching(rs.getString("MINUTES_NONTEACHING"));
rowData.setMinutesOutside(rs.getString("MINUTES_OUTSIDE"));
rowData.setMinutesTeaching(rs.getString("MINUTES_TEACHING"));
tableDataFiller.addElement(rowData);
System.out.println("Let's see what the RS vector
has: ");
//debug output here...
resultObject test =
(resultObject)tableDataFiller.elementAt(rowCount);
String n = test.getClassCode();
System.out.println("ClassCode for row: "+rowCount+"
"+n);
rowCount++;
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
return tableDataFiller;
}
Also I will see if I can get this into one of those compilable
examples as suggested earlier.