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.
Related
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
Before I start, I'm aware that its a bad idea to not use a Layout Manager and usually I do use one, however, I also have all my components automatically re-size and relocate based on the size of the window. In addition the program I'm working on is only intended to run on 1 machine throughout its entire lifetime. Please don't downvote me just because of lack of layout manager, I found it to be what I need for this particular program.
To my issue, I found a similar post on stackoverflow but a solution was never achieved.
I'm adding a dynamic amount of JLabels to my JPanel, I've noticed that when not using a layout manager, the scroller doesn't work.
This is a simplified version of my initialization code.
JPanel mypanel = new JPanel();
mypanel.setLayout(null);
mypanel.setSize(800,450);
mypanel.setForeground(Color.WHITE);
mypanel.setBackground(Color.BLACK);
scrollablePanel = new JScrollPane(mypanel);
scrollablePanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollablePanel.setPreferredSize(new Dimension(800,300));
scrollablePanel.setSize(800,300);
scrollablePanel.setLocation(250,156);
myContainer.add(scrollablePanel);
where myContainer would be the container (also without a layout manager). The JLabels are added later on to the JPanel with:
enter code heremypanel.add(label1);
after some basic settings are set for the labels such as setForeground and setBackground.
Unfortunately when run, the scrollbar appears as if its not required (see image)
http://i.imgur.com/zp0QKGG.png
The table text seen in the image is made up of multiple JLabels.
If it's not possible to resolve the issue without using a Layout Manager I will switch to BoxLayout, I was just hoping there would be a solution.
The problem is JScrollPanel needs the preferred size of the view component to determine when the view exceeds the scroll pane's viewable area.
The preferred size of a component is normally determined via the layout manager. While you can use setPreferredSize, it is typically discouraged and you will simply run into the same problem as the content exceeds what ever value you decide to set.
A better solution would be to use a LayoutManager or compound layout (using multiple layout managers over multiple containers) to achieve the result you desired...or write your own...
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
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.
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.