Java GUI layout questions - java

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.

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.

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.

Related

How to design a fixed position panel in Java Swing

Sorry about the basic question. I tried to find an answer on Google looking for expressions such as "unmoving jpanel java swing" or "static jpanel java swing" but could not find anything useful.
I'm trying to compose a screen with two components, an umoving (fixed position) JPanel at the bottom of the screen containing a few buttons and another JPanel which scrolls vertically and horizontally above it.
This construct is going to be fitted in an already existing application.
I could provide some code but I'm assuming the above description is enough to result in one or more suggestions on how to accomplish it overall.

Java Swings GUI, changing the display based on which button is clicked. What is the ideal way to implement this?

I'm pretty new to programming Java based GUI applications. Here's what I have in mind, I want to divide the window into two areas.
The first are contains buttons (or a list), and based on which button was clicked, or which item was selected, the second area changes. (finite number of buttons)
Something like this:
I can think of many ways to do this, but I am not sure what is the best practice. Do I have several invisible panels and make only 1 panel visible at a time, or do I change the ordering (bring panel x to front), or is there some other way?
Appreciate any help I receive!! Thanks in advance!
Although this is a primarily opinion-based answer I'd go with a Nested Layout approach:
Main panel with BorderLayout. Or you can use the frame's content pane which already has BorderLayout as default layout manager.
Left panel with BoxLayout (or GridBagLayout).
Right panel with CardLayout.
Note: Buttons in the left panel should switch right panels cards.
See Lesson: Layoing Out Components within a Container tutorial.
For complex GUIs you have also third-party layout managers available, listed in this answer:
MiG Layout
DesignGridLayout
FormLayout

Resize JFrame and Components Based On Computer Resoultion

I've made a java application in netbeans and am wondering how to have the size of the jframe half the width and height of the computer resolution and also having the components comply with this change. I tried putting code and it did make the frame half the height of the computer resolution but my components, such as buttons and textfields, stopped showing. How can I achieve this? Thanks.
(EDITED)
Set the JFrame's layout manager to GridLayout. In the properties window of the GridLayout itself (select in the navigator window) set columns to 1 and rows to 2. This should give you what you want and you won't have to get into the code.
This is the key code being called within the initComponents() method of your JFrame subclass (created by NetBeans) but it is important to understand where it is:
getContentPane().setLayout(new java.awt.GridLayout(2, 1));
I love Netbeans but you do have to understand the basics.
Good luck with your project. Swing is an awesome toolset that was way ahead of it's time.
As usual in this situation the key is using the right combination of layout managers for your containers. You're probably using NetBeans generated code (something I recommend you avoid until you are very comfortable with Swing coding), and it's probably having you use GroupLayout, a fine layout, but one that might not behave as well as you'd like on resizing components. I suggest that you go through the layout manager tutorial and try to nest JPanel containers and play with different layouts that re-size well such as GridLayout, GridBagLayout and BorderLayout to try to create the best layout that can re-size well.

Resizing a JPanel inside a JFrame

I am new to Java, started learning swing and have a problem with resizing a JPanel inside a JFrame. I am following this tutorial:
http://vincentramdhanie.blogspot.com/2009/11/cardlayout-with-netbeans.html
because ultimately I am learning each of the different layouts and have come to the CardLayout now. In the above, there is a JPanel being used for a status panel. That is what I want to do as well, but when I drag a JPanel onto my blank JForm it takes up all the space and I don't see any resizing handles for it like I would if I were using a .NET panel. Changing preferredSize in the properties window also does nothing. What am I missing here? I feel like a complete noob for asking such a basic question but I really can't find any way to resize this thing.. :-|
EDIT:
I forgot to mention; I am using NetBeans IDE
You can't resize the JComponent because you've select CardLayout. The CardLayout can holds/manages one or more components that share the same display space.
What you need to read documentation and good tutorials.

How to auto resize java swing elements?

I'm trying to auto resize the left side of my application. I have a JTextField and a JTree on the left and 3 JButtons on the right. But I just don't know how to make the left side auto resizeable.
I did it with the Netbeans GUI Creator (or whatever it is called) but I don't know how to to it without Netbeans. (I usually don't program with Netbeans, this was just an exception to see if it's even possible to do so with Swing.
Here is the code Netbeans created: http://pastebin.com/ERwY4rBC
It's not that the code is completely unusable but I wanted to try it manually.
The GroupLayout looks nice, but the Oracle site says it's mainly for the use for GUI tools. So, using GroupLayout would be not "Java like" or how do I have to understand it? Or is there even a better way to achieve this without GroupLayout?
Thanks!
So, using GroupLayout would be not "Java like" or how do I have to understand it
GroupLayout is to put it simply really hard to hand-code, and results mostly in a lot of code. But it is not "not Java like", it is just not something you want to do by hand, and the code afterwards is hard to read as it is rather verbose.
What you try to achieve (according to the screenshot) is easily achievable using some 'nested layouts'. If your main panel uses a BorderLayout where you put the left, resizable panel in the BorderLayout.CENTER and the other, non-resizable panel in the BorderLayout.EAST you will obtain the desired resize behavior.
Then you just have to decide which LayoutManager to use for those individual panels. I think that both the BoxLayout as well as the FlowLayout will do just fine.
Do yourself a favour and use MigLayout for all your layout needs. It is especially convenient for coding UI by hand.
There is a WebStart application on their site that demos different layout situations with code samples provided.

Categories