Update of cursor during high load

Hi All

I have a JFrame and in that Jframe I have a button. When I press that button I want to load another Jframe. Because the second JFrame takes a while to load due to different kind of operations taking place I want to set the cursor to the wait cursor.

No matter what I do, the cursor isnt updated immediately. Sometimes it will take 20 seconds before it changes. I have even put the setCursor in a thread but even take doesnt work. It seams as if Java has totally no idea how to properly balance processor power. I want the cursor to change first then startup the frame.

Has anybody got an idea how to solve this?

Martyn Hiemstra

[667 byte] By [martynhiemstraa] at [2007-11-27 8:32:59]
# 1

It would help if you post some code. It sounds like you're doing something wrong but there's no way to know exactly what. Maybe this example will give you some ideas:

public class CursorTest extends JFrame {

private JButton button = new JButton("Clicky");

private Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR);

private Cursor defCursor = new Cursor(Cursor.DEFAULT_CURSOR);

public CursorTest() {

setSize(640,480);

setLocationRelativeTo(null);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent ev) {

dispose();

}

});

getContentPane().add(button, BorderLayout.NORTH);

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

new Thread() {

public void run() {

setWaitCursor();

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

setDefCursor();

}

}.start();

}

});

}

private void setWaitCursor() {

EventQueue.invokeLater(new Runnable() {

public void run() {

getGlassPane().setCursor(waitCursor);

getGlassPane().setVisible(true);

}

});

}

private void setDefCursor() {

EventQueue.invokeLater(new Runnable() {

public void run() {

getGlassPane().setCursor(defCursor);

getGlassPane().setVisible(false);

}

});

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new CursorTest().setVisible(true);

}

});

}

}

BinaryDigita at 2007-7-12 20:28:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

To summarize the above example, you need to use a separate Thread for the long running task otherwise the code is executed in the GUI EDT and it never gets a chance to repaint itself until the long running task finishes.

"Using Threads in Swing": http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

camickra at 2007-7-12 20:28:59 > top of Java-index,Desktop,Core GUI APIs...