The cursor icon does not change after running the setCursor method

In my application there are JTableresizable header columns. Usually, when I move the cursor over the table title to resize, the cursor icon changes to a resize arrow, such as ↔.

But the following scenario has different things.

There Frameis a button action in the same thing , and during the action I set the cursor to the busy icon and change it to the default cursor after the action is completed using the method Container.setCurosr(Cursor cursor).

Sometimes, if I move the cursor over the header of the resizing table, after the button is pressed, the cursor icon does not change to the resize arrow, the cursor does not change at all.

Can this be considered a bug in Java Swing, or is there a solution to this problem?

Update: sample code attached

import java.util.*;  
import java.awt.*;  
import javax.swing.*;  
import java.awt.event.*;

public class ColumnResizeIconTest extends JFrame {

JScrollPane scrollPane;
JTable table;
JButton button;

public ColumnResizeIconTest() {
    setLayout(new BorderLayout());
    addComponents();
    setSize(300,300);
}

private void addComponents() {
    addButton();
    addTable();
}

private void addButton() {
    button = new JButton("Click Me");
    button.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            setWaitCursor();
            for(int i=0; i<2000; i++) {
                System.out.print(i);
            }
            setDefaultCursor();
        }
    });
    add(button, BorderLayout.NORTH);
}

private void addTable() {
    scrollPane = new JScrollPane(createTable());
    add(scrollPane, BorderLayout.CENTER);
}

private JTable createTable() {
    Object[][] cellData = { { "1-1", "1-2","1-3" }, { "2-1", "2-2", "2-3" }, { "3-1", "3-2", "3-3" } };
    String[] columnNames = { "column1", "column2", "column3" };
    table = new JTable(cellData, columnNames);
    return table;
}

private void setWaitCursor() {
    Container container = getContentPane();
    setWaitCursor(container);
}

private void setWaitCursor(Container container) {
    for(int iCount = 0; iCount < container.getComponentCount(); iCount++) {
        Component child = (Component) container.getComponent(iCount);
        if(child instanceof Container) {
            setWaitCursor((Container) child);
        } else {
            child.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        }
    }
    container.setCursor(new Cursor(Cursor.WAIT_CURSOR));
}

private void setDefaultCursor() {
    Container container = getContentPane();
    setDefaultCursor(container);
}

private void setDefaultCursor(Container container) {
    for(int iCount = 0; iCount < container.getComponentCount(); iCount++) {
        Component child = (Component) container.getComponent(iCount);
        if(child instanceof Container) {
            setDefaultCursor((Container) child);
        } else {
            child.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }
    container.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

public static void main(String[] argv) throws Exception {
    ColumnResizeIconTest test = new ColumnResizeIconTest();
    test.setVisible(true);
}
}

Press the button several times and try resizing the table column. The cursor is stuck with the default cursor.

+5
source share
2 answers

As already mentioned in my comment: it’s not completely trivial to overwrite cursors, even for one component :-) The main problem (in the recursive cursor mode for waiting) is the assumption that all components execute, use the default cursor.

, : " " , resizeCursor, . , : ( , : -)

, , , , , , . :

  • ( ) waitCursor.
  • JLayer (jdk7) JXLayer (jdk6) waitCursor
  • , f.i. JProgressBar JXBusyLabel ( SwingX) -

( @mKorbel: -)

OPs SSCCE ( !): addButton, , , ( ). ...

private void addButton() {
    button = new JButton("Click Me");
    final ActionListener off = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setDefaultCursor();
            button.setEnabled(true);
        }

    };
    button.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            setWaitCursor();
            button.setEnabled(false);
            Timer timer = new Timer(2000, off);
            timer.setRepeats(false);
            timer.start();
        }
    });

    add(button, BorderLayout.NORTH);
}
+8

1)

for(int i=0; i<2000; i++) {
    System.out.print(i);
}

BackGround Task javax.swing.Timer, SwingWorke Runnable # Thread, example

2) Cursor Error/Exception , , ,

+1

All Articles