Problem with JRE 1.6
Basically i have made this program which gets information off the internet and displays it in a JFrame. However when i call the setContentPane method to update my JFrame with new information the JButtons and JFrame stop responding and doesnt display the new contentPane. But the MenuBar still works ok for some reason. It does not cause 100% CPU usage so its not a thread problem, that i know of. If i resize the JFrame with the mouse it updates the contentPane and everything is back to normal.
This problem does not happen in JRE 1.5 update 11 or below.
Is this a Java bug or something? Please help, this is frustrating.
[641 byte] By [
ian_777a] at [2007-11-27 4:54:33]

# 1
> Basically i have made this program which gets
> information off the internet and displays it in a
> JFrame. However when i call the setContentPane method
> to update my JFrame with new information the JButtons
> and JFrame stop responding and doesnt display the new
> contentPane. But the MenuBar still works ok for some
> reason. It does not cause 100% CPU usage so its not a
> thread problem, that i know of. If i resize the
> JFrame with the mouse it updates the contentPane and
> everything is back to normal.
>
> This problem does not happen in JRE 1.5 update 11 or
> below.
>
> Is this a Java bug or something? Please help, this is
> frustrating.
Unless someone has experienced this exact problem before and immediately knows the solution (can happen, I suppose), your best bet for help is to pare down your code to the smallest possible size that still illustrates your problem and post that code here (using code tags of course).
Good luck!
/Pete
ps: and welcome to the forum, by the way!
Message was edited by:
petes1234
# 2
To borrow from camickr (without permission), if you need further help then you need to create a [url=http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, so we can tell exactly why your code is not behaving correctly.
# 3
OK, this is NOT my exact code, but this code exhibits the same behaviour.
I have also found out that if there was text highlighted/selected in a JTextAreabefore clicking the update button then the program would work properly? odd.
Feel free to comment on my style of coding though.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test extends JFrame implements Runnable
{
JTabbedPane tp;
public void run()
{
JPanel temp = new JPanel();
JTextArea ta = new JTextArea("sfddfs");
temp.add(ta);
tp = new JTabbedPane();
tp.addTab("Tab1", new JPanel());
tp.addTab("Tab2", new JPanel());
tp.addTab("Tab3", temp);
tp.setSelectedIndex(2);
MenuBar mb = new MenuBar(); //Menu Bar stuff
Menu m1 = new Menu("File");
MenuItem mi1 = new MenuItem("Update");
mi1.addActionListener( new Update() );
m1.add(mi1);
mb.add(m1);
Dimension ScrnSize = Toolkit.getDefaultToolkit().getScreenSize();
int locW = (ScrnSize.width)/2 - 775/2;
int locH = (ScrnSize.height)/2 - 580/2;
this.setLocation(locW, locH);
this.setMenuBar(mb);
this.setContentPane(tp);
this.setSize(775,580);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void updatePane()
{
tp = new JTabbedPane();
tp.addTab("Tab1", new JPanel());
tp.addTab("Tab2", new JPanel());
tp.addTab("Tab3", new JPanel());
tp.setSelectedIndex(2);
setContentPane(tp);
System.out.println("GUI Updated");
}
private class Update implements ActionListener
{
public void actionPerformed(ActionEvent action)
{
new D().start();
}
private class D extends Thread
{
public void run()
{
int count = 0;
while(count<10)
{
try{Thread.sleep(200);}
catch(InterruptedException e) {e.printStackTrace();}
System.out.println(count);
count++;
}
updatePane();
}
}
}
public static void main(String[] args)
{
Thread thread = new Thread( new Test() );
thread.start();
}
}
# 4
First of all, I don't know of any reason why you should need to set the content pane of a JFrame (corrections to this are welcome!). If you need to update the components, update the old ones, do not create new ones. In this particular case, you don't have any controls on the tabbed pane, but in case you have such in your real program and attach listeners to them, then those listeners are dead the moment you create a new tabbed pane with new controls and set it as the new content pane. This would explain why your components "stop responding" when you update them.
Second, although the content pane may be any kind of a container (and thus any kind of JComponent), you should at best leave it to the default of a JPanel (or at least set it to a new JPanel). I wouldn't set the content pane to a tabbed pane, if at all...
As for the menu bar, the reason it is working is that it is not part of the content pane, so it is not affected by your code (the content pane and the menu bar are held together by a JLayeredPane, which in turn is a contained within the root pane, together with the glass pane).
Concerning your coding style, here's what I would do differently (which is in turn not necessarily best practice):
* Avoid having more than one level of nested classes, and accessing members (especially private members) of classes more than one level above the nested class.
* Use static inner classes where possible.
* The point of that sleep loop is a bit mysterious to me. Except for the debug output, it is basically equivalent to a Thread.sleep(2000), surrounded by a try/catch.
Greets,
Mike
MikePa at 2007-7-12 10:09:16 >

