Reputation: 1114
I have this code that seems to throw a IllegalComponentException, and I’m not sure why. It boils down to these lines of code:
JRadioButton setRed = new JRadioButton(“Red", true);
JRadioButton setBlue = new JRadioButton("Blue", false);
JRadioButton setYellow = new JRadioButton("Yellow", false);
JPanel options = new JPanel();
options.add(setBlue, BoxLayout.Y_AXIS);//error here
options.add(setRed, BoxLayout.Y_AXIS);//and probably here too
options.add(setYellow, BoxLayout.Y_AXIS);//and here
Here is the error:
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.addImpl(Container.java:1034)
at java.awt.Container.add(Container.java:406)
at DrawCanvas.go(DrawCanvas.java:42)
at DrawCanvas.main(DrawCanvas.java:27)
Upvotes: 0
Views: 431
Reputation: 285405
Your code makes no sense. You don't use the BoxLayout constants when adding components to a JPanel, but rather you use the constants when creating your BoxLayout object, something that you've not yet done.
You need to:
Google: Java BoxLayout tutorial
. First hit.
Upvotes: 2