我创建了一个窗口,其中包含一个填充了 mysql 数据的 jtable。当点击 Jdialog 时,还有一个按钮“加载表”,让用户从 JComboBox 中选择另一个表!一切正常,但问题是当从 JComboBox 中选择一个表并单击 Jdialog OK 按钮时,JTable 没有从新表中加载数据(不刷新)!下面是简化代码!!
请任何帮助!!!!
类 mysqlData 扩展 JFrame {
Font buttonFont = new Font("Arial",Font.PLAIN, 16);
Font labelfont = new Font("Arial",Font.PLAIN, 24);
Connection connecting;
public String tableName = "Examination";
static final String Select = "Available Mysql Tables";
//connection statements
mysqlData()
{
Connections con = new Connections();
connecting = con.connect;
System.out.print("value of input is: " + tableName);
}
//creating jtable and populating it with data from mysql database
public JScrollPane dataTable() throws SQLException
{
Statement st = connecting.createStatement();
ResultSet result = st.executeQuery("Select * from " + tableName);
ResultSetMetaData md = result.getMetaData();
int columnCount = md.getColumnCount();
Vector<String> data = new Vector<String>();
Vector<String> column = new Vector<String>();
//JOptionPane.showMessageDialog(null,"Wrong username or password" + tableName,"Failed!",JOptionPane.ERROR_MESSAGE);
DefaultTableModel tablemodel = new DefaultTableModel(data,column);
tablemodel.setRowCount(0);
tablemodel.setColumnCount(0);
for(int i=1; i<=columnCount; i++)
{
tablemodel.addColumn(md.getColumnName(i));
}
while(result.next())
{
Vector<String> row = new Vector<String>(columnCount);
for(int i=1; i<=columnCount; i++)
{
row.add(result.getString(i));
}
tablemodel.addRow(row);
}
JTable table = new JTable(tablemodel);
table.setPreferredScrollableViewportSize(new Dimension(900,780));
JScrollPane scrolPane = new JScrollPane(table);
return scrolPane;
}
//a button that pops up a jdialog and lets users to select a table from jcombo box
public JButton tablesButton()
{
JButton tablesListButton = new JButton("LOAD TABLES");
tablesListButton.setFont(buttonFont);
tablesListButton.addActionListener(listener);
return tablesListButton;
}
//actionlistener for the tablesButton
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String[] tables = new String[]{"Attendance","Examination","Students","Subjects","Teachers","Salary"};
tableName = (String) JOptionPane.showInputDialog(mysqlData.this,"Please select your favorite sport",Select, JOptionPane.INFORMATION_MESSAGE, null,tables,"Attendance");
}
};
}