3

i need some advice on JDialog stuff.

I need to catch the "input" data, and i am wondering, what is the best way to do it. Ok, here is complete problem. When user clicks the button (located on JFrame), JDialog pops out, and there are 3 text fields for entrys and one combo box. After user presses ok, i need to return those data in JFrame. Well, i can do this on my way, but thats too much of spaghetti code :)) What listener should i use? And if you are willing to give me code example i would be thankful :)

hrza
  • 85
  • 7

1 Answers1

1

Hmm there are a lot of ways of solving it, maybe you can try a delegate approach with an interface like the following one:

public interface DialogListener {
    public boolean okClicked(String input1, String input2, String input3, String combo);
    public void cancelClicked();
}

(The boolean is if you decide to wether dismiss the dialog or not).

Implement that in your frame or frame controller if you're using a MVC approach and add that component as a listener of your custom dialog.

Another approach would be to set properties on the dialog and then your frame or controller could query the dialog for the input values.

This depends on how generic you want things to be.

You could also have someting like this which is more generic and reusable but less convenient:

public interface DialogListener {
    public boolean okClicked(JDialog dialog, Map<String, JComponent> components);
    public void cancelClicked();
}