Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI nightmare

 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are my 3 classes so far:
BarTill:

ChoicePanel:

SubChoicesPanel:

The first 2 classes are ok (for now). The 3rd class is giving me a lot of trouble. Being honest I can handle layout managers with simple projects, but I am getting into all sorts of problems with this.
The SubChoicesPanel class is supposed to create a GridLayout panel 4 columns wide and the rows are dependant on how many JLabels exist in the array subChoices. So if the user selected Draught Beer in the program the 1st row in the grid would be a JLabel Bitter �2.04 per pint, then a JButton +, then a JButton -, then a JLabel 0. This sequence of components would repeat filling the grid.
When I run the program the code creates a GridLayout that is 2 columns wide even though I constructed a GridLayout 4 columns wide.
Because I'm populating the various components & adding then to my panel using loops this makes it hard for me to spot where I am going wrong.
Please help?
Many thanks, Dave.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is because the GridLayout puts most of it's effort into meeting the first constraint specified in it. The number of rows that you are specifying by using "subChoices[num].length" is *not* the number of rows that you actually want... it is double the number of rows, because you are storing both the name and the cost as strings in this array.

You can do either one of these things to get the GridLayout to work... specify "subChoices[num].length / 2" as the rows, or just put "0" as the row value. A "0" value as the rows parameter lets the GridLayout know that only thing it should care about is the columns, never mind how many rows are passed to it.
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why it was that int j=0; was in the loop. I moved it to the block before and it comes as no suprise the all the JButtons have listeners. Thanks Nathen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic