There is a JTable
in my application with resizable header columns. Normally when I move the cursor over table header for resizing, the cursor icon changes to resize arrow, like <-->.
But things are different in the following scenario.
There is a button action in the same Frame
, and during action performed, I am setting the cursor to busy icon and change it back to default cursor once the action is completed, using Container.setCursor(Cursor cursor)
method.
Sometimes if I move the cursor over table header of resizing, after a button action, the cursor icon does not change to resize arrow, cursor does not change at all.
Can this be considered as a bug in Java Swing or is there a solution for this issue?
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);
}
}
Click on the button a few times and try to resize the table column. The cursor is stuck with Default cursor.