Results 1 to 4 of 4
  1. #1
    Neurax is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Updating data members in panels

    Hello there,

    For a class project, I am building a simple video game. I have the engine of the game designed already and am working on implementing graphics into the game. My idea was to use CardLayout such that the title screen, game setup screen, and gameplay screen would be on separate panels and the card would be shown at the appropriate time.

    I can post snippets if necessary, the code is too large and cumbersome to post for now.

    I seem to be running into the following issue:

    In the class's constructor, I am building each JPanel... i.e.

    JPanel titleScreen = buildTitleScreen();
    JPanel charScreen = buildSelectionScreen();

    where those two methods just procedurally create panels and return them to the function call, all using private data members declared outside of the class.

    When I fill in JTextFields on one JPanel and then press a button to show the next panel, the next panel, which relies on info in from previous panel, doesn't contain the updated information because when it was built, the fields were empty.

    Currently, my workaround is to remove the card from the stack, rebuild the panel, and re-add it in, but I was wondering if there was a function I could call on the JFrame to rebuild the container with updated information.

    I've looked into the validate and revalidate methods, but couldn't figure out if they were what I needed and how to use them.

    Any suggestions? All help is much appreciated!

    Neurax

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

    Default Re: Updating data members in panels

    The info displayed in the JTextFields should be part of the model. When the model's state is changed, say by changing the text in a JTextField and somehow submitting it (perhaps by pressing a button), the model should notify listeners of the change, including the views, so they can update their display with the new information.

  3. #3
    Neurax is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Updating data members in panels

    I'm not so sure what you mean by model.. I once watched a lecture series on ObjectiveC that described an MVC design (Model, View, Controller) but I never really understood the concept.

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

    Default Re: Updating data members in panels

    Yes, I mean MVC, absolutely. The model is the logic behind the program, both its brains and its state. The GUI or view should be as dumb as can be and delegate as much logic as possible to the model. The information held in the JTextFields will get submitted to the program when the user presses the JButton telling the program to accept the data entered and change CardLayout views. That information should get held by a non-GUI class that is the model, and any portion of the view that needs that information for display needs to be notified of the change.

Similar Threads

  1. JFrame frozen while updating data from streams?
    By SmugMug in forum New To Java
    Replies: 2
    Last Post: 05-31-2012, 06:57 AM
  2. Replies: 3
    Last Post: 02-11-2012, 03:32 AM
  3. Accessing abstract class subclass's data members
    By Claymz in forum New To Java
    Replies: 23
    Last Post: 04-18-2011, 12:26 PM
  4. Sharing Data Members between two JFrames
    By PrinceSendai in forum AWT / Swing
    Replies: 3
    Last Post: 10-17-2010, 03:51 AM
  5. private data members?
    By blueduiker in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 12:13 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
  •