my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Label Not Appearing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Label Not Appearing" Watch "Label Not Appearing" New topic
Author

Label Not Appearing

Gordon Mankelow
Greenhorn

Joined: Aug 02, 2013
Posts: 6
Hi,

I am very new to Java and trying something very simple, I have a form that is built using GridBagLayout so that I can have 6 buttons on the left, 6 on the right and a workspace panel in the middle.

When you click on one of the buttons the panel in the middle changes. The Panel is to show a mix of simple line graphics, labels, fields and buttons. I have the code working to show the simple graphic but a textfield (label or buttons) will not show.


This is the code from the Action listener of the button, which is sitting in the main frame, workPanel is the central Panel.


and this is the code for the Triangle Panel which is added into the Workspace Panel




I have read a number of posts that have the same issues and I have tried to work through them with my code but none seem to work. Also the SetSize and Color in the Triangle panel does not work unless I change them in the Listener.

Any help would be greatly appreciated.

Thanks

Gordon
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Welcome to the Ranch.
Try revalidate() and repaint() (in that order)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Gordon Mankelow
Greenhorn

Joined: Aug 02, 2013
Posts: 6
Maneesh,

Thanks for the warm welcome.

I added

workPanel.ReValidate and workPanel.Repaint to the ActionListener code but with the revalidate workPanel goes blank. Remove that and it works as before, the Graphics are shown but not the label. Adding the Revalidate and Repaint to the Triangle gives the same outcome.


Gordon
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 320
    
    2
I'm not sure what exactly causes the problem. I never use JPanels that contain components AND are used to do some dedicated drawings.
I don't know when the components get redrawn and when the paintComponent is invoked, clearing all the contents. Maybe that's the problem here.

My suggestion is: use grid position 0,1 (i.e. the middle of the top row) for the label, and grind 1,1 as your workpanel.
Also, you put "setSize(500, 500) in the triangle class. Don't do that, since it is the job of the GridbagLayout to determine the size of the grids.
So, use "setPreferredSize()" instead.

And, if your shapes have different sized JPanels, then what Maneesh suggests is exactly what I should do too. If that doesnt work, you could try to
revalidate and repaint the parent container (the container with the GridbagLayout).

Greetz,
Piet
Gordon Mankelow
Greenhorn

Joined: Aug 02, 2013
Posts: 6
Still struggling with this, I have done as Piet suggested with a 2nd Panel to show the components and keep the graphics on another Panel, but even this does not work. The ReValidate() command seems to (and from what I have read) updates the heirachy of components so I either end up with graphics on one panel and nothing on the other or a label on one and no graphics.

This is my listener code now



but if any one can help this is my full code.



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

Gordon Mankelow wrote:Still struggling with this, I have done as Piet suggested with a 2nd Panel to show the components and keep the graphics on another Panel, but even this does not work.


You may have done that -- I didn't go through all of that GUI setup code to see whether you did or not. But I did notice that in your action listener you had some code which mucked about with the GUI, trying to add and remove things. My advice: Don't do that. Just put the components in one panel and the graphics in another panel (as Piet already suggested) and leave them there. If you want to update the graphics, call revalidate() on the panel containing the graphics.
Gordon Mankelow
Greenhorn

Joined: Aug 02, 2013
Posts: 6
Thanks

But I am trying to alter what's in the panels. I.e draw a triangle or a square depending on which button is pressed and show or hide buttons etc in the other. How would I do this if I dont alter the GUI in the action listeners.

Gordon

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

You don't "alter" any panels like that. If you want to change the contents of a panel which you drew using the Graphics methods, then you just call its revalidate() method. This will eventually call the panel's paintComponent() method -- e.g. the one you posted in your original post. At this point your code in paintComponent() should display whatever it is you want to see on the screen. That's all. No adding or removing of components, just redrawing when necessary.
Gordon Mankelow
Greenhorn

Joined: Aug 02, 2013
Posts: 6
I had thought about that but I couldn't work out how to have a condition in the paintcomponent so that it showed a different drawing depending on what button had been clicked.

Sorry if I am being a bit thick, but any examples would be greatly appreciated.

Gordon


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

You simply have the button store the appropriate data in the model, and then the graphical component would get the data from there. How you design the model is up to you.

But I'm guessing you might not understand the word "model". If that's the case then have a look at the Wikipedia article Model–view–controller. Your GUI is the "view" in this case, and the "model" is where you're going to store the data for your application.
Gordon Mankelow
Greenhorn

Joined: Aug 02, 2013
Posts: 6
Paul,

Many thanks, doh! MVC, yes understand that and now have it working.

Many thanks

Gordon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Label Not Appearing
 
Similar Threads
help with prints
JLabel.setText() is NOT Updating the Text
Getting wrong answers for conversion
Paint method is covering other layouts
Java printing problem