2013-04-11 58 views
0

我想在另一個JFrame中按下按鈕時刷新JList。從另一個JFrame中刷新Jlist

所以我有一個JFrame GuiBoss管理員工(添加,刪除,更新)。當我按下按鈕添加,另一個Jframe打開,在我創建一個新的員工。

//打開「add_form」,在其中給出有關新員工的詳細信息。

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {          
    GuiBoss gb = new GuiBoss(contrb,boss); 
    Add_form af = new Add_form(gb,contrb,boss); 
    af.setVisible(true); 

} 

//刷新列表並添加新員工。

public void refresh(Employee e){ 
    System.out.println("I reach this point!"); 
    //if i print e.getName() it works, printing the right name that i give in the "add_form" 
    listModel.addElement(e); 
    //listModel.clear(); //don't work either. 

} 

我的問題是,當我從GuiBoss框架,消息提交有關新員工我調用該函數刷新(員工E)的細節(「我到了這個地步!」)在控制檯上顯示出來,listModel的大小會改變,但它不會刷新的列表。 另外我必須說,我正確地爲列表設置模型。

//採取從主框架( 「GuiBoss」)從形式和呼叫刷新(員工E)數據

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {          
    //String Id = txtID.getText(); 
    String UserName = txtName.getText(); 
    txtHour.setVisible(false); 
    boolean b = false; 
    if(rbtnYes.isSelected() == true){ 
     b = true; 
    } 
    if(rbtnNo.isSelected() == true){ 
     b = false; 
    } 
    if(rbtnYes.isSelected()==false && rbtnNo.isSelected() == false){ 
     System.out.println("Select the presence!"); 
    } 
    else{ 
     txtOra.setVisible(true); 
     String Hour = txtHour.getText(); 
     e = new Employee(UserName,b,Hour,boss); //boss i get from main frame when i start this add new employee form 
     contrb.addEmployee(e); 
     gb.refresh(e); //gb is of type GuiBoss were i have the function that does  
     // the refresh 
    } 
} 

請讓我知道如果u有任何ideeas.Thanks。

+1

*「當我在另一個JFrame中按下一個按鈕,」 *應該只有一個!請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) – 2013-04-11 22:31:07

回答

2

爲什麼不使用模式JDialog來收集有關新員工的信息,而不是彈出另一個框架。關閉對話框後,您可以從對話框中提取詳細信息,並從當前幀中刷新列表。

這樣可以避免不必要地暴露部分API。

查看詳情How to use Dialogs

更新

假設你已經正確設置了模式,那麼你的代碼應該工作......按照本例...

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.DefaultListModel; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.ListModel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestList03 { 

    public static void main(String[] args) { 
     new TestList03(); 
    } 

    public TestList03() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private DefaultListModel model; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      model = new DefaultListModel(); 
      JList list = new JList(model); 
      add(new JScrollPane(list)); 
      JButton btn = new JButton("Add"); 
      btn.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        model.addElement("New Element"); 
       } 
      }); 
      add(btn, BorderLayout.SOUTH); 
     } 

    } 

} 

這將表明,有別的東西錯了,你沒有向我們展示了......

更新,其中可能的修復參考問題

這基本上演示了將主面板的引用傳遞給子工廠,該子工廠負責將值實際添加回主面板。通常我會使用某種interface,而不是將整個面板暴露出來以提供對單一方法的訪問,但這是一個簡單的例子。

它使用正常的implements和內部類作爲ActionListener來演示將「自我」的引用傳遞給另一個類的兩種最常見的方法。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.DefaultListModel; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.ListModel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestList03 { 

    public static void main(String[] args) { 
     new TestList03(); 
    } 

    public TestList03() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel implements ActionListener { 

     private DefaultListModel model; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      model = new DefaultListModel(); 
      JList list = new JList(model); 
      add(new JScrollPane(list)); 

      JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER)); 

      JButton btn1 = new JButton("Add 1"); 
      btn1.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        new Factory(TestPane.this, "Added by Button 1"); 
       } 
      }); 
      buttons.add(btn1); 

      JButton btn2 = new JButton("Add 2"); 
      btn2.addActionListener(this); 
      buttons.add(btn2); 

      add(buttons, BorderLayout.SOUTH); 
     } 

     public void addItem(String text) { 
      model.addElement(text); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      new Factory(TestPane.this, "Added by Button 2"); 
     } 
    } 

    public class Factory { 

     public Factory(TestPane testPane, String text) { 
      testPane.addItem(text); 
     } 
    } 
} 
+0

如果我在打開我的「add_form」表單之前隨時使用我的列表和listModel,它就可以工作。我甚至在主框架啓動時填充模型並列出存儲庫中的員工。我正在尋找工作方式我們使用對話框(謝謝你的提示)。 – user2271933 2013-04-11 20:43:43

+0

聽起來像你有一個參考問題。我懷疑GuiBoss gb =新的GuiBoss(控制,老闆);打電話 – MadProgrammer 2013-04-11 21:04:14

+0

我認爲你是對的,但我需要通過一個GuiBoss因爲,我必須從第二frame.Or調用刷新功能是否有任何其他方式,我可以在不調用添加到ListModel的從第二幀(ADDFRAME)主要一個(GuiBoss)? – user2271933 2013-04-11 21:13:56