Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,331 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,804 people online right now. Registration is fast and FREE... Join Now!




Java application design problem

 
Reply to this topicStart new topic

Java application design problem, How do I avoid getting tangled with my JFrame?

thenovices
3 Sep, 2008 - 04:34 PM
Post #1

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
Okay, so I've been working on a vocabulary flashcard-ish application and I came across a problem. I've been able to deal with it, but I believe that my solution may be messy and not very good design practice.

So my JFrame is called VocabApp, with a JPanel called VocabPanel, which holds all the GUI components. VocabApp has a JMenuBar, VocabMenu. VocabPanel has an instance of VocabGame, which deals with all the game logic and stuff and also holds an instance of VocabList, which deals with all the vocabulary words and their definitions.

Whew, that was a lot, I hope that made sense. Rephrased: VocabApp has a VocabMenu and a VocabPanel. VocabPanel has a VocabGame which has a VocabList.

So when I want to open a file, I open a JFileChoose from the VocabMenu. Once VocabMenu gets the File, it has to pass it all the way to VocabList, which will process the data accordingly. However, currently I'm doing something like this to solve the problem:

CODE

VocabApp top; //the top level JFrame
File f;
top.getPanel().getGame().getList().loadList(f);


Sure, it works, but I feel weird having to call top.getPanel() and panel.getList(). It just seems wrong, because the game logic stuff should just be like left alone.

So any suggestions on how to solve this weird design issue?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Application Design Problem
3 Sep, 2008 - 06:29 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Don't really know what is a VocabList but I would do it the other way around

Pass the JMenuBar down the way and add the ActionListener to the level which needs it

CODE

class VocabApp extends JFrame {
    VocabApp() {
         super("Title");
         JMenuBar VocabMenu = new JMenuBar();
         ....
         ....
        add(new VocabPanel(VocabMenu);
     }

     class VocabPanel extends JPanel {
           VocabGame vg;

           VocabPanel(JMenuBar menuBar) {
                super(.....);
                ......
                ......
                vg = new VocabGame(menuBar);
           }
     }
}

class VocabGame implements ActionListener {

     VocabGame(JMenuBar menuBar) {
             // extract the element of the JMenuBar and add
             jMenuItem.addActionListener(this);
    
     }

}

User is offlineProfile CardPM
+Quote Post

thenovices
RE: Java Application Design Problem
3 Sep, 2008 - 08:47 PM
Post #3

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
ah I see,
I guess that works, I forgot about using an ActionListener. Thanks!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:54AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month