Strange JFrame / JPanel behaviour

E

emaiale

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.
 
R

Richard Wheeldon

emaiale said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top