I am having a difficult time using a for loop to find the current Component in my GUI using CardLayout. I am putting this in the beginning Java because it is more a poroblem of understanding
how to use the for loop, or Alorithms in general<-- is this considered an algorithm, or logic?
here is a small code snippet
I am getting a NullPointerException all the time.This type of operation seems to be my biggest problem at the moment.Are there any good books or online tutorials i can refer to?
Thanks for your reply, I actually think i have it working(to a certain point!) here is what i have done
when changePanel1 is showing, it also prints changePanel1 correctly and when the other 2 are showing then it doesn't print anything which is correct.
however i have some JTextFields and JButtons on each panel with the same names in each panel
if i use lbl4.setText(txt1.getText()) instead of the System.out.println(getCurrentPanel().getName()); the text gets set on the third panel? and not always right away?
First question is : does the the method getCurrentPanel() look correct to you?
second question is: why would the text then get set on a different panel? or can i not have JTextFields and JButtons with the same names on different panels , even if i specify which panel is showing?
I hope this is understandable ,if not i will post my current code completely. If everything looks OK then maybe this should be switched to the Swing Forum then.
Why do you have two nested loops, both over cardPanel.getComponents()? You should remove one of them. I would choose the inner one. Also, you call getComponents() for each iteration of the inner loop. You should cache its results, which makes getComponent(i) obsolete as you can replace it with direct array access. So one of the following should be good: