私はJTable
でmysqlテーブルの内容をバインドしたいと思います。私は正常に接続しました。テーブルの行と幅を500に設定します。データを含む500行と空を表示します。今私はデータだけを見たいと思う。空の行を表示したくありません。Java - JTableで動的に行を初期化するには?
私を助けてください
私のアプリケーションは、事前に
class AbstractTableEmployee extends AbstractTableModel {
int row2;
private String[] columnNames = { "ID", "Name", "Company", "Department", "Location", "Mobile NO" , "Shift" };
private Object[][] data = new Object[500][500];
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
int row = 0;
try {
row = this.count();
} catch (SQLException ex) {
Logger.getLogger(AbstractTableEmployee.class.getName()).log(Level.SEVERE, null, ex);
}
return row;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
private int getRowCountFromDB() throws SQLException {
Connection con = (Connection) DBConnection.getDBConnection();
int row = 0;
Statement st = null;
ResultSet rs = null;
String Sql = "Select * from Employee_Master where status = 'Active'";
try {
st = (Statement) con.createStatement();
rs = st.executeQuery(Sql);
while (rs.next())
{
row++;
}
}
finally {
con.close();
rs.close();
st.close();
}
return row;
}
private int count() throws SQLException {
return this.getRowCountFromDB();
}
}
おかげ..方法getRowCountFromDB()
を挿入した後に遅くなります。
空でない行のみをフェッチします。このチュートリアルを読む - http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – adatapost
カスタムTableModelを作成しないでください。単にDefaultTableModelを使用してください。テーブルのサイズをハードコードする必要がないように、行を動的に追加することができます。 – camickr