我創建了一個窗口,其中包含一個填充了mysql數據的jtable。點擊時還有一個按鈕'LOAD TABLES',一個Jdialog彈出讓用戶從JComboBox中選擇另一個表!一切工作都很好,但問題是,當從JComboBox中選擇一個表並單擊Jdialog OK按鈕時,JTable不會從新表中加載數據(不刷新)!以下是簡化代碼!如何更新JDialog中的組合框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");
}
};
}
你對'dataTable'的結果做了什麼? – MadProgrammer