0

Sorry if this has been posted before, but I cannot seem to find any good info that helps me, or I just don't understand other answers enough to help me as I just started programming GUIS.

I wrote a program that has various Items in their own (sub)Jpanel(jtextfields. combobox's, buttons etc.) and all the sub Jpanels in a main jpanel inside a jframe. My goal is to center and stack each sub JPanel on top of one another, so that when the user resizes the window each item stays centered and stacked on top of one another. (when I stacked I don't mean layered where one pane is in front of another, rather stacking the panes like a sandwich so to speak) My panes just move with the default flowlayout and I hope to stop that.

I have seen BoxLayout but like I said, I am new to GUIS and I am not sure if I can apply the BoxLayout to Jpanels.

asked Feb 20, 2014 at 18:21
2
  • 1
    You mean, JPanel, right? And yes, you can set the LayoutManager of any Container. Commented Feb 20, 2014 at 18:22
  • woops ;P yes, I meant JPanel Commented Feb 20, 2014 at 18:25

2 Answers 2

2

First off, what you're referring to is a JPanel, not a JPane

Secondly,

JPanel panel = new JPanel(new BorderLayout());

BorderLayout is probably your best bet. For more information on layouts, check out

http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html#choosing

And by the way, NetBeans GUI Builder is probably your best friend as an introductory GUI programmer.

answered Feb 20, 2014 at 18:24
Sign up to request clarification or add additional context in comments.

9 Comments

haha thank you, I did mean JPanel. I was researching JLayeredPane and was not thinking.
Why is BorderLayout his best bet? I would suggest BoxLayout is what he's looking for.
Because BorderLayout.CENTER works well and will always be centered, which is what he wants. BoxLayout will work the same if there is exactly one component in it.
how would you apply BoxLayout to a JPanel(s) though? The java documentation only has an example for applying it to the JFrame.
JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
|
1

There are easy ways to design your User Interface in Java Swing or the last One JAVAFX. In Swing, there is a eclipse plugin called Swing Windows Builder, there you can easily build your UI by dragging and dropping and As for the JAVAFX, there is Nice and free IDE called Intelli IDE CE, it has also built-in UI designer called Oracle JavaFx Scene Builder. Go for the JavaFx and speed up your development.

answered Nov 17, 2016 at 18:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.