开发者

similar input dialog-creation patterns/swing

开发者 https://www.devze.com 2023-01-25 05:08 出处:网络
I have a created a UI in swing using NetBeans. There are some use cases where the user presses buttons and a JDialog appears as a result.

I have a created a UI in swing using NetBeans.

There are some use cases where the user presses buttons and a JDialog appears as a result.

The JDialog accepts input via JComboBox, at least 4.

E.g.

User presses "ButtonA" and a JDialog appears that displays the following:

"select X:" combo  
"select Y:" combo  
"select Z:" combo  
"select O:" combo  etc  

I.e. beside each combo is a descriptive label. There are a few other controls in each JDialog besides each combo, i.e. JCheckbox, JTextarea

If user presses "ButtonB" a JDialog appears that displays the following:

"select A:" combo  
"select B:" combo  
"select C:" combo  etc  

So the dialogs are not the same but have some pattern in their presentation format. I.e. usage of comboboxes.

Initially I though to create a single JDialog and pass some arguments for the text it should display on the labels of the JDialogs and whether the JCheckbox should appear or not, if the JTextArea should appear or not and with what text etc, all depending on parameters passed in the initialization of the JDialog so as to have a single class for all use cases, but the code started getting really complicated and I rejected this approach.

What I did was create via GUI designer a JDialog, exactly as needed for each button press and came up with about 10-11 such JDialogs.

This appoach h开发者_C百科as made the programming much simpler but I do not know if it is a standard way to deal with this since I created 10-11 extra classes for the input.

My question is, whether what I did is reasonable, or there are better ways to deal with situation like this.

Thank you!


I think I would write a DialogBuilder class that returns a JDialog.


EDIT:

Visual vs Programatic creation of Dialogs

I'm used to generating Dialogs by writing code. before Java I used to code in Delphi in which Dialogs were created using a GUI - so initially I did find this annoying and even tedious. Nowadays I rather enjoy it. So yes, I would probably throw out (most of) the dialog code created by netbeans. Unless your dialogs are more complicated than your question suggests.

Complexity

Yes, there is a danger that you'll end up taking a lot of time to create quite complex code. However, if you are like me, you'll learn a lot doing it. One thing you'll learn is how to use various patterns to avoid that complexity. Finding an elegant way to solve those issues is something I find pretty satisfying - it's one of the aspects of programming I find most enjoyable.

Parameters

When you mention your concern about "if argA&& argB && !argC then display JCheckbox etc for 10-11 different jdialogs" - I can only suggest you try to approach the problem from a different direction. For example, you could pass the DialogBuilder a list of pairs of labels and JComboboxes. Or maybe you could extend JCombobox so that it has an additional field and a getter for it's label.

e.g.

DialogBuilder builder = new DialogFactory();
builder.add("Select X:", new Xcombo(...), true);  // with checkbox
builder.add("Select Y:", new Ycombo(...), false); // without
...
builder.getDialog.setVisible(true);

or

builder.add("Select X:", new MyCombo(xList), false);
builder.add("Select Y:", new MyCombo(yList), false);

You can see (I hope) that the DialogBuilder.add(String, JComboBox, boolean) method wouldn't need dozens of complex if ... then ... else clauses.

Just my $0.02 worth.

0

精彩评论

暂无评论...
验证码 换一张
取 消