[英]How To Remove Empty Rows In JTable Java
I am selecting records from the database and its showing some empty records in the JTable Along with the records I am not been able to filter records with table .我正在从数据库中选择记录,并在 JTable 中显示一些空记录以及我无法使用 table 过滤记录的记录。
Here are some empty records which is in the image这是图像中的一些空记录
public ArrayList<User> getUsers(){
ArrayList<User> usersList=new ArrayList<User>();
Connection connection=getConnection();
String query="SELECT * FROM info";
Statement myState;
ResultSet rs;
try{
myState=connection.createStatement();
rs=myState.executeQuery(query);
User user;
while(rs.next()){
user=new User(rs.getInt("id"),rs.getString("fname"),rs.getString("lname"),rs.getInt("age"));
usersList.add(user);
}
}
catch(Exception ep){
ep.printStackTrace();
}
return usersList;
}
I am populating these records in JTable with this function which is given below.我正在使用下面给出的这个函数在 JTable 中填充这些记录。
public void ShowUsersinJTable(){
ArrayList<User> uList=getUsers();
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
Object[] row=new Object[4];
for(int i=0;i<uList.size();i++){
row[0]=uList.get(i).getId();
row[1]=uList.get(i).getFname();
row[2]=uList.get(i).getLname();
row[3]=uList.get(i).Age();
model.addRow(row);
}
}
as the code seems about right try changing your code to this:由于代码似乎正确,请尝试将代码更改为:
ArrayList<User> uList=getUsers();
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
for(int i=0;i<uList.size();i++){
Object[] row=new Object[4];
row[0]=uList.get(i).getId();
row[1]=uList.get(i).getFname();
row[2]=uList.get(i).getLname();
row[3]=uList.get(i).Age();
model.addRow(row);
}
also you can delete all rows before filling the table (only needed if table was filled with data)您也可以在填充表格之前删除所有行(仅在表格填充数据时才需要)
for (int i = table.getRowCount() - 1; i >= 0; i--) {
model.removeRow(i);
}
please tell me if it has worked, so i can help you further if needed请告诉我它是否有效,以便我可以在需要时进一步帮助您
The default number of rows of the jTable in netbeans is four. netbeans 中 jTable 的默认行数是四。 So you can remove this by going to jTable properties -> model -> Rows.
因此,您可以通过转到 jTable 属性 -> 模型 -> 行来删除它。
Or, you can set rowCount to be zero before adding rows in the table like this:或者,您可以在表中添加行之前将 rowCount 设置为零,如下所示:
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.