Results 1 to 9 of 9
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Make a variable interface

    I have recently completed some coursework for a computing class which I used Java to complete (Could have been any language).

    I used basic Swing GUI.

    My biggest issue with the program was that when I clicked a button, in order to progress I usually had to open a new window and close the current one as I could not figure out how to have the objects within the active JFrame merely change. For example, I may have wanted a main menu with multiple buttons but as one button was clicked I wanted the buttons to disappear and a new menu appear all within the same JFrame.


    How can I do this?


    The only example I can think of at the moment is CCleaner (I know it probably isn't java) where clicking the 4 buttons on the left hand side will change the windows content.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Make a variable interface

    CardLayout

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Make a variable interface

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Make a variable interface

    Quote Originally Posted by DarrylBurke View Post
    CardLayout

    db
    Thanks, just had a look through it.

    So when i use any Java program like this it is actually loading every possible interface at once but layering them and only showing the top one at any one time?


    I was considering making a text-based game using buttons to choose options; for this I was hoping to have hundreds of different interfaces (Different numbers of buttons each containing different text). Would this means I would have to create hundreds of different interfaces and have them essentially all loaded at once?

    THis doesn't sound very efficient.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: Make a variable interface

    You're trying to work around the fact that you don't know how to rebuild the same panel; don't do that, do the right thing and figure it out. It can be as simple as replacing one JPanel with another one (note: replace = remove old one, add new one) and then letting the JFrame do its layout again, for example by calling pack() again.

    Because also for this game of yours, which is a cool idea btw., it is simply easier and more efficient to use one JPanel that you rebuild to fit your needs. In my experience, of course.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Make a variable interface

    Quote Originally Posted by gimbal2 View Post
    You're trying to work around the fact that you don't know how to rebuild the same panel; don't do that, do the right thing and figure it out. It can be as simple as replacing one JPanel with another one (note: replace = remove old one, add new one) and then letting the JFrame do its layout again, for example by calling pack() again.

    Because also for this game of yours, which is a cool idea btw., it is simply easier and more efficient to use one JPanel that you rebuild to fit your needs. In my experience, of course.
    This is exactly what I wanted in the first place. I just got the impression that it wasn't possible and that cardlayout had to be used instead.
    What do I need to research (What is this procedure actually called) to get to do this?

    Thanks
    Last edited by kkid; 07-29-2013 at 08:42 PM.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: Make a variable interface

    Try a google for:

    java swing replace panel

    See how that works? Always start your search query with the word 'java' because that's your language of interest. Specifically you want to know something about Swing so that's the second keyword. And finally you need to find a simple way of describing what you want to do, which may take a few different approaches to get prudent examples and answers.

    This will lead you to plenty of existing forum posts where people had exactly the same question as you, with the answers. Imagine how easy you have it, when I started out I bought a huge 1000+ page book to learn Swing :)

    There is also the swing tutorial which is loaded with example programs:

    http://docs.oracle.com/javase/tutorial/uiswing/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Make a variable interface

    Quote Originally Posted by gimbal2 View Post
    Try a google for:

    java swing replace panel

    See how that works? Always start your search query with the word 'java' because that's your language of interest. Specifically you want to know something about Swing so that's the second keyword. And finally you need to find a simple way of describing what you want to do, which may take a few different approaches to get prudent examples and answers.

    This will lead you to plenty of existing forum posts where people had exactly the same question as you, with the answers. Imagine how easy you have it, when I started out I bought a huge 1000+ page book to learn Swing :)

    There is also the swing tutorial which is loaded with example programs:

    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    Yeah, but you had a contents page

    Thanks for your help :)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Make a variable interface

    So does the Oracle tutorials:
    Really Big Index.

    And, assuming this is a form of text based adventure, you have a single screen.
    That screen consists of a description area and several options (the buttons you mention).
    What populates those fields, and controls how many buttons to show, is decided by your model.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 04-16-2013, 09:56 AM
  2. Replies: 2
    Last Post: 01-09-2013, 08:10 PM
  3. Query abt interface variable
    By Gobi in forum New To Java
    Replies: 5
    Last Post: 06-25-2011, 04:27 PM
  4. Interface variable to class
    By zill in forum Advanced Java
    Replies: 6
    Last Post: 10-11-2008, 03:29 AM
  5. make a variable name from a string?
    By Kinnikinnick in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:54 PM

Posting Permissions

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