Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Strange JFrame / JPanel behaviour

Reply
Thread Tools

Strange JFrame / JPanel behaviour

 
 
emaiale
Guest
Posts: n/a
 
      06-13-2005
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.WindowConstan ts.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.ActionEve nt 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.
 
Reply With Quote
 
 
 
 
Richard Wheeldon
Guest
Posts: n/a
 
      06-13-2005
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
 
Reply With Quote
 
 
 
 
emaiale
Guest
Posts: n/a
 
      06-13-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JPanel inside JPanel inside JFrame t1m1976 Java 1 11-07-2010 09:47 PM
[JPanel] Drop Down JPanel Raphael Jolivet Java 1 07-18-2008 12:30 AM
Add a JPanel to a JPanel... Andreas Beresko Java 6 07-16-2004 12:10 AM
get the value of the position of JPanel inside the JFrame Frédéric Colombero Java 0 11-05-2003 07:25 AM
Re: Invisible JPanel within parent JFrame Roedy Green Java 0 07-30-2003 10:58 PM



Advertisments