I have only been learning Java for a month or so, so apologies if this question is bit silly or if I get some of my terminology wrong.
I have recently been learning about Swing components. I am building a small program that uses a JFrame as it's main interface (which I built using Netbeans). When a certain button is pressed I want another menu to open, which will allow the user to add/remove items from an array (it will need a JList, a text input field and two buttons).
What is the best way to do this? I have read that it is bad coding practice to open multiple JFrames at once for various reasons. Should I be using JOptionPane or Dialog instead? Every tutorial I can find on using these seems to involve using them to display simple messages or a simple user input (such as Yes/No). I haven't found a tutorial on how to inbed other jComponents inside, or even to what extent it is possible.
I agree you shouldn't just open new JFrames willy-nilly, because users are bound to get annoyed at such a thing.
The same goes for modal pop-ups within a JFrame, though. Furtunately there are other options. You could use a CardLayout to switch out (a part of) the content of a JFrame. There's also JDesktopPane/JInternalFrame, if a multiple-document interface makes more sense. Or a JTabbedPane if working with tabs would be the most user friendly option.
I've moved your topic to our Swing / AWT / SWT forum by the way
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ian Taylor wrote:Should I be using JOptionPane or Dialog instead? Every tutorial I can find on using these seems to involve using them to display simple messages or a simple user input (such as Yes/No). I haven't found a tutorial on how to inbed other jComponents inside, or even to what extent it is possible.
Something missed by most beginners, as it's not really clear from any of the popular tutorials, is that the message argument to JOptionPane's show...Dialog(...) methods is declared as Object. JOptionPane is one of the smartest classes in the JDK: it can deal with a Component or an array as a message, not just with simple Strings.
I suggest you write a small class just with a main(...) method that creates a message, and displays it in a JOptionPane's dialog. Its capabilities may impress you. If you're not sufficiently impressed, try an array (Component or JComponent) filled with different subclasses.
There are no new questions, but there may be new answers.
Joined: Sep 03, 2012
Thanks for the replies and thanks Jelle for moving my post to the correct forum.
I have read all the options that you suggested Jelle, but none of them really seem to suit my needs. I want it so that the new interface pops open in a new window, not a new tab or a new section of the old window or anything like that. It's not like they will be opening willy-nilly, it's just this one screen that will be treated as a separate interface.
Darryl thanks for the reply, but unfortunately I don't understand much of what you said. I am very new to Java and object programming in general (I posted this in the newbie forum, before Jelle moved it). If I understand you right you seem to be suggesting that the 'message' displayed in a JOptionPane can be an object rather than just a string. Can it handle more than one object? As described above, could I make a JOptionPane that could handle a JList, two buttons and a text input? I can't find any tutorial or sample code that shows how to do this.
Ian Taylor wrote:As described above, could I make a JOptionPane that could handle a JList, two buttons and a text input?
You can put those components in a JPanel, using an appropriate layout manager or combination of nested layouts, and pass the panel as the message argument.
You can find the description of how the message and other parameters are interpreted and used in the opening paragraphs of the API for JOptionPane (<- click that if you don't know where to find the API).
Joined: Sep 03, 2012
Cheers, I'll check out that possibility. I have the second JFrame running okay independently, it's just linking the two together I am having problems with.