Click to See Complete Forum and Search --> : Help !! (JDesktopPane)open multiple overlapping internal frames


Jacky Kuo
November 25th, 2004, 10:29 PM
:(
Hi,
I wrote a program using JDesktopPane to open multiple documents,
it wiil new a class (chart.java) and open it(JInternalFrame), this is a charting window.
If I open multiple charting windows, the variables, functions... in the chart.java would be shared between the 2 charting windows, So my charting would got something wrong, such as I do something in A charting windows, then the same result I would get in both the 2 charting windows, This is not waht I want.

public class jWindowFrame extends JFrame {
JMenuBar menuBar;
JDesktopPane desktop;
static final Integer DOCLAYER = new Integer(5);
public void newDocument() {
JInternalFrame doc = new chart();
desktop.add(doc, DOCLAYER);
try {
doc.setVisible(true);
doc.setSelected(true);
} catch (Exception e2) {}
}
public void quit() {
try
{
System.exit(0);
}
catch (Exception ex) {}
}
protected JMenu buildFileMenu() {
JMenu file = new JMenu("file");
JMenuItem newWin = new JMenuItem("new doc");
newWin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newDocument();
}
});
file.add(newWin);
return file;
}
protected void buildMenus() {
menuBar = new JMenuBar();
menuBar.setOpaque(true);
JMenu file = buildFileMenu();
menuBar.add(file);
}
protected void buildContent() {
desktop = new JDesktopPane();
getContentPane().add(desktop);
}
public jWindowFrame() {
buildContent();
buildMenus();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
quit();
}
});
}
}

Saeven
November 25th, 2004, 10:46 PM
Smells like static variables which should be private in your chart.java classes.

Remember that static variables are shared across all classes, more correctly so, they belong to the class. So if you change one in a second instantiation of your chart.java - the value will change for all of your chart.java instantiations - simply because they are of that class.

Regards.
Alex

Jacky Kuo
November 25th, 2004, 11:09 PM
Thanks for your reply....
Yes, there are many static variables, functions in my jchart.java, in fact, jchart.java
would new other classes(XXX.java), and then in these so many classes(.java),
some variables, functions have to be set to be static, then they can be shared or whatever, if this is the only way to solve my program, then I have a big deal to change my program.

Any other suggestions?

Any way, thank you very much

Saeven
November 25th, 2004, 11:21 PM
If you are declaring everything as static, and consider this said in a respectful tone - that you need to reevaluate your object model. Static methods are often refereed to static variables only "cannot acces blabla". Abusing static variables results in what you are experiencing.

There's also other downsides to static variables. If you run a heap profiler on a Java program with many static declarations - as the heap grows, these remain at their initial position. So as you instantiate these classes, the jumps to reach these variables becomes a longer and longer stretch until the GC runs and perhaps clears some of them out.

This is probably a simplified case of what you are experiencing:


public class Desktop{
ChartFrame a, b;

public static void main( String args[] ){
a = new ChartFrame( 2, 3 );
b = new ChartFrame( 4, 5 );
a.calculate();
System.out.println( a );
b.calculate();
System.out.println( a );
System.out.println( b );
}
}

public class ChartFrame{
private int x, y;
static int result;

ChartFrame( int a, int b ){
x = a;
y = b;
}

public void calculate(){
result = x + y;
}

public String toString(){
return String.valueOf( result );
}
}


Your results will not be what you would have expected!