Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: JScrollPane overrides JFrame cursor setting, but only at startup
    primarykey
    data
    text
    <p>I have a program that is going to do some work (in a background thread) at startup, so I'd like to display a busy cursor for a few moments while a table is being populated. Unfortunately, no matter what I did, I could not get a wait cursor to appear if my window (<code>JFrame</code>) appeared under the pointer. I did some experimentation, and I tracked it down to a simple test case (below). Basically, if there exists a <code>JScrollPanel</code> (e.g. as a parent for a <code>JTable</code>) as a child of the <code>JFrame</code>, then only the default cursor will appear unless I move the pointer out of the window and back in.</p> <p>Here's the compilable code:</p> <pre><code>package cursortest; import java.awt.Cursor; import java.awt.Dimension; import javax.swing.*; public class CursorTest extends JFrame { void initPanel() { JScrollPane panel = new JScrollPane(); panel.setMinimumSize(new Dimension(500, 500)); panel.setMaximumSize(new Dimension(500, 500)); panel.setPreferredSize(new Dimension(500, 500)); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(panel) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(panel) ); } public CursorTest() { initPanel(); setMinimumSize(new Dimension(500, 500)); setMaximumSize(new Dimension(500, 500)); setPreferredSize(new Dimension(500, 500)); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); pack(); } /** * @param args the command line arguments */ public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new CursorTest().setVisible(true); } }); } } </code></pre> <p>Launch the program so that when the window appears, the mouse pointer is within the bounds of the window. You may have to fiddle with the sizes to get that to happen (that I know I have forced in an artificial way for this example). </p> <p>If <code>initPanel</code> is NOT called, then the mouse cursor will immediately appear as a wait cursor.</p> <p>If <code>initPanel</code> IS called, then the mouse cursor will appear as default until you move the pointer out of the window and then back in.</p> <p>Also, if the <code>JScrollPane</code> is replaced with some other kind of widget, say a <code>JLabel</code>, then this cursor problem does not manifest.</p> <p>Can anyone help me to figure to how to fix this problem? I've thought about also setting the busy cursor for the widgets (my <code>JScrollPanel</code> and/or <code>JTable</code>), but this doesn't work. You could try it by adding the line <code>panel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));</code>.</p> <p>Oh, and I should probably mention that I'm doing this on a Mac.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload