IllegalComponentStateException in Java Swing

When executing the java swing base program, I had a strange error that I only had on my computer (I tried 2 others, and everything was fine).

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class ModalDialogsTest extends JFrame implements ActionListener{ private JButton choice; ModalDialogsTest(){ setSize(400,300); JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); choice = new JButton("My button"); panel.add(choice); choice.addActionListener(this); } public void actionPerformed(ActionEvent event){ simpleStringChoiceDialog(); } private void simpleStringChoiceDialog(){ Object[] possibilities = {"choice 1", "choice 2", "choice 3"}; String s = (String)JOptionPane.showInputDialog( this, null, null, JOptionPane.PLAIN_MESSAGE, null, possibilities, possibilities[0]); System.out.println(s); } public static void main(String[] args) { ModalDialogsTest newWindow = new ModalDialogsTest(); newWindow.setVisible(true); } } 

The problem is that I get an error message when I click on the drop-down menu to select a choice. Error:

  java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2044) at java.awt.Component.getLocationOnScreen(Component.java:2018) at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:390) at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:388) at sun.lwawt.macosx.LWCToolkit$CallableWrapper.run(LWCToolkit.java:527) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241) at sun.lwawt.macosx.LWCToolkit$CPeerEvent.dispatch(LWCToolkit.java:684) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:691) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:155) at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182) at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:221) at java.security.AccessController.doPrivileged(Native Method) at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:219) at java.awt.Dialog.show(Dialog.java:1077) at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:583) at ModalDialogsTest.simpleStringChoiceDialog(ModalDialogsTest.java:109) at ModalDialogsTest.actionPerformed(ModalDialogsTest.java:70) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:696) at java.awt.EventQueue$4.run(EventQueue.java:694) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) choice 2 

As you can see, an error occurs, but I can still get the result of the function, so I do not understand what is wrong ... Any hint? I am using Java 7 on MacOS X Mountain Lion.

So, here are the results of what you asked me:
System.out.println (System.getProperty ("java.vm.name")); gave me:

 Java HotSpot(TM) 64-Bit Server VM. 

System.getProperties () list (System.out). gave me:

 -- listing properties -- java.runtime.name=Java(TM) SE Runtime Environment sun.boot.library.path=/Library/Java/JavaVirtualMachines/jdk... java.vm.version=23.3-b01 user.country.format=IE gopherProxySet=false java.vm.vendor=Oracle Corporation java.vendor.url=http://java.oracle.com/ path.separator=: java.vm.name=Java HotSpot(TM) 64-Bit Server VM file.encoding.pkg=sun.io user.country=FR sun.java.launcher=SUN_STANDARD sun.os.patch.level=unknown java.vm.specification.name=Java Virtual Machine Specification user.dir=/Users/hugo/Documents/workspace/dialogs java.runtime.version=1.7.0_07-b10 java.awt.graphicsenv=sun.awt.CGraphicsEnvironment java.endorsed.dirs=/Library/Java/JavaVirtualMachines/jdk... os.arch=x86_64 java.io.tmpdir=/var/folders/2n/q2bb2df90qqb_x38djlwx... line.separator= java.vm.specification.vendor=Oracle Corporation os.name=Mac OS X sun.jnu.encoding=US-ASCII java.library.path=/Users/hugo/Library/Java/Extensions:/... java.specification.name=Java Platform API Specification java.class.version=51.0 sun.management.compiler=HotSpot 64-Bit Tiered Compilers os.version=10.8 http.nonProxyHosts=local|*.local|169.254/16|*.169.254/16 user.home=/Users/hugo user.timezone= java.awt.printerjob=sun.lwawt.macosx.CPrinterJob file.encoding=US-ASCII java.specification.version=1.7 user.name=hugo java.class.path=/Users/hugo/Documents/workspace/dialo... java.vm.specification.version=1.7 sun.arch.data.model=64 java.home=/Library/Java/JavaVirtualMachines/jdk... sun.java.command=ModalDialogsTest java.specification.vendor=Oracle Corporation user.language=fr user.language.format=en awt.toolkit=sun.lwawt.macosx.LWCToolkit java.vm.info=mixed mode java.version=1.7.0_07 java.ext.dirs=/Users/hugo/Library/Java/Extensions:/... sun.boot.class.path=/Library/Java/JavaVirtualMachines/jdk... java.vendor=Oracle Corporation file.separator=/ java.vendor.url.bug=http://bugreport.sun.com/bugreport/ sun.cpu.endian=little sun.io.unicode.encoding=UnicodeBig sun.font.fontmanager=sun.font.CFontManager socksNonProxyHosts=local|*.local|169.254/16|*.169.254/16 ftp.nonProxyHosts=local|*.local|169.254/16|*.169.254/16 sun.cpu.isalist= 

EDIT: I forgot to mention that I tried on another Mac exactly the same as mine (besides, I have more memory, but that doesn't matter) and on a Windows 7 computer.

+6
source share
4 answers

I had the same issue with Java 7 on Mac Yosemite. In my case, this is caused by a third-party tool called "Cinch", which I use to place my windows on the desktop. Deactivating the tool eliminates Exceptions.

+6
source

Perhaps you are using a windowing improver (otherwise excellent) zooom ?

I get this error all the time with some Java GUIs, even with commercial software like Matlab.

Besides the messy consoles, this really didn't violate any functionality.

Unfortunately, I have no solution. But perhaps knowing that this could be the reason is helpful.

+4
source

Rewrite how Hovercraft Full Of Eels said:

 public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ModalDialogsTest newWindow = new ModalDialogsTest(); newWindow.setVisible(true); } }); } 

an exception should no longer appear.

EDIT:

compiled and run with Oracle Java7 without problems. OpenJDK for Mac seems to have some problems . If I hadn’t seen this, I would be sure that you didn’t place the whole stack. Check again that you have placed the entire stack. If you placed the whole stack , then:

  • Are you using OpenJDK or Oracle Java ?

  • The other two computers were also a Mac or?

To check which JVM implementation you are using, put this line in your code:

 System.out.println(System.getProperty("java.vm.name")); 

and publish the conclusion. If the output is null, put this line:

 System.getProperties().list(System.out); 

and post-conclusion.

+2
source

Just an idea. Make the panel variable global and then change the first argument of showInputDialog from this to panel .

  String s = (String)JOptionPane.showInputDialog( panel, null, null, JOptionPane.PLAIN_MESSAGE, null, possibilities, possibilities[0]); 

EDIT:

I think these lines

 at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:583) at ModalDialogsTest.simpleStringChoiceDialog(ModalDialogsTest.java:109) 

Suppose the problem is the parentComponent showInputDialog argument. Modify your code and create a JFrame instead of its extension. See also this and this.

EDIT 2:

I changed my code a bit, ran it, and if the exception still exists, then I don't know what else might be the problem ....

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ModalDialogsTest implements ActionListener{ private JButton choice; private JPanel panel = new JPanel(); private JFrame frame = new JFrame(); ModalDialogsTest() { frame.getContentPane().add(panel); panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); choice = new JButton("My button"); panel.add(choice); choice.addActionListener(this); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,300); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { simpleStringChoiceDialog(); } private void simpleStringChoiceDialog() { Object[] possibilities = {"choice 1", "choice 2", "choice 3"}; String s = (String)JOptionPane.showInputDialog( frame.getContentPane(), null, null, // panel, null, null, // <-- or this JOptionPane.PLAIN_MESSAGE, null, possibilities, possibilities[0]); System.out.println(s); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ModalDialogsTest(); } }); } } 
0
source

Source: https://habr.com/ru/post/927301/


All Articles