• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best Method for Second Interface Screen

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

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.

Any help appreciated
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRanch!

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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic