How to prevent my swing components from scattering - java

I am using border layout in my frame and I have used different panels in various regions of the border layout. But when I maximize my frame the components scatter all over the place and the panels are ousted from the frame. Which property should I be manipulating to prevent this?

You can use Absolute Positioning by making setLayout(null).
Check this link for example.
If problem persists try using GridBagLayout or open-source MiG Layout.MiG Layout is helpful in designing complex UI.

Related

change the size of panels inside a desktopPane when it's size changes

I´m using this code for starting the desktopPane maximized
this.setExtendedState(MAXIMIZED_BOTH);
When the jDesktopPane displays it's maximized, but the panels inside are the same size and I don't know how to resize them. I want the panels to resize too
For the JPanel inside your JDesktopPane, you need to implement layout manager. Then those panel resize based on the layout manager. For example, Here is tutorial how to use FlowLayoutManager
I implemented Layout Manager, but used Free Design Instead of Flow Layout, it was easier to me and I got the results I wanted

Why the JInterFrame not resizing?

I am creating one java Application which includes the JDesktopPane and JInternalFrame.
And I am using GridLayOut to set JInternalFrame
The Issue is I can't resize the JInternalFrame as per the GridLayout size
Below is One Screen Shot of Application
I want the JInnernalFrame as the grid size.
Please do help me.
Because JInternalFrames are suppose to be added to and managed by a JDesktopPane
Take a look at How to Use Internal Frames for more details
JDesktopPane isn't suppose to have a layout manager, the GridLayout is taking control and making decision about how the JInternalFrame should be managed.
Simple answer is, don't use a layout manager with a JDesktopPane. Instead use JInternalFrame#pack to size the frames, setLocation to place it and don't forget to make it visible
I want the JInnernalFrame as the grid size
Either don't use JInternalFrames this way, or provide functionality that can mimic this when you want it. Layout managers tend to be to rigid for providing both these functionalities

Java Swing Application Fullscreen mode

I'm working on a Swing Application. The application needs to be in fullscreen all the time including its components. I made the application fullscreen using frmSwingapplication.setExtendedState(Frame.MAXIMIZED_BOTH);
The JFrame contains 2 JPanels. One has JTree and upon the selection of node of the JTree, a new panel is painted in another JPanel.
The problem is, when the application is minimized, it looks like
However when I maximize it, it looks like That is, only covering the portion it did while minimized. How can I resolve this issue? I tried setting size and bounds of container I got from getContentPane() but it was all useless. I even tried it with frame.pack(); and frame.setVisible(true);
You have to use proper layout managers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Certain layout managers will use all the available space and size/lay out components inside them to fill the whole area. Examples for such layout managers are BorderLayout, GridLayout.

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.

Categories