Strange JFrame / JPanel behaviour

Discussion in 'Java' started by emaiale, Jun 13, 2005.

  1. emaiale

    emaiale Guest

    Dear friends,
    I have a problem with a JFrame (NewJFrame) and a JPanel (JP) subclass.
    NewJFrame acts as a container for JP. Now, let's suppose that "a" is a
    JP instance variable, with public modifier, to put it simple.
    So, if I want to modify "a" from NewJFrame, and JP is a NewJframe's
    instance variable, it can be done as follows:

    jp.a = 8; // for instance

    so far so good.

    But, I've found a strange behaviour: when I try to print "a" from JP's
    paintComponent() (overloaded), its value remains unchanged.

    Here is the code, edited with Netbeans:

    // ===================================================================
    /**
    *
    * "container" JFrame
    */
    public class NewJFrame extends javax.swing.JFrame {

    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private JP j = new JP();


    public NewJFrame() {
    initComponents();
    }

    private void initComponents() {
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuBar2 = new javax.swing.JMenuBar();
    jMenu2 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();

    jMenu1.setText("Menu");
    jMenuBar1.add(jMenu1);


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jMenu2.setText("Menu");
    jMenuItem1.setText("Item");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jMenuItem1ActionPerformed(evt);
    }
    });

    jMenu2.add(jMenuItem1);

    jMenuBar2.add(jMenu2);

    setJMenuBar(jMenuBar2);

    pack();
    }
    // </editor-fold>


    // modify "a" from NewJFrame
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
    j.a++;
    System.out.println("a: "+ j.a);
    j.repaint();
    System.out.println("a: "+ j.a);
    }

    public void init2(){
    JP j = new JP();
    getContentPane().add(j, java.awt.BorderLayout.CENTER);
    pack();

    }

    public static void main(String args[]) {
    NewJFrame n = new NewJFrame();
    n.init2();
    n.setVisible(true);
    System.out.println(n.j);
    n.j.a = 3;
    System.out.println(n.j.a);
    }

    }



    // "extended" JPanel

    import java.awt.Graphics;
    import javax.swing.JPanel;

    /**
    *
    * extended JPanel
    */
    public class JP extends JPanel{
    public int a =0;
    /** Creates a new instance of JP */
    public JP() {
    }

    public void paintComponent(Graphics g){

    System.out.println(this.a);
    // [snip]
    }
    }

    =========================================================================

    In "jMenuItem1ActionPerformed", (using a JMenu) I tried to

    - increment a
    - print a (apparently incremented)
    - invoke JS repaint, which prints a, again, but NOT incremented... why?
    - reprint a: now it says it's incremented!!!

    every time I call paintComponent (for example, resizing the component),
    it says that a is "its" a.

    It seems like paintComponent maintains its own copy of a, with the
    unchanged value, or that paintComponent doesn't see any update to a...

    Is it a mistake (I hope) or a bug?

    Any help would be appreciated.
    emaiale, Jun 13, 2005
    #1
    1. Advertising

  2. emaiale wrote:
    > public class NewJFrame extends javax.swing.JFrame {

    ....
    > private JP j = new JP();

    ....
    > public void init2(){
    > JP j = new JP();
    > getContentPane().add(j, java.awt.BorderLayout.CENTER);
    > pack();


    You've got two JPs. Why ?
    This is probably your bug,

    Richard
    Richard Wheeldon, Jun 14, 2005
    #2
    1. Advertising

  3. emaiale

    emaiale Guest

    Richard Wheeldon ha scritto:
    > emaiale wrote:
    >
    >>public class NewJFrame extends javax.swing.JFrame {

    >
    > ...
    >
    >> private JP j = new JP();

    >
    > ...
    >
    >> public void init2(){
    >> JP j = new JP();
    >> getContentPane().add(j, java.awt.BorderLayout.CENTER);
    >> pack();

    >
    >
    > You've got two JPs. Why ?
    > This is probably your bug,
    >
    > Richard


    Yes it WAS my bug!!!
    Thanks for your help.
    emaiale, Jun 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    3,046
    Roedy Green
    Jul 30, 2003
  2. Frédéric Colombero
    Replies:
    0
    Views:
    1,085
    Frédéric Colombero
    Nov 5, 2003
  3. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    26,980
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,518
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,406
    t1m1976
    Nov 7, 2010
Loading...

Share This Page