我想在 JTable 中添加行,但效果不佳。有人可以帮助我吗?表格显示正常但不是动态显示
//displays all data in Jtable
void refresh()
{
Vector<Vector<String>> data = new Vector<>();
ResultSet rs = st.executeQuery("SELECT * FROM tblInfo");
while(rs.next())
{
Vector<String> d = new Vector<>();
d.add(rs.getString("ID"));
d.add(rs.getString("Name"));
d.add(rs.getString("User"));
d.add(rs.getString("Pass"));
data.add(d);
}
Vector<String> header = new Vector<>();
header.add("ID");
header.add("Name");
header.add("Username");
header.add("Password");
model = new DefaultTableModel(data, header);
table = new JTable(model);
st.close();
rs.close();
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.white);
scroll = new JScrollPane(table);
getContentPane().add(scroll);
st.close();
rs.close();
}
//adding data to database
void addDoctor()
{
st.executeUpdate("INSERT INTO tblInfo(Name) VALUES ('Name')");
st.close();
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
else if(btnAdd == source)
{
addDoctor();
refresh();
}
感谢您的任何回复。:) 我在发布之前已经编辑了这段代码。