2013-09-01 21 views
0

我創建了一個窗口,其中包含一個填充了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"); 


     } 
    }; 

}

+0

你對'dataTable'的結果做了什麼? – MadProgrammer

回答

0

我已經做了你的榜樣一些重構由於編譯錯誤。下面 代碼應工作:

ActionListener listener = new ActionListener() { 
    public void actionPerformed(ActionEvent event) { 
     String[] tables = new String[] { "Attendance", "Examination", "Students", "Subjects", "Teachers", "Salary" }; 

     tableName = (String) JOptionPane.showInputDialog(null, 
         "Please select your favorite sport", "Attendance", 
         JOptionPane.INFORMATION_MESSAGE, null, 
         tables, tables[0]); 
     JOptionPane.showMessageDialog(null, "You have selected: " + tableName); 

     System.out.println("value of input is: " + tableName); 
    } 
}; 

以供將來參考,這裏設置的JOption/InputPane例子:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

HTH

+0

感謝您的快速回復!但它不是答案!我想用tableName的新值刷新JTable; –

0

你必須做很多重構的我想,我會給你一些提示來做到這一點。

1)的主要問題是,在您的ActionListener當一個表

tableName = (String) JOptionPane.showInputDialog(mysqlData.this,"Please select your favorite sport",Select, JOptionPane.INFORMATION_MESSAGE, null,tables,"Attendance"); 

你用它做任何事情,你必須刷新tableModel。你必須再次調用

Statement st = connecting.createStatement(); 
     ResultSet result = st.executeQuery("Select * from " + tableName); 
     ResultSetMetaData md = result.getMetaData(); 

而且你要訪問您的當前JTable

DefaultTableModel tableModel = (DefaultTableModel)jtable.getModel(); 
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每次的新實例,你只需要改變模型。