• Post Reply Bookmark Topic Watch Topic
  • New Topic

retrieving name of container  RSS feed

 
Al Swearengen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone. as the saying goes, i'm new so please have patience with my ignorance.
i'm having a problem with a relatively simple idea. i'm creating a 10 question multiple choice quiz where a question is a class that extends JPanel and has (with the parameters in my constructor) it's own button group for answers and its own JLabel for the question.

the problem i'm having is that the button to go to the next question is also part of the question class. and for me to use Card Layout (the only way i've been able to find that would work) my generic class has to know the name of the container it's stored in when calling the next method eg: void next(Container parent). is there a method i haven't found that will allow me to retrieve the name of the container so i can just leave the button in the Question class? does this question even make sense?

here's what i have so far.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Al,
Welcome to the Ranch

You need to think OO.
1) Have a Question class which will hold the question and its answers. Think of it as a data bag.
2) Prepare and maintain a list of all your Question objects.
3) Subclass JPanel and pass the Question object to its constructor. Based on the question and answers build the UI. Obviously this will be a generic panel for all questions.
4) When the user clicks previous/next, query the question list (#2) to get the previous/next question.
5) Create your panel (#3) at runtime by passing the retrieved question and display it.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> the problem i'm having is that the button to go to the next question is also part of the question class.

it shouldn't be part of the question class.

you should have your cardlayout panel at the frame's BorderLayout.CENTER
and your next/previous buttons on a JPanel at the frame's BorderLayout.SOUTH

also, if your questions all have the same number of multiple choices e.g. 4,
you don't really need the cardlayout, setting the text of the question, choices,
and any selection (for previous button) is all you need to do
 
Al Swearengen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Hi Al,
Welcome to the Ranch

You need to think OO.
1) Have a Question class which will hold the question and its answers. Think of it as a data bag.
2) Prepare and maintain a list of all your Question objects.
3) Subclass JPanel and pass the Question object to its constructor. Based on the question and answers build the UI. Obviously this will be a generic panel for all questions.
4) When the user clicks previous/next, query the question list (#2) to get the previous/next question.
5) Create your panel (#3) at runtime by passing the retrieved question and display it.


thanks for the reply, Maneesh. i'm not entirely sure what you mean by think OO. my answers are contained within the question in the String key. i'm not sure what you mean by number 2, are you talking about using a collection? i'm not sure what you mean.

Michael, i had thought about moving the button to a different panel but then i couldn't think of any way that i wouldn't be looking for the name of the instantiated panels, anyway. different location but i'd still be going through the same process to retrieve the container i'm calling the next() method on. i'm not sure i'm understanding or explaining that correctly but you know what i mean, right?

and while i can simply change the text of the question and answers to continue rather than using cardLayout for multiple panels i tried it this way so that later on, when i do have to use a button to swap panels in cardLayout, i'd already know how to do it. plus i couldn't get repaint() to work after setting the query text to something differently.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Michael, i had thought about moving the button to a different panel but then i couldn't think of any way
> that i wouldn't be looking for the name of the instantiated panels,

you don't need to know the name of the panel.
all you need is a reference to the CardLayout, and it is just
cl.next() (or cl.previous())

the only problem doing it this way is when you get to the last or first panels,
where you may want to disable the next or previous button
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a simple demo of cl.next/previous



and if you wanted to stop the cards from 'wrapping' i.e. going from last to first (next)
or from first to last (previous), you can add something like in this modifed code

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Al Swearengen wrote: i'm not entirely sure what you mean by think OO. my answers are contained within the question in the String key. i'm not sure what you mean by number 2, are you talking about using a collection? i'm not sure what you mean.


I meant think from the Object perspective. Having a question object which holds all the relevant data (only) is immensely helpful. Your approach was to mix the data with the UI. There is a strong reason why people advocate MVC (Model View Controller)

Al Swearengen wrote: i'm not sure what you mean by number 2, are you talking about using a collection? i'm not sure what you mean.

Yes I mean a collection of all your questions. It would be a central place to query for the previous/next questions. In real life it would be some database kind of stuff where your data would reside.



 
Al Swearengen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys, that worked. thanks a a lot for the help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!