0

I'm in the process of learning to use swing components starting with the buttons. I'm trying to get a few buttons to move after clicking one of them. To set the initial location of the buttons, I have such code in my main class' constructor:

    setLayout(null);
    b1 = new JButton("Host");
    b1.setActionCommand("openServer");
    b1.addActionListener(this);
    b1.setBounds(300,10,200,30);
    add(b1);

And two more buttons. In the main method I

frame.setContentPane(newContentPane);

where the NewContentPane was set to be a new object of the main class. Which all works fine and beatifully. But then I click the button, the actionlistener starts its work. In its fine quest it again uses

b1.setBounds(-1,-1,0,0);
b3.setBounds(30,30,50,30);

Or whatever, to hide b1 and make b3 appear which was hidden previously. This alone does nothing, as I expected. So I tried repainting the frame or the panel, which did nothing. So I thought, why not try the same old: setContentPane. And it works, the buttons move properly. But I get a bunch of errors as follows:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:620)
at javax.swing.JFrame.setContentPane(JFrame.java:693)
at packtion.ButtStallion.actionPerformed(ButtStallion.java:86)
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:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I tried everything I could think of including googling. Google just gives links to how to set the initial location of buttons, could never find anything on how to change its location while the program is running.

Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1024
  • 23
  • 2
  • 8
  • 3
    `setLayout(null);`: that is the root of problems. Use an appropriate `LayoutManager`. Take a look at the Java LayoutManager tutorial. – Guillaume Polet Feb 19 '13 at 09:39
  • Never use `Null` layout.Other `LayoutManagers` can help you in a better way.Change Location?`SetLocation(x,y)` – joey rohan Feb 19 '13 at 09:41
  • 2
    Rather then trying to setBounds(-1, -1, 0, 0) for "hiding" buttons, simply use JButton#setVisible(false); – MadProgrammer Feb 19 '13 at 09:43
  • @joeyrohan `setLocation(x,y)` will not work when using a LayoutManager. The basic rule is: if you use a LayoutManager (and you should always do that), you must stop using "setSize()/setBounds()/setLocation/setPreferredSize()". – Guillaume Polet Feb 19 '13 at 10:17
  • To my understanding, null layouts just give me the control of all components' sizes and locations. With a non-resizable window, why is this a problem? I don't understand the reasoning against null layouts. As for Layoutmanagers, I'll get into them regardless, so I'll take that advice, thank you. – 1024 Feb 19 '13 at 10:19
  • See [setLayout(null) is never necessary. Ever!](https://forums.oracle.com/forums/thread.jspa?threadID=1351374) for some more quirks in the ointment. Note that 'moving buttons on button click' is (because of the entire layout manager situation) not nearly as trivial as someone who is just starting out in Swing should try. – Andrew Thompson Feb 19 '13 at 14:11
  • @GuillaumePolet that was in context with null layout – joey rohan Feb 19 '13 at 15:35
  • @joeyrohan ok, but it is hard to understand since your 2 previous sentences clearly emphasize on the usage of a LayoutManager. It is hard to guess that you meant that in the context of no LayoutManager – Guillaume Polet Feb 19 '13 at 17:18

1 Answers1

2

You are trying to assign null to the content pane.

But actually the approach is not good. You should not call setBounds() but use a LayoutManager instead changing the button's constraints. After the change call

container.revalidate();
container.repaint();
StanislavL
  • 56,971
  • 9
  • 68
  • 98