similar input dialog-creation patterns/swing - java

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 has 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.

Related

How can I make JLabel selectable without destroying accessibility?

We want to be able to select the text on some labels.
At the moment, we're using a hack where we extend JEditorPane and then try to style it to look identical to a JLabel. This seems to work fine, but really it just lulls you into a false sense of security until a user using a screen reader asks you why you're inserting an editor which is never editable. Which, funnily enough, is exactly what has happened.
As it turns out, getAccessibleContext() for an editor pane returns the context for an editor pane, unsurprisingly. I know I can override that method and reimplement the entirety of JLabel.AccessibleJLabel myself, but there are a lot of things to implement, and I feel like there must be a better way.
Is there, in fact, a better way? e.g., it seems like I should be able to do it via the Look & Feel, but maybe there is an even easier trick.
I guess ideally, I would have hoped that there were just a property on JLabel to allow making the text selectable directly, allowing us to both keep the semantics/accessibility of adding a label to the view, while also allowing us to keep the usability.

Collect Input from a Java JTextarea

I'm working on one of my first java applets and I want to start of fairly simple (though I have a good understanding of how code works I dont know much in terms of what methods I all have at my disposal when using java)
I have created a Jframe window that has a JTextarea in it. I would like to execute certain lines of code when certain things are typed into this box. In essence, its a simple text input system. How would I go about doing this or is there a better component to use for this?
in addition to getText(), for JTextField some prefer the getDocument() method. In Java, Listeners are used to capture events, such as "what was typed to the text area". This tutorial will get you started, if you have trouble implementing you can come back with a more specfic question and some code :)

Adding 2 groups of JRadioButton to be corresponding the same

I am doing a simple GUI painting box program.
However, I have a problem with adding 2 similar separated groups to be corresponding in the same way.
I mean when I click the JRadioButtonMenuItem Line, then the JRadioButton Line below also has to be selected too. What should I do?
Do you need to see my code?, please let me know
Thank you so much.
P/s: it says I need 10 reputation to post image
Share the model between the two radio buttons:
JRadioButton radioButton = new JRadioButton("Line");
JRadioButtonMenuItem radioMenuItem = new JRadioButtonMenuItem("Line");
radioMenuItem.setModel( radioButton.getModel() );
Actually, you should share the Action as well between the two components. Read the section from the Swing tutorial on How to Use Actions for more information and examples.
The exact solution depends a lot on how your code is structured right now. I bet that the standard library has some functionality to accomplish what you want to do, but if you want to go ahead and implement it then you might as well (minimal time input and you learn something).
The most direct solution that comes to my mind is to encapsulate selecting a button in a method that will manipulate all sets of corresponding buttons. I am going to assume that you are using action listeners for the buttons right now, if not you could adapt the idea. In the action listener, you can detect the mouse click and perform some work as necessary. That work should include updating the other buttons appropriately too. You could even create a method that both action listeners call and updates all necessary sets of buttons.
It is also possible to use the same action listener on both sets of the buttons, but you'll need to know which selection the user wants to be active (likely an easy task).
My Java is pretty rusty, so I am not including any example code, but if anything is unclear or you think an example would help I can do so.
Hope at least something here helps you. Good luck!

containers to use for JTabbedPanes

I am developing a math game and I was thinking about displaying the data like so:
Display the data that the user is to screencap and send to me, in a JFrame. This data will be displayed in the form of two JTabbedPanes, each tab showing a table (with five rows and 8 columns)
Ask the user (via JOptionPane.showConfirmDialog()) if they would like to see a graphical representation of their progress.
If they click "Yes", then show them a JFrame with their chart. This JFrame will have a JMenuBar with only one JMenu with two JMenuItems (an option for both of the two modes).
In this new JFrame, display via CardLayout the active JTabbedPane (the FloatMode has four difficulties, while the IntMode has all the FloatMode's difficulties plus Elementary School difficulty.
I know that multiple JFrames can be bad practice, and I was thinking about using JSplitPanes, but then it might get too crowded (and I wouldn't be able to let the users choose the mode!). Would a JDialog be recommended, or would you recommend something else? //I apologize if it sounds like I am asking you all to do some of my thinking for me...
Seems best suited to a CardLayout with a single control to select between cards (e.g. a JList or JComboBox, JSpinner, menus etc.).
E.G. as seen in this short example, that uses radio buttons in a button group to select the card.
I might have a potential solution: make the JMenuBar universal! That would eliminate the need for two JTabbedPanes and thus speed up performance (due to less Objects floating around, especially expensive GUI Objects)!

Swing JTable Hack

I have a fairly complicated JTable subclass (WidgetTable and its WidgetTableModel) that works fine when I add it to a dummy JPanel for testing purposes.
Since I am absolutely horrid at working with LayoutManagers, I like to use the NetBeans built-in GUI Builder for all my layout work. Then I usually just code-around the autogenerated (GUI builder) code and that has always worked for me. It is the best of both worlds: I get my presentation looking exactly the way I want it, and I also get fine-grained control over the componentry.
However, I have never used the GUI Builder tool to make tables. After tinkering around with it for a while last night, it looks as though it is only good for making pretty basic (fixed # of rows, fixed # of columns, etc.) JTables.
My WidgetTable actually has a dynamic number of both rows and columns, special editors/renderers and many other bells and whistles.
My problem:
I have two conflicting constraints: (1) I need to use the GUI builder to position and size the table exactly where I want it in the container, but, (2) The table component available through the GUI builder is too basic to handle my WidgetTable.
I need a way to design a "table placeholder" into my container with the GUI builder, such that, once NetBeans autogenerates that placeholder code, I tweak the code and instruct it to dynamically instantiate one of my WidgetTables instead, consuming the location and size that I defined the placeholder component to take up.
This way I can have my cake, and eat it too. The only problem is, I don't think the GUI builder supports this ability to drag-n-drop abstract JComponents, position and size them, and then plug subclasses into them elsewhere in the codebase.
Anybody ever have this problem before or have any interesting recommendations? I imagine the best thing to do would be for me to just roll up my sleeves and learn LayoutManagers, but I'm mostly a server-side developer and only come over to the client-side every once in a blue moon; and honestly, don't have the energy to learn the intricacies and nastiness of GroupLayout and its sinister cousins.
Thanks for any help!
Insert a JTable using the GUI builder, reset its model property to the default value, and tweak the construction code so that it looks like
jTable1 = new WidgetTable(this.widgetTableModel);
You may tweak the creation code by right-clicking on the JTable, selecting "Customize code", choosing "custom creation" instead of "default code" in the first combo box, and typing the code for the constructor call.
If you need your jTable1 variable to be of type WidgetTable rather than JTable, edit the "Variable declaration code" in the same dialog box.
NetBeans also allows you to create custom components for building UIs. This may be more work than you want to put into your WidgetTable, but if you think you're going to have to build more UIs with custom components, it could be worth learning.
I do this all the time. I have an subclassed JTable that I use with the GUI editor and it is Dynamic.
Add a JTable to your project using the GUI editor and the layout of your choice.
Once the table is added, right click on it and click on custom code.
In the constructor of the JTable, change it to say new WidgetTable(new WidgetModel()) instead of new JTable(new DefaultTableModel()).
Create a global variable for you WidgetTable. Something like private WidgetTable widgetTable;
In you constructor, after the call to initComponents(), cast your JTable to a Widget table and use that from now on.
`widgetTable = (WidgetTable)jTable1;

Categories

Resources