Can I Move Swing Components in CardLayout? [duplicate] - java

This question already has an answer here:
Positioning component inside card layout
(1 answer)
Closed 5 years ago.
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.

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.

Related

Java Layout Manager tips [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I created a small program which is shown in the link. And I used
absolute layout. But I want to try other layouts. Are there any alternative layouts to use apart from absolute layout ?
Here is the link
As a different opinion, I wouldn't recommend GridBagLayout. It is the most flexible so you can do pretty much anything with it but it is pretty hard to use. A simpler way is to use a composed approach, meaning multiple panels in your main UI where each panel has its own layout. For example, for the two top components you can use a vertical BoxLayout and for the bottom part you can use a FormLayout
But the point is, it is better IMO to mix and match layouts than to use one single super flexible layout for everything. Check this link for more info. They mention what I recommended:
Layout Managers are often mixed together in a single frame or dialog,
where a top level container has its own Layout Manager (often a
BoxLayout or BorderLayout), and smaller parts use their own layout,
completely independent of the others.
Another nice tip is to use a GUI builder such as Eclipse's WindowBuilder or Netbean's Matisse; that way you don't design UIs in code, which is pretty abstract
Use gridbaglayout. To properly use it, read first about gridbagconstraints.

Java JFrame Item Positions/Sizes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am aware of the methods and coding required to set a JFrame item's position and size, but I need a bit of help knowing how to correctly size and position items. Is there a way to use another JFrame API/method to separate the window into sections to make positioning items easier? What are the maximum values for height and width? How do I set a button to an average size? If you have some experience with JFrames, please give me some info about this to help me understand how to position and size JFrame items.
You shouldn't be worrying about pixel perfect positioning of components. Swing wasn't meant to be used as such. Being a language that is meant to be run on many different platforms, the GUI library should be flexible as such. To maintain the flexibility, layout managers are introduced. You should be using these layout managers do the sizing and positioning for you.
You can see a visualization of how each work at A Visual Guide to Layout Managers. With these layout managers, you'll want to learn (at the very least)
Which ones respect preferred size and which ones don't.
how each ones are represented visually.
How to create white space, using gaps, empty borders, struts
How use use nested containers with different layout managers to get your desired result.
It may intimidating with all the different possibilities, but like learning anything new, take it one step at a time. Go through each tutorial for each layout manager.

how do i set the size of my window gridbaglayout [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm using gridbaglayout and I want my window to be 700 x 500 always. I also was my JButtons to be the exact same size but if the word inside them is longer they get longer and I can't have this. How can I do this? Please help me with good code I been trying for hours.
I tried to do frame.setSize and panel.setSize but neither work and I need this fast! Please help!
The basic answer is, you don't. That's not the point of any layout manager.
A layout manager simple makes decisions about how best to layout it's children based on the sizing hints that they provide.
Remember, while it might look great on your screen, the next computer you run it on may make it look like crap.
If you "must" define the size of anything, then you need to override the getPreferredSize method of your component and return an appropriate size hint.
Having said that, I wouldn't do this for components like JButton (or actually anything other than JPanel and JComponent), the way they calculate their sizes are complicated and best left alone.
You can modify the size of components through the use of Borders and, in the case of GridBagLayout, Insets and modifying the GridBagConstraints properties.
Have a closer look at How to Use GridBagLayout for some more ideas

Add Dynamically loading images using for loop in JToolBar [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Issue regarding dynamically loading Images in loop using Java Swing
Can any one tell how I would add images dynamically using for loop in JToolBar. I tried a lot but it didn't work for dynamic loading of images. I want to create ToolBar where I load images in a loop.
JToolBar is particularly well adapted to adding JButton instances each having a distinct Icon, so I would advocate using ImageIcon. Complete examples maybe found here, and more were cited in comments to an answer to your previous question.

Java Layout Manager Advice [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've got the idea of how I want my graphics to look but I've been messing around with LayoutManagers for a while and can't seem to find a combination that works well.
Here's the setup:
1 JFrame containing 2 JScrollPanes. ScrollPane1 gets 30% of the JFrame vertical space. ScrollPane2 gets 70% of the JFrame vertical space.
Each JScrollPane has: 1 JLabel centered in the middle at the top of the ScrollPane, 1-4 JButtons centered at the bottom of the scrollpane, and many JCheckBoxes somewhere in the middle of each scrollpane.
Ideally it would look like below:
http://i.imgur.com/f7KsC.jpg
My question is: What layout managers would you use for the JFrame and each JScrollPane?
EDIT: All external toolkits must go through our company's legal division before they can be incorporated into our projects. I would like to stick to internal Java libraries.
EDIT 2: Finished!! Here's the final setup:
http://i.imgur.com/5ndjx.png
What I learned is that it's important to keep compartmentalizing in Java. I was using too few containers for all of my objects hoping they would fit cleanly on the Frame. So I started using way more Panels and Layout Managers and it came out BEAUTIFUL. Looks perfect. Thanks everyone for the help.
IMHO, I would make the content pane of the container a JSplitPane instance.
The upper half would consist of three layered JComponent instances and use the BoxLayout manager with a Y_AXIS alignment. The top layer would use the FlowLayout manager. The middle layer would consist of several juxtaposed JComponent instances, where each would use the BoxLayoutmanager with a Y_AXIS alignment. And of course, the layer itself would use the FlowLayout manager. The bottom layer would consist of several juxtaposed JComponent instances and use the FlowLayout manager.
The lower half would be the exactly the same as the upper half, and therefore, consider encapsulating all this logic within a single custom component.
For more information, see Using Layout Managers.
I would be inclined to use MigLayout at pretty much every step.

Categories

Resources