RFR: 8344981: [REDO] JDK-6672644 JComboBox still scrolling if switch to another window and return back

Alexander Zvegintsev azvegint at openjdk.org
Mon Feb 10 21:11:16 UTC 2025


On Tue, 4 Feb 2025 23:35:32 GMT, Damon Nguyen <dnguyen at openjdk.org> wrote:

> Redo for JComboBox infinite scrolling issue. The issue is that when a scrollbar is clicked and held, if the user switches focus (ex: ALT+TAB) while scrolling, when focused is returned to the scrolling application, the JComboBox will still be scrolling even though nothing it being clicked.
> 
> Previously, a KeyboardFocusListener was added to determine the focus. However, there was a memory leak on Windows and Ubuntu. This current implementation uses the current FocusManager and is overall a cleaner, simpler approach.
> 
> CI testing is green on all platforms.

Unfortunately, the provided test fails for me on Ubuntu 22.04.
On the first `Alt + TAB` it switches to the test frame, so it doesn't trigger the timer to stop.

BTW, the test can be automated, e.g. do something like the following

<details>
  <summary>JComboBoxScrollFocusTestAuto.java</summary>


import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicComboPopup;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class JComboBoxScrollFocusTestAuto {

    private static Robot robot;
    private static JFrame comboboxFrame;
    private static JComboBox<String> combobox;


    public static void main(String[] args) throws Exception {
        robot = new Robot();
        try {
            SwingUtilities.invokeAndWait(JComboBoxScrollFocusTestAuto::createAndShowGUI);
            doTest();
        } finally {
            SwingUtilities.invokeAndWait(comboboxFrame::dispose);
        }
    }

    private static void createAndShowGUI() {
        comboboxFrame = new JFrame("JComboBoxScrollFocusTest Test Frame");
        combobox = new JComboBox<>();
        for (int i = 0; i < 100; i++) {
            combobox.addItem(String.valueOf(i));
        }
        comboboxFrame.add(combobox);
        comboboxFrame.setSize(400, 200);
        comboboxFrame.setLocationRelativeTo(null);
        comboboxFrame.setVisible(true);
    }

    static Rectangle getOnScreenBoundsOnEDT(Component component)
            throws InterruptedException, TimeoutException, ExecutionException {
        robot.waitForIdle();
        FutureTask<Rectangle> task = new FutureTask<>(()
                -> new Rectangle(component.getLocationOnScreen(), component.getSize()));
        SwingUtilities.invokeLater(task);
        return task.get(500, TimeUnit.MILLISECONDS);
    }

    private static int getScrollbarValue()
            throws InterruptedException, InvocationTargetException, ExecutionException, TimeoutException {

        FutureTask<Integer> task = new FutureTask<>(() -> {
            BasicComboPopup popup = (BasicComboPopup) combobox.getAccessibleContext().getAccessibleChild(0);
            JScrollPane scrollPane = (JScrollPane) popup.getAccessibleContext().getAccessibleChild(0);
            JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
            return scrollBar.getValue();
        });
        SwingUtilities.invokeAndWait(task);

        return task.get(500, TimeUnit.MILLISECONDS);
    }

    private static void doTest() throws Exception {
        robot.waitForIdle();
        robot.delay(500);

        Rectangle rectangle = getOnScreenBoundsOnEDT(combobox);

        Point ptOpenComboboxPopup = new Point(rectangle.x + rectangle.width - 5, rectangle.y + rectangle.height / 2);

        robot.mouseMove(ptOpenComboboxPopup.x, ptOpenComboboxPopup.y);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        robot.waitForIdle();
        robot.delay(500);


        BasicComboPopup popup = (BasicComboPopup) combobox.getAccessibleContext().getAccessibleChild(0);
        JScrollBar scrollBar = ((JScrollPane) popup.getAccessibleContext().getAccessibleChild(0)).getVerticalScrollBar();

        // Start scrolling
        Rectangle scrollbarBounds = getOnScreenBoundsOnEDT(scrollBar);
        robot.mouseMove(scrollbarBounds.x + scrollbarBounds.width / 2, scrollbarBounds.y + scrollbarBounds.height - 5);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);

        robot.delay(1000);

        if (getScrollbarValue() == 0) {
            throw new RuntimeException("The scrollbar is not scrolling");
        }

        // closing popup by moving focus to the main window
        comboboxFrame.requestFocus();
        robot.waitForIdle();
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

        robot.waitForIdle();
        robot.delay(500);

        // open popup again
        robot.mouseMove(ptOpenComboboxPopup.x, ptOpenComboboxPopup.y);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        robot.waitForIdle();
        robot.delay(500);

        if (getScrollbarValue() != 0) {
            throw new RuntimeException("The scroll bar is scrolling");
        }
    }
}


</details>

-------------

PR Comment: https://git.openjdk.org/jdk/pull/23451#issuecomment-2649244315


More information about the client-libs-dev mailing list