Hello:
I have a frame that shows rs from a db. I also can show the hodgepodge of code dealing with how a table could be manipulated. I am trying to cipher out what is needed to simply add a empty editable row to the table when it builds.I do not expect the answer. Just, if someone could describe a way to start because I am confused on the AbstractTable model in general. Editing this row will be featured in my next dumb question.
Thanks.
public void createRegisterComboBox() throws SQLException {
JList r=new JList(profileArray);
for(int i=0;i<r.getModel().getSize();i++){
registerCB.addItem(r.getModel().getElementAt(i));
}
registerCB.addActionListener(this);
}
///////////////////////////////////////////////////////////////////
///////////////////// actions //////////////////////////////
//////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == registerCB) { // show the selected table from the combo box
System.out.println("Create the table on the display panel");
/*
if (displayScrollPane != null) {
this.doLayout();
displayPanel.remove(displayScrollPane);
}
*
*/
try {
tableName = (String) registerCB.getSelectedItem();
/*
if (rs != null) {
rs.close();
}
*
*/
String queryStr = ModelUtils.getXMLResource(RESOURCE.concat(tableName).trim());
System.out.println("queryStr: derived from: "+RESOURCE.concat(tableName).trim());
System.out.println("select statement : " + queryStr);
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
//String query = "SELECT * FROM " + tableName;
rs = stmt.executeQuery(queryStr);
if (SCROLLABLE) {
model = new CreateInsertScrollingTableModel(rs);
} else {
model = new CachingResultSetTableModel(rs);
}
//not implemented the data apears in a frame
/*
table = new JTable(model);
displayScrollPane = new JScrollPane(table);
displayScrollPane.setAutoscrolls(true);
displayScrollPane.setViewportView(table);
displayPanel.add(displayScrollPane);
this.doLayout();
*
*/
table = new JTable(model);
table.getModel().addTableModelListener(table);
displayScrollPane = new JScrollPane(table);
displayScrollPane.setViewportView(table);
CreateDataJInternalFrame cif = new CreateDataJInternalFrame(tableName);
CreateDataJInternalFrame.createDataInnerFrame(displayScrollPane);
} catch (ProfileEx
......
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package view.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
/**
*
* @author depot
*/
public class CreateInsertScrollingTableModel extends ResultSetTableModel implements TableModelListener {
public CreateInsertScrollingTableModel(ResultSet thisResultSet) throws FileNotFoundException,
IOException, SQLException {
super(thisResultSet);
}
@Override
public Object getValueAt(int r, int c) {
try {
ResultSet rs = getResultSet();
rs.absolute(r + 1);
fireTableDataChanged();
return rs.getObject(c + 1);
} catch (SQLException e) {
System.out.println("Error " + e);
return null;
}
}
@Override
public int getRowCount() {
try {
ResultSet rs = getResultSet();
fireTableDataChanged();
return rs.getRow();
} catch (SQLException e) {
System.out.println("Error " + e);
return 0;
}
}
public boolean isCellEditable(int r, int c) {
return true;
}
public void setValueAt(Object v, int r, int c) {
fireTableDataChanged();
}
public void tableChanged(TableModelEvent e) {
int r = e.getFirstRow();
int c = e.getColumn();
TableModel model = (TableModel) e.getSource();
String columnName = model.getColumnName(c);
Object data = model.getValueAt(r, c);
}
}
public abstract class ResultSetTableModel extends AbstractTableModel {
private ResultSet rs;
private ResultSetMetaData rsmd;
public ResultSetTableModel(ResultSet aResultSet) {
rs = aResultSet;
try {
rsmd = rs.getMetaData();
} catch (SQLException e) {
System.out.println("Error " + e);
}
}
public String getColumnName(int c) {
try {
return rsmd.getColumnName(c + 1);
} catch (SQLException e) {
System.out.println("Error " + e);
return "";
}
}
public int getColumnCount() {
try {
return rsmd.getColumnCount();
} catch (SQLException e) {
System.out.println("Error " + e);
return 0;
}
}
protected ResultSet getResultSet() {
return rs;
}
}