• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cardlayout problem

 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow its a long time since i wrote a message here (or worked on my project at all) but i decided enough was enough i need to set aside some time to work on it. so i have done and i have run into a brick all after about an hour

my problem is to do with the show() class in the cardlayout. Im hoping i've cut my code down enough for you guys (it still shows the same error)



The code compiles propolly but during run time i get the following error when trying to use one of the buttons to switch to another card



the only thing that springs to mind when i read this is that it doesn't like having to refferrence another method from inside an anonymous inner class, but im a little out of my depth. any surrgestions are most welcome.

thanks in advance

sam
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
masterToolPanel() returns a JPanel, so each time you call it, you get another JPanel(CardLayout)

setting one initially should work

JPanel cardLayoutPanel = masterToolPanel();

and then refer to 'cardLayoutPanel'
 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, first thankyou for your quick reply

I think i get why that makes sence so thats a step in the right direction :P but im not sure how who want me to impliment that change! i tried like this


and your quite right i don't get an error now, but im still not getting the card to switch so if i've missinterpritted what you were saying can you please correct me.

i've also tried defining it inside the inner class - again no error but the card doesn't switch.


thanks in advance

sam
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but you still have another masterToolPanel() here:
masterPanel.add(masterToolPanel(), BorderLayout.CENTER);

this is the one seen on the screen, not the 'cardLayoutPanel' you've created, and accessed via the actionListeners

move cardLayoutPanel to a class field, then
masterPanel.add(cardLayoutPanel, BorderLayout.CENTER);
and it works OK

also, all those 'statics' will bite you later on
 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks for the reply
Michael Dunn wrote:move cardLayoutPanel to a class field

could you explain what you mean by a class field because im a little lost.
also, all those 'statics' will bite you later on

the only statics i have are (according to eclipse) needed so im a little there!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's your code modified to include the class field, and the statics removed
the statics were needed because of your code structure, now changed

run the program, click the second button and the text message changes
click the first button, it changes back - hopefully this is what you are trying to do.

 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats brilliant it works perfectly

as an aside why is using "static" in a method not avisable?

thanks again

sam
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> as an aside why is using "static" in a method not avisable?

specific usage of static is good
Math.random()
System.out...()
etc

but overuse can lead to problems.
I've seen numerous posts of "is this a Swing bug" turn out to be
just a changed program (now to be reused) where a static field was the culprit

simple demo, QuizPanel.
run as is, see all the radioButtons/labels when you click 'next'
now change the comments around so they're static, rerun and see

and if someone was using this, the forum post would be "why don't my radioButtons appear"

 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou very much this has cleared quite a few misconseption i had and hopfully i'll avoid being one of the people who thinks he has found a bug

cheers
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!