• Post Reply Bookmark Topic Watch Topic
  • New Topic

int value not correct  RSS feed

 
Mark Dittrich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right - this is my first ever application. Believe it or not, I've come quite a way to get it to work how it currently works (having discovered card layout along the way). I am sure that the code given is not optimal, and I'm going to work on that in future iterations, but for now I'm just trying to get my Maths Quiz to function properly. I am almost there, however I have hit a small snag in that the integer i am using to keep track of the score is not calculating correctly, and I cant work out why, despite being sure that this must be an absolute beginning/noobie mistake. I essentially have 3 questions, which have 3 button groups of 4 buttons each associated with them.

I have 2 problems:
1) If I try to add a label or a button to every card after the radiobuttons, they dont display when i run the program - why?
2) Key problem: I have setup an ActionListener to check for when the correct answers are selected, and when they are, to increment i (intially 0) by 1. I've coded this currently for the first question, but somehow instead of being 1, the value is 2! And the label displays this on the final card. I dont get it!

Any help would be appreciated - code is below:



Thank you
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Dittrich wrote:2) Key problem: I have setup an ActionListener to check for when the correct answers are selected, and when they are, to increment i (intially 0) by 1. I've coded this currently for the first question, but somehow instead of being 1, the value is 2! And the label displays this on the final card. I dont get it!

Well I'm afraid I'm no Swing expert, but I'm having a hard time visualising
(a) what this is supposed to look like.
(b) how it's supposed to work.
It seems to me that the only time your counter will be incremented will be when the "Next" button is hit, and that only if ANY of the correct answers is selected.

It also seems to me that because you've made your entire class (which contains all these buttons) an ActionListener, you might end up executing the actionPerformed() method twice every time you click "Next"; but I simply don't know enough about Swing to know whether this is your problem or not.

I can, however, give you a couple of general suggestions:
1. Separate your "question" logic from your display. COMPLETELY. Right now, they are so tightly coupled that it's hard to know where your question logic ends and your display logic begins.
2. Use your questions data and structure (ie, question→list of answers→correct answer) to create your buttons (possibly as arrays) and labels. It'll save you a lot of brittle "named" buttons that will have to be changed if you ever want to change your questions/answers.

Winston
 
Mark Dittrich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for responding Winston.

It looks pretty straight forward. When you launch the app, it displays a label with the first question, and the underneath it are the 4 options to choose from. These are all the items I can seem to get to display one the first and second cards (panels) in the layout. If you click the correct answer, your "score" should be incremented once you click the "Next" button. The "Next" button then also loads the next question and repeats the operation until you get to the final question.

I had wanted the button / buttons to be on every card as well, but for some reason these never displayed after the JRadioButtons. To get around that I moved the button out of the cards and onto the frame directly. For some reason the 3rd card does seem to display the extra button label etc.

I cant seem to understand WHY the listener would execute twice, or why on my cards i dont seem to be able to display objects after the radio buttons. I know that this code needs optimising (i'm just not sure how right now) and was hoping to just get it to work, and improve it from there. Once i get the score to show up correctly, this version would be "done".

I have thought about specifically putting the action listeners on the radio buttons themselves, which i will try tonight sometime. In terms of using arrays I tried and failed hence trying to simplify the logic with a mountain of code.

EDIT: In thinking about this as I've written the above, i think I know why it might be showing 2 instead of 1 on the final card (the only place the label displays) and that would be because i click the next button twice to get to the 3rd screen, so even though card 1 is not the current/active card, i must still be counting it is my guess. I still dont know why i have the display issues though in getting extra labels or buttons onto cards 1 and 2....
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, it doesn't matter whether a checkbox is displayed or not, it is still selected. I think instead of incrementing a counter, something like this should do the trick:

i = (Q1A4.isSelected() ? 1 : 0) + (Q2A3.isSelected() ? 1 : 0) + (Q3A3.isSelected() ? 1 : 0)

I'm not a Swing expert, either, but I think in order to show newly added GUI elements you need to invalidate the layout, so that Swing will lay out all the components from scratch.

I think it would be more intuitive for the user if the result was displayed on its own card, instead of being part of the last question card.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Dittrich wrote:I have thought about specifically putting the action listeners on the radio buttons themselves, which i will try tonight sometime.

Well that would certainly be a start. Like I say, I don't know what the effects are of making your entire class an ActionListener, but it doesn't seem right to me.

Suggestion: Your "increment count" function sounds like an Action to me, not an ActionListener; and since it's the only one you need right now, why not just define an UpdateCountAction class and supply it to the constructor of whatever button you want to use it on?

I'm quite sure if I'm wrong, I'll be flamed by the experts here ; but that method definitely sounds to me like an "action"; not a "listener". Obviously, you'll still need to put the code in the same named method (except it'll be part of your Action class), but then you can have the action performed any time you want.

Winston
 
Mark Dittrich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Indeed, it doesn't matter whether a checkbox is displayed or not, it is still selected. I think instead of incrementing a counter, something like this should do the trick:

i = (Q1A4.isSelected() ? 1 : 0) + (Q2A3.isSelected() ? 1 : 0) + (Q3A3.isSelected() ? 1 : 0)

I'm not a Swing expert, either, but I think in order to show newly added GUI elements you need to invalidate the layout, so that Swing will lay out all the components from scratch.

I think it would be more intuitive for the user if the result was displayed on its own card, instead of being part of the last question card.


Thank you. So I think the mystery of "2" is solved now. I will try out your code tonight as well - I have never seen anything like it, so I'd be happy to learn a bit more assuming it works. I agree with your thinking that you get the score afterwards. During the initial creation though i wanted to check it step-by-step which is how i got the 2 issue. If this works i'll just create a summary screen at the end.

So the only question that still remains is, why do i have display issues on the cards when i try to add a label or button after the RadioButtons? I will have to play around.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I don't know what the effects are of making your entire class an ActionListener, but it doesn't seem right to me.

While not the most elegant way to handle this, it's not uncommon to do it like this. And it's really just a single method -actionPerformed- that implements that interface, the rest of the class is not involved.
 
Mark Dittrich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mark Dittrich wrote:I have thought about specifically putting the action listeners on the radio buttons themselves, which i will try tonight sometime.

Well that would certainly be a start. Like I say, I don't know what the effects are of making your entire class an ActionListener, but it doesn't seem right to me.

Suggestion: Your "increment count" function sounds like an Action to me, not an ActionListener; and since it's the only one you need right now, why not just define an UpdateCountAction class and supply it to the constructor of whatever button you want to use it on?

I'm quite sure if I'm wrong, I'll be flamed by the experts here ; but that method definitely sounds to me like an "action"; not a "listener". Obviously, you'll still need to put the code in the same named method (except it'll be part of your Action class), but then you can have the action performed any time you want.

Winston


working with multiple classes is something I'm not yet used to - i think i think in incredible basic programming terms at the moment - i'm pretty fresh to it all. I will definitely look into how i can incorporate something like this into my solution though and hopefully make it re-usable when i want to setup different quiz apps not related to multiplication!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Dittrich wrote:working with multiple classes is something I'm not yet used to - i think i think in incredible basic programming terms at the moment - i'm pretty fresh to it all. I will definitely look into how i can incorporate something like this into my solution though and hopefully make it re-usable when i want to setup different quiz apps not related to multiplication!

OK, well here's my idea; and like I say, it may not be the best, but maybe you can see where my thinking is going:Probably not much different from what you already have, but it seems more intuitive to me because you've actually named your action. And why JButton doesn't have some more constructors (or a Builder) to cover situations like this, I have no idea.

Just one of the reasons I hate Swing.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!