Results 1 to 9 of 9

Thread: Multiple Views

  1. #1
    MrDave is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Multiple Views

    I have an application that needs to display different views in the same JFrame.

    I tried creating a class which loads the JFrame and then have classes which inherit JPanel for each view I want to display.

    I can load the first view (MainMenu) by adding it to the JFrame but I can't figure out how to add the next view to the JFrame when the user clicks a button in the MainMenu JPanel.

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    JFrame2.setVisible(true);
    JFrame1.setVisible(false);

    Is that what you mean?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Try using a CardLayout

    I think that you want to look up the Sun Swing tutorial that covers CardLayout as it will allow you to swap JPanels or any JComponent easily. You can find it here: CardLayout tutorial
    Last edited by Fubarable; 04-08-2010 at 10:29 PM. Reason: Link added

  4. #4
    MrDave is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Cheers for the link but I still have the same problem.

    The GUI class has the cardLayout and controls what is loads into the JFrame.

    The mainMenu class loads at first but I need to make it so when a button is clicked in mainMenu is tells the GUI class to load the next view.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you give your gui classes the appropriate public methods and allow your controller code (the action listeners for the menus) have a reference to the gui's, I think that you can solve this. If not, consider creating an sscce (see my link in my signature below) and post it here.

  6. #6
    MrDave is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks for the help, I've finally solved the problem...just for reference this is what I did.

    I was able to solve the problem using "singleton pattern" for the GUI class since I only ever need one instance of it.

    I added a getInstance() and changeView() method to the GUI class which allowed me to change the view of the GUI instance from any other class via GUI.getInstance().changeView()

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by MrDave View Post
    I was able to solve the problem using "singleton pattern" for the GUI class since I only ever need one instance of it.
    Sorry, but this smells of bad design. Are you sure you want to do this and not simply pass a reference?

  8. #8
    MrDave is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Sorry, but this smells of bad design. Are you sure you want to do this and not simply pass a reference?
    Yep sorry that is exactly what I wanted to do, I missed the obvious and made it complicated.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by MrDave View Post
    Yep sorry that is exactly what I wanted to do, I missed the obvious and made it complicated.
    The way I would do it is define a 'View' interface:

    Java Code:
    public interface View {
       public void display(Data data); // whatever Data may be
       // more methods here ...
    }
    All view implementations would implement this interface; when I really go fancy I define a ViewFactory that gives my a View given an ID or whatever:

    Java Code:
    public abstract class ViewFactory {
       public static View createView(ID id) { ... }
    }
    Note that this class is abstract and just contains a static method that gives me the View I want; there is no need for more fancy patterns such as abstract factories etc.

    Anything that needs a View can consult the ViewFactory and voila.

    kind regards,

    Jos

Similar Threads

  1. multiple views in one folder tab in RCP app
    By natelapp in forum Eclipse
    Replies: 1
    Last Post: 08-02-2009, 12:48 PM
  2. Multiple views on user interface application
    By dand_dd in forum SWT / JFace
    Replies: 36
    Last Post: 04-24-2009, 10:05 AM
  3. Relation between 2 views
    By tojas in forum SWT / JFace
    Replies: 0
    Last Post: 04-19-2009, 04:22 AM
  4. List views, a type of object
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 04:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •