Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: JPanel does not show the changes

  1. #1
    JAPA1972 is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default JPanel does not show the changes

    I have a panel (MyPanel) inside a frame. MyPanel just draws an ellipse. If I add a new instance of the MyPanel to my frame, the ellipse is successfully drawn.

    The below two lines draw the ellipse.

    fr.add(new MyPanel());
    fr.setVisible(true);

    However, if I do the following, the ellipse is not drawn:

    JPanel pn = new JPanel();
    fr.add(pn);

    pn = new MyPanel();

    fr.getComponent(0).repaint();
    fr.getComponent(0).validate();
    fr.repaint();
    fr.validate();

    fr.setVisible(true);


    What am I missing here? Why doesn’t the second approach work? Let me emphasize that I can’t use the first approach for reasons long to explain (this is a small sample of a larger project).

    Thanks in advance.



    The whole code is as follows:

    ********************************

    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.Ellipse2D;

    public class ShapeTest {
    private JFrame fr;


    ShapeTest(){
    fr = new JFrame();
    fr.setSize(200, 200);
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    // the below two lines –commented out- show the circle
    //fr.add(new MyPanel());
    //fr.setVisible(true);

    //the below lines won't show the circle
    JPanel pn = new JPanel();
    fr.add(pn);

    pn = new MyPanel();

    fr.getComponent(0).repaint();
    fr.getComponent(0).validate();
    fr.repaint();
    fr.validate();

    fr.setVisible(true);
    }

    public static void main(String[] args) {
    ShapeTest st = new ShapeTest();
    }
    }

    class MyPanel extends JPanel{


    @Override
    public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D)g;

    Ellipse2D ellipse = new Ellipse2D.Double(50,50,100,100);
    g2.draw(ellipse);
    }


    }

    ************************

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: JPanel does not show the changes

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: JPanel does not show the changes

    As you've already discovered, setting a variable to a new reference has no effect on its previous value. Why do you think it should have? This code
    Java Code:
    JPanel pn = new JPanel();
     fr.add(pn);
    
     pn = new MyPanel();
    doesn't anywhere attempt to add the new MyPanel to the JFrame.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JPanel does not show the changes

    Quote Originally Posted by JAPA1972 View Post
    I have a panel (MyPanel) inside a frame. MyPanel just draws an ellipse. If I add a new instance of the MyPanel to my frame, the ellipse is successfully drawn.

    The below two lines draw the ellipse.

    Java Code:
    fr.add(new MyPanel());
    fr.setVisible(true);
    However, if I do the following, the ellipse is not drawn:

    Java Code:
    JPanel pn = new JPanel();
    fr.add(pn);
    
    pn = new MyPanel();
    
    fr.getComponent(0).repaint();
    fr.getComponent(0).validate();
    fr.repaint();
    fr.validate();
    
    fr.setVisible(true);
    What am I missing here? Why doesn’t the second approach work? Let me emphasize that I can’t use the first approach for reasons long to explain (this is a small sample of a larger project).
    An essential thing to learn is the difference between a variable and an object. In your second example you create a JPanel variable, pn, and have it reference a standard JPanel, and then you place that JPanel into a JFrame. You then change the variable that pn is referring to, and seem to expect the component that the JFrame is holding to magically change to the new object, and that's not how it works.

    Perhaps it would be best to visualize this. Image the variable pn is an arrow, and that initially this arrow is pointing to a JPanel object. You then tell your program to place the object that pn is pointing to into the JFrame. If later you point the arrow at something new, it's not going to change what the JFrame holds. To do that, you need to remove the JPanel from the JFrame and place the new component in, but you would need to do this working with the JFrame object, not the pn variable.

    Also please note that you should not try to access components via getComponent(...) as that is very fragile. Doing this will likely get you a reference to the JFrame's JRootPane, not the JPanel object that you've added.

    Finally, when posting code here, please wrap your code in [code] [/code] tags so that it retains its formatting and is easier to read.

    Best of luck and welcome to the java-forums.org!

    Edit: and I am way too slow! Hello Darryl!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: JPanel does not show the changes

    Edit: and I am way too slow! Hello Darryl!
    But solidly explained. And that was my last post before bedtime. Mornin' Fu (evening for you).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Last JLabel I add to JPanel doesnt show up
    By Ambergahill in forum New To Java
    Replies: 6
    Last Post: 01-29-2014, 08:07 PM
  2. how to show jpanel from external jar?
    By skadron_alfa in forum New To Java
    Replies: 5
    Last Post: 01-15-2012, 06:27 AM
  3. JPanel colors don't show
    By mando in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2011, 03:52 AM
  4. JPanel: Show Tooltip on click
    By knuth in forum New To Java
    Replies: 2
    Last Post: 10-01-2009, 04:46 PM
  5. How show a popWindow that have JPanel like parent
    By Wolverine in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2009, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •