4

Today while i was creating a JTabbedPane i noticed a weird (in my opinion) bug, which makes no sense...and since i am looking for this for more than 2 hours, can't find anything neither anything online.

Take a look at this:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UnsupportedLookAndFeelException;

@SuppressWarnings("serial")
public class WhatTheHeck extends JPanel {
    private final JTabbedPane pane = new JTabbedPane();

    public WhatTheHeck() {
        setLayout(new BorderLayout());
        pane.addTab("1", new JTextField("first tf"));
        pane.addTab("2", new JTextField("second tf"));
        add(pane);
    }

    private void display() {
        JFrame f = new JFrame("TabColors");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.setSize(new Dimension(300, 300));
       // f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new WhatTheHeck().display();
            }
        });
    }
}

I am editing the first one and change to 2nd tab. Everything ok.

HOWEVER!

If i edit the first textfield and while i am editing it i change my keyboard language (from english to greek or vice versa) with alt+shift...(windows 7) and change to the 2nd tab, i am getting the following stacktrace:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source) at java.awt.Component.getLocationOnScreen(Unknown Source) at javax.swing.text.JTextComponent$InputMethodRequestsHandler.getTextLocation(Unknown Source) at sun.awt.im.InputMethodContext.getTextLocation(Unknown Source) at sun.awt.windows.WInputMethod$1.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

I'm guessing something is up with alt shift that changes my keyboard language?

Is there a way to avoid getting this error?

George Z.
  • 6,643
  • 4
  • 27
  • 47
  • When the error occurs nothing seems to be affected. Anyone with more experience in swing who is able to confirm that no matter how many times the error will take place nothing will be affected? – George Z. Feb 22 '18 at 21:38
  • I'm on JDK 1.8.0_112 and I don't get that exception with your code – nullPointer Jan 30 '19 at 11:06
  • Potentially related to https://bugs.openjdk.java.net/browse/JDK-8179665, depending on the JDK you use? I suggest you try calling f.setLocationRelativeTo(null); after f.setVisible(true);? – David Kroukamp Jun 16 '20 at 22:28

0 Answers0