| Author |
creating a new JPanel from another JPanel "choice" button
|
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi,
I am have a JPanel with 3 JButtons for options to pick, and also a two other buttons to either quit or to do an equasion.
this is the 3 buttons as choices
and i would like to create a new JPanel when one of the buttons is clicked.
the JPanel for the first JButton that can be clicked is below.
and i am trying to get it to start with this
I am just learning GUI's so i am sure there are some mistakes here but i am just trying what i think should work ;) Right now i don't get an error Message but no new JPanel comes up either.
Thanks
Mike
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Whenever you modify the GUI hierarchy tree while the GUI is already shown you need to revalidate() and repaint() the container you added a component to or removed a component from.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
|
Thanks Rob! I will give that a try in a few and see what happens!!
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi again,
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?
Thanks
Mike
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi Paul,
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);
Thank you for your time!!
Mike
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
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.
Ever heard of CardLayout?
|
luck, db
There are no new questions, but there may be new answers.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Darryl Burke wrote:
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.
Ever heard of CardLayout?
I have now ;) i will check it out!
Thanks
Mike
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi again,
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...
Thanks Mike
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
ok i have got it working but in a strange way , maybe i am missing something or not thinking properly
I commented on the current poroblem/ strangeness!! why is that so?
thanks
Mike
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
ok i have been trying this for a long time, and it seems as though i am not finding the current card. and getting an empty string .
can someone tell me how to figure out which card is the current one showing?
Thanks
Mike
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Go through the panel's child components and check the visible flag.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi Rob,
Thanks for the quick answer, but i am not sure how to do that? could you elaborate a little more please?
I thought to try if(DICHTE.isVisible()) but that doesn't work.
thanks
Mike
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Thanks again Rob!! , I am still new at this so will try to grasp what you wrote here and use it properly.
Mike
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi Rob,
Still haven't figured it out.... I am not sure exactly what you mean?
i tried also something like this
but still doesn't seem to do the trick? still getting a null pointer exception.
Thanks
Mike
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Why are you using isShowing() when I suggested isVisible()?
Check out the following little example:
However, isShowing() should also work, at least if the frame is visible at the time.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi Rob!
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)..
Thanks for your help
Mike
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi,
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?
Mike
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
|
bump..
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Thanks Rob,
This is drivin me crazy lol. I actually had that , and had the names being rpinted in the getCurrentPanel() method
if the card showing was "DICHTE" then it printed
Dichte
true
false
false
if the next card "MASSE" was showing it printed
false
Masse
true
false
but every time i tried to use
if(currentPanel.equals(DICHTE )) i always got a null pointer exception? i thought i finally had it worked out but not quite !!
thanks for your time Rob this just has me stumped good
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Thanks for the explaination, first 8 hours at work, then i will get back on it!!
Mike
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
|
ok i have tried this too long lol it is slowly drivin me insane. Can't figure it out !?!
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi again;)
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 .
Thanks in advance for any help
Mike
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
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().
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
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().
Thanks Darryl,
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.
Thanks again
Mike
|
 |
 |
|
|
subject: creating a new JPanel from another JPanel "choice" button
|
|
|