JDesktop/JInternalFrame Updating?

I am wondering if anyone can give me a pointer on a problem that I am having with the JDesktop. What I am trying to do is provide feedback to the user during a long load operation by displaying a temporary JInternalFrame tha contains a status message. The attached code simulates the problem. When the user clicks on the "Load" option under the File menu, I want to display a message in a frame to tell the user that the load operation has begun (and to be patient).

What happens, is that the load begins (simulated in this code by an endless loop), but the File/Load menu option fails to clear, and the JInternalFrame is not displayed.

Any help would be greatly appreciated.

Richard

/******************/

/* Sample Code */

/******************/

import java.awt.*;

import java.io.*;

import java.awt.event.*;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.*;

public class Test extends JFrame

{

JDesktopPane desktop;

public Test()

{

super("Test");

int inset = 50;

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setBounds(inset, inset,

screenSize.width - inset*2,

screenSize.height - inset*2);

//Set up the GUI.

desktop = new JDesktopPane();

setContentPane(desktop);

setJMenuBar(AddMenu());

}

public JMenuBar AddMenu()

{

JMenuBar bar = new JMenuBar();

JMenu FileMenu = CreateFileMenu();

bar.add(FileMenu);

return bar;

}

protected JMenu CreateFileMenu()

{

final JMenu menu = new JMenu("File");

menu.add(new SelectableMenuItem("Load"));

return menu;

}

protected class SelectableMenuItem extends JMenuItem

implements ActionListener

{

public SelectableMenuItem(String item)

{

super(item);

addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

JMenuItem item = (JMenuItem) e.getSource();

if (item.getText()=="Load")

{

Load();

}

}

}

public void Load()

{

JInternalFrame StatusFrame=new JInternalFrame("Loading Project", true, false, true, false);

StatusFrame.getContentPane().add(new JLabel("LOADING PROJECT!"));

desktop.add(StatusFrame);

try

{

StatusFrame.reshape(0,0,200,200);

StatusFrame.setVisible(true);

}

catch(Exception ea)

{

System.out.println(ea.getMessage());

}

int x=0;

while(x==0)

{

// simulating a long load of some data

System.out.println("Loading data...");

}

}

public static void main(String[] args) throws Exception {

try

{

UIManager.setLookAndFeel(

"javax.swing.plaf.metal.MetalLookAndFeel");

}

catch (Exception e) { }

Test test=new Test();

test.setVisible(true);

}

}

[2987 byte] By [cassianus12a] at [2007-10-2 5:35:19]
# 1

I think it maybe that when you add the internal frame to the desktop it still needs to repaint itself. But when you enter the infinite loop then processing gets stuck in it. If you put the task into a thread, the everything works well

<code>

public void Load() {

JInternalFrame StatusFrame = new JInternalFrame("Loading Project", true, false, true, false);

StatusFrame.getContentPane().add(new JLabel("LOADING PROJECT!"));

desktop.add(StatusFrame);

try {

StatusFrame.reshape(0, 0, 200, 200);

StatusFrame.setVisible(true);

} catch (Exception ea) {

System.out.println(ea.getMessage());

}

Runnable t1 = new Runnable(){

public void run() {

int x = 0;

while (x == 0) {

// simulating a long load of some data

//System.out.println("Loading data...");

}

}

};

Thread thread = new Thread(t1);

thread.start();

}

</code>

battbota at 2007-7-16 1:45:57 > top of Java-index,Desktop,Core GUI APIs...