This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
So, I have been trying to remove the JPanel i added when another button is clicked , so it removes the current on and adds the new one. Not sure i am doing it properly?
the entire code is below ,maybe someone can steer me in the right direction?
Maybe I am adding the new JPanel in the wrong place or improperly?
There's a lot of things there which are, let's say, confusing.
For example, at line 234 you create a MasseRechnen panel and then do nothing with it.
Likewise at line 225 you create a DichteRechnen panel and then do nothing with it. Both those lines could be thrown out -- unless creating the panels has some side effect, which wouldn't be a good idea.
And at lines 231 and 232 you revalidate and repaint... what, exactly? The code is inside a JPanel (declared at line 211), so you're revalidating and repainting that. Which is kind of pointless, because you don't have a displayed instance of MeinListener anyway. I can't understand why you have a listener which is a JPanel, especially one which is never actually used as a JPanel. You actually want to revalidate and repaint the main JFrame.
And you have one those awful listeners (which were popular in tutorials about ten years ago) which listens for a whole lot of different events and then has a whole series of if-statements which do completely different things based on which event they heard. It's better to have one listener for each different event. (Basic programming rule: an object should have one responsibility.) I would rewrite the code which assigns listeners to buttons so that it assigns a separate anonymous inner class (which implements ActionListener) for each button (or whatever) which will generate events.
You can probably tell I didn't read all of your code. There was a lot of it, that's why. You might consider stepping back for a bit. You're having a problem with adding new components? Then write a much smaller program which only does that and nothing else. Make that work, and then implement the working technique into your big program. That would have two advantages: less code to distract you, and less code for other people to wade through.
By the way I've never written a Swing program which tries to add or remove components. (And based on what I've seen on forums, I'm glad I haven't tried that.) What I do is to put all of the components into the GUI when I initialize it, and set them to be visible or invisible as required. No nasty revalidating or repainting necessary.
First off thanks for the good advice , i think my books are a little outdated and they show using listeners in this fashion that is why i used the same (it is the only way i have learned so far, and i really just started to learn about GUI's).
i will go through the problems you mentioned and try to explain.
line 234 actually creates the new panel and it works to add the new panel.
line 225 creates the new panelDichteRechnen() panel which also works, and i can write in the text fields and get the answer as well, although that formula isn't working correctly at the moment either but i am trying to get the panels right first.
line 231 and 232 i am trying to remove the panelDichteRechnen() panel that is there and then add the panel on line 234, but so far it just add the new panel on the right beside the other one i first created.
And yes i get your point about someone having to look through all the code, but i figured only the snippets wouldn't show exactly what i am trying to do.
the adding a JPanel seems to work fine, i just can't get rid of the one before adding the new one, it just continues to add new ones to the right. If noone else has an idea how to do this then i will have to consider learning a new way such as you wrote with setVisible(true or false);
So i have redone this GUI with the CardLayout, it is working as far as changing the panels for the correct choice, but the calculation done in the compute() method always returns zero? I thought i had
it set up correctly, but something must be incorrect! Any help would be appreciated.
here is the code:
I only have the first card "dichte" set up properly as this is an "work in progress" and i am trying to get the first one working properly, then i will fix the other 2 cards to match...
If dichte (lower case, DICHTE is a String) is the currently visible panel then its isVisible() method should return true. If not then it should return false. That's how CardLayout works, it makes all children except the current one invisible.
Thanks again, and the sample code is very nice;). I will see if i can incorperate that in my code!!
PS: The reason i had isShowing(); there is because i was trying allot of different things i found through Google before i had to
break down and ask here again
Since i started learning Java about 7 Months ago or so i have had a couple of things that just had me stumped , and this is one of those things!!
eventually i get it in the end most of the time;) i will let you know how it works out (or not)..
So i have been working on this again and i still don't have it figured out completely.
i have this to find the current Component (JPanel)
i have this to check which panel is showing
and with it set up like this to test it prints out
So i can see that one isVisible() is true and the other 2 false and i see the hash codes from all three BUT, I don't know what to put in if (currentPanel.equals(WHAT TO PUT HERE TO CHECK???)) only choices Eclipse shows
me is DICHTE? but using that doesn't work as i already knew.
been trying it over and over but can't get it worked out?
EDIT!!! OK i geuss i will need to Iterate? but not exactly sure how to do it?
Give each panel a name when you create them, using setName(DICHTE), setName(MASSE), etc.
Get the current panel using a method based on that method (you need to return the actual visible panel).
Make sure it isn't null (using an if-statement).
Call getName() on the returned panel.
Compare against your values.
The NPE means that currentPanel was null, but that makes sense: the currentPanel you return in your latest method is not the loop variable, that goes out of scope when the loop ends. It's the instance field, and that never gets set.
So i have been trying this further as i can't give up!!! I have a couple more questions though...
I have this now to figure out which card is visible
and this to check it
but i am still thinking this isn't the proper way completely as the output is showing dichte once, then masse twice, and volume three times?, but just having System.out.println(currentPanel.getName()); it shows the current
panel properly . Also if this is the correct way to do it, then how do then use the user input on the JTextField then? I have tried just setting the text when the panel is dichte, but the text first gets set when i click "Compute" on the volume panel??? Sorry i am still really confused .
Darryl Burke wrote:This typically happens when you add a Listener within the event code of the same or another Listener, but you haven't shown us the code that does that.
Also, getName() returns a String, so it is redundant to call toString().
ok here is the code that creates and adds the panels
It has the same listener for all three panels , i also tried it with each panel having it's own listener but that didn't change anything.Or maybe the setup in general isn't correct.I can post the entire thing if you want to see that.