This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
I am very new to swing and java awt. I am trying an application where I have 2 panels in main container. First panel (p1)contains some data and a button , upon click of button, Second panel (p2) has to draw a graph based on the content of first panel.
To draw the graph I am creating another class called secondPanel and passing p2 in the constructor, to get the graphics object of p2 to draw the graph.
The problem I am getting is when I change the data and redo the graph it retains the old graph. As I understand it is because of the graphics object that I am getting from p2.
before passing object p2 to the constructor of SecondPanel, I am calling removeAll() and validate() on p2, however my graphics object is not recreated.
Really appreciate if someone can help me here. Here is the code snippet of SecondPanel.
In the main container here is my code of reloading p2
How can I reload the graphics. while using super.paintComponent(g), I am getting null pointer exception and I don't know whether graphics object obtained by super will fit my requirement.
Thanks in advance!!!.
When SecondPanel is created, you add the ref from a foo JPanel
Afterwards you add the SecondPanel to foo
A bit complicated, don't you think? ;)
You should use a CardLayout (p1 vs. p2).
Anyhow ... usually an invalidate() and repaint() on the component should do the trick.
Joined: Feb 21, 2008
Yeah it was a bit complicated . So I changed my approach instead of using another class, I am using an inner class that extends JPanel and it works. The code posted above was not working even with invalidate and repaint may be because I was not calling super.paintComponent(g); .I think this needs to be called for repainting graphics object.