2016-05-24 8 views
0

私は自分のアプリケーションのGUIを設計しています。私はSwing Components間の通信にいくつか問題があります。 私はクラスMainGUIを持っています。ユーザーがクリックすると、新しいJDialogオブジェクトを作成して表示する必要があります。ダイアログは私が望むようにポップアップしますが、前回私が訪れたときに入力された以前の値が表示されます。それを防ぐ方法とその行動の理由は何ですか?私はopenActorDialog()関数に何か間違っていると思います。あなたはそれを使用した後、再度nullにダイアログを設定するか、またはあなたがそれを使用する前に、そうでなければ、古いダイアログが表示されますする必要がなぜJPanelコンポーネントが自動入力されるのですか?

public class MainGUI extends JPanel { 
private ArrayList<Actor> actorList = new ArrayList<Actor>(); 
private JTextField field = new JTextField(10); 
private JButton openDialogeBtn = new JButton("Open Dialog"); 
private String description; 
// here my main gui has a reference to the JDialog and to the 
// MyDialogPanel which is displayed in the JDialog 

private JDialog dialog; 

public MainGUI() { 

    setLayout(new BorderLayout(0, 0)); 
    add(openDialogeBtn); 
    field.setEditable(false); 
    field.setFocusable(false); 

    add(field); 

    JMenuBar menuBar = new JMenuBar(); 
    add(menuBar, BorderLayout.NORTH); 

    JMenu mnFile = new JMenu("File"); 
    menuBar.add(mnFile); 

    JMenu mnNew = new JMenu("New"); 
    mnFile.add(mnNew); 

    JMenuItem mntmSystem = new JMenuItem("system"); 
    mnNew.add(mntmSystem); 

    JMenuItem mntmUseCase = new JMenuItem("use case"); 
    mnNew.add(mntmUseCase); 

    JMenuItem mntmActor = new JMenuItem("actor"); 
    mnNew.add(mntmActor); 

    JTree tree = new JTree(); 
    add(tree, BorderLayout.WEST); 

    /* 
    * Here we set the actions to be performed when the user interacts 
    */ 

    mntmActor.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      openActorDialog(); 
     } 
    }); 

} 

private void openActorDialog() { 
    //Creation of the JDialog 
    CreateActorDialog createActorPanel = new CreateActorDialog(); 
    if (dialog == null) { 
     Window win = SwingUtilities.getWindowAncestor(this); 
     if (win != null) { 
      dialog = new JDialog(win, "Create an actor", ModalityType.APPLICATION_MODAL); 
      dialog.getContentPane().add(createActorPanel); 
      dialog.setSize(500, 360); 
      dialog.setLocationRelativeTo(null); 

     } 
    } 
    dialog.setVisible(true); // here the modal dialog takes over 

    // this line starts *after* the modal dialog has been disposed 
    // **** here's the key where I get the String from JTextField in the GUI 
    // held 
    // by the JDialog and put it into this GUI's JTextField. 

    Actor a = new Actor(createActorPanel.getActorNameFromDialog(), 
      createActorPanel.getActorDescriptionFromDialog()); 
    field.setText(createActorPanel.getActorNameFromDialog()); 
    actorList.add(a); 
    System.out.println("It is written:" + createActorPanel.getActorDescriptionFromDialog()); 
    for (Actor actor : actorList) { 
     System.out.println(actor.name + " with description :" + actor.description); // Will 
                        // invoke 
                        // override 
    } 

} 

}

+0

「CreateActorDialog」は何をしていますか? – MadProgrammer

+0

CreateActorDialogは、JPanelを拡張し、ユーザが値を設定するいくつかのテキストフィールドを持つクラスです。 – Teo

+0

'dialog'は一度作成されると常に同じになります。ダイアログ上で' createActorPanel'のインスタンスを置き換えることはないので、最後に表示されたときの古い値が含まれます。 (古いものを削除することを確認して)コンポーネントを置き換えるか、値をリセットする何らかの「リセット」メソッドを使用する – MadProgrammer

答えて

2

+0

Ahh、私これに気付かなかった...今は正常に動作している! – Teo

関連する問題