0

I have read this SO Question regarding this problem, but I am still a little lost on how to freely move and position swing components. I am new to Swing, and so I am still learning the ins and outs of it. I have read other documentation and articles online, but I have yet to see anything that shows how to move components around. I have even tried things set as setBounds, but it has been to no avail.

What are some ways I can position components in the CardLayout? Any help is appreciated.

JCMcRae
  • 245
  • 1
  • 5
  • 11
  • 1
    The basic answer is, yes, you could, but you will spend the rest of your career remaking the entire API to compensate for issues which have already been solved by the layout manager API - so, yes, you could, but no, you shouldn't :) – MadProgrammer Oct 03 '17 at 00:52
  • And here you are, the answerer to your linked question himself, @MadProgrammer. Nice answer to that question, BTW. – Hovercraft Full Of Eels Oct 03 '17 at 00:54
  • @HovercraftFullOfEels :) – MadProgrammer Oct 03 '17 at 00:55
  • *"Can I Move Swing Components in CardLayout?"* Sure, just in the same way you can position a card layout in its parent container by giving it the right constraints in the appropriate layout. Just make each card a `JPanel` and position the components in that card/panel using the same principles (layouts and constraints). – Andrew Thompson Oct 03 '17 at 06:20

1 Answers1

2

Your question doesn't make sense, in that it's asking something akin to, can I move an elevator sideways. No, you can't. A CardLayout is for swapping components, and that's all it can do. The JPanel that holds it is given a preferredSize by the layout determined by the maximal preferred sizes of the contained components. This layout is not for positioning, translating, or scaling components, and for these functionalities you'd need to use other layout managers. Fortunately you can nest panels, each using its own layout manager and thereby move or translate components as your use of layouts see fit.

For more detailed answer, consider creating and posting your minimal example program.

And in fact, the question that you linked to answers this much more comprehensively, and so I've made this answer a community wiki and have closed your question as a duplicate. I strongly urge you to read or re-read the Layout Manager Tutorial.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you. I will look into this some more. Like I said, I am very new to it and still learning. – JCMcRae Oct 03 '17 at 00:50
  • @J-Play: 1) read the tutorials, 2) play with some code. 3) don't come back until you've repeated 1 and 2 a number of times. Get really familiar with the tools that you're using. – Hovercraft Full Of Eels Oct 03 '17 at 00:51