2016-06-08 162 views
-1

我目前正在使用使用JTabbedPane的Java應用程序。如何在項目選項卡被點擊時刷新JTabbedPane選項卡?

我想刷新一個標籤JTabbedPane,但是當我點擊「標籤」項目時標籤不刷新。

可能是什麼問題?

public class MainClass 
    extends  JFrame 
{ 
private  JTabbedPane tabbedPane; 
private  JPanel  panel1; 
private  JPanel  panel2; 
private  JPanel  panel3; 
public  JPanel  panel4; 
private  JPanel  panel5; 

public MainClass() 
{ 


    setTitle("Demandes d'autorisation d'absence"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(800 ,400 ); 
    setBackground(Color.gray); 

    JPanel topPanel = new JPanel(); 
    topPanel.setLayout(new BorderLayout()); 
    getContentPane().add(topPanel); 

    // Create the tab pages 
    createPage1(); 
    createPage2(); 
    createPage3(); 
    createPage4(); 
    createPage5(); 

    // Create a tabbed pane 
    tabbedPane = new JTabbedPane(); 
    tabbedPane.addTab("Mise à jour", panel1); 
    tabbedPane.addTab("Demandes en cours", panel2); 
    tabbedPane.addTab("Demandes autorisées", panel3); 
    tabbedPane.addTab("Demandes autorisées mise à jour", panel4); 
    tabbedPane.addTab("A propos", panel5); 
    topPanel.add(tabbedPane, BorderLayout.CENTER); 
} 

public void createPage1() 
{ 
    panel1 = new JPanel(); 
    panel1.setLayout(null); 

    JLabel label1 = new JLabel("Mise à jour des autorisations :"); 
    label1.setBounds(10, 15, 300, 20); 
    panel1.add(label1); 


    JButton b = new JButton("Actualiser"); 
    b.setBounds(10, 55, 150, 20); 
    b.addActionListener(new miseajour()); 
    panel1.add(b); 

} 

public void createPage2() 
{ 
    panel2 = new JPanel(); 
    panel2.setLayout(null); 

    ArrayList columnNames = new ArrayList(); 
     ArrayList data = new ArrayList(); 

     // Connect to an MySQL Database, run query, get result set 
     Connection con = null; 
     Statement st = null; 
     ResultSet rs = null; 

     String url = "jdbc:mysql://localhost:3306/Autorisations"; 
     String user = "root"; 
     String password = "root"; 

     try { 
      con = DriverManager.getConnection(url, user, password); 
      st = con.createStatement(); 
      rs = st.executeQuery("SELECT * FROM Autorisations where etat='en cours'"); 
     { 
      ResultSetMetaData md = rs.getMetaData(); 
      int columns = md.getColumnCount(); 

      // Get column names 
      for (int i = 1; i <= columns; i++) 
      { 
       columnNames.add(md.getColumnName(i)); 
      } 

      // Get row data 
      while (rs.next()) 
      { 
       ArrayList row = new ArrayList(columns); 

       for (int i = 1; i <= columns; i++) 
       { 
        row.add(rs.getObject(i)); 
       } 

       data.add(row); 
      } 
     }} 
     catch (SQLException e) 
     { 
      System.out.println(e.getMessage()); 
     } 

     // Create Vectors and copy over elements from ArrayLists to them 
     // Vector is deprecated but I am using them in this example to keep 
     // things simple - the best practice would be to create a custom defined 
     // class which inherits from the AbstractTableModel class 
     Vector columnNamesVector = new Vector(); 
     Vector dataVector = new Vector(); 

     for (int i = 0; i < data.size(); i++) 
     { 
      ArrayList subArray = (ArrayList)data.get(i); 
      Vector subVector = new Vector(); 
      for (int j = 0; j < subArray.size(); j++) 
      { 
       subVector.add(subArray.get(j)); 
      } 
      dataVector.add(subVector); 
     } 

     for (int i = 0; i < columnNames.size(); i++) 
      columnNamesVector.add(columnNames.get(i)); 

     // Create table with database data  
     JTable table = new JTable(dataVector, columnNamesVector) 
     { 
      public Class getColumnClass(int column) 
      { 
       for (int row = 0; row < getRowCount(); row++) 
       { 
        Object o = getValueAt(row, column); 

        if (o != null) 
        { 
         return o.getClass(); 
        } 
       } 

       return Object.class; 
      } 
     }; 

     JScrollPane sp = new JScrollPane(table); 
     sp.setBounds(10,25, 800, 1000); 
     panel2.add(sp); 

} 

public void createPage3() 
{ 
    .... 

} 

public void createPage4() 
{ 
    .... 

} 

public void createPage5() 
{ 
    ... 

} 


// Main method to get things started 
public static void main(String args[]) 
{ 
    // Create an instance of the test application 
    MainClass mainFrame = new MainClass(); 
    mainFrame.setVisible(true); 
} 
+0

你是什麼意思刷新? –

+0

在第一個選項卡中,我執行一些SQL查詢,並在第二個選項卡中我有一個Jtable,我希望這個更新時,點擊選項卡 –

+0

你必須執行並再次填充 –

回答

1

如果你想知道何時選中了一個標籤,那麼你可以使用ChangeListener

tabbedPane.addChangeListener(...); 

然後,當事件觸發時,您可以獲取選項卡窗格的選定索引並進行處理。

相關問題