Results 1 to 3 of 3
  1. #1
    tuwannu is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default struggling with creating a GUI

    I'm not quite sure where to put this topic in, I hope this is the right category.

    I'm creating a simple music player with a JFrame and two JPanels

    I put the three components into three classes:

    a PlayerFrame extends JFrame,
    a SongInfoPanel extends JPanel,
    and a ControllerPanel extens JPanel.

    The SongInfoPanel contains labels that show information about the song, and a label that indicates whether the music is playing or paused.
    The ControllerPanel contains buttons used to pause and play the music.

    And I have another Main class that does not extends anything, it creates a PlayerFrame, a SongInfoPanel, and a ControllerPanel separately. The Main class then adds the two panels into the PlayerFrame.

    Now here's my confusion. I press the play/pause button but also want also the label in SongInfoPanel to be updated... These two objects don't know that each other exists, how could the SongInfoPanel be updated when a button in ControllerPanel is pressed?

    I've thought of several ways to do it, but i'm not sure which one would be appropriate, or none at all (which i think is very likely so)...

    1. When creating a ControllerPanel, pass a reference to SongInfoPanel as an argument so the ControllerPanel can update the SongInfoPanel directly. I have a feeling that this is the worst-in-the-world solution.

    2. Make the two panels inner classes of PlayerFrame. Again, so that ControllerPanel can access a variable that points directly to SongInfoPanel. But I would end up having only one big outer class, this would be a bad bad design wouldn't it?

    or 3. When a button in ControllerPanel is pressed, the button's actionlistener calls getParent().getParent()... so on until it reaches PlayerFrame. And create a method in the PlayerFrame that updates the SongInfoPanel. This would also mean that I need a method to alter play/pause label. Later on I would need the player to change the song, I would then need another method to update the labels for song info, and so on?

    or what should be the way to handle this problem? I'm completely lost.

    Sorry for the confusion I may have created. And thanks in advance for any help.

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

    Default

    One way is to connect the songinfo and controller with a Control class (a la MVC). Using interfaces to demonstrate

    SongInfoPanel.java

    Java Code:
    public interface SongInfoable
    {
      // the implementation code will change the song
      //status and update the label
      public void play(); 
      public void pause();
      public void stop();
    }
    Java Code:
    public interface Controllerable
    {
      public void addButtonListener(ActionListener al);
    }

    The Control class can have access to the ControllerPanel and the SongInfoPanel, it can create the ActionListener object and place it in the ControllerPanel. The ActionListener will then translate the button presses into SongInfo actions.

  3. #3
    tuwannu is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks, Fubarable! Your reply was very helpful. I guess I'll need to have a look more at design patterns..

Similar Threads

  1. Creating a toolbar in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:08 PM
  2. Creating .Jar files
    By Deathmonger in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 12:59 AM
  3. creating jar file
    By po0oker in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2008, 08:49 PM
  4. regarding to creating table.....
    By daredavil82 in forum New To Java
    Replies: 0
    Last Post: 11-18-2007, 05:55 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
  •