Java - jframe does not fit set dimensions - java

I've used methods in JFrame:
setSize(600, 600);
setResizable(true);
Next I've created a JButton, and set it's bounds to 0,0,600,600.
I've found that the button is a bit (~40) bigger than the window. It made me some problems when I tried to put a few components exactly where I wanted to. I am using null layout. How to fix it and make my frame exactly 600x600?

The obvious answer is to get rid of the null layout and use a LayoutManager instead. If you want a component to take all available space, use a BorderLayout and put the component in the BorderLayout.CENTER.
To answer your specific question: the size of the JFrame is 600 by 600, but that is not the size of its content pane. The JFrame also contains window decorations which take up some of the size.
You could try to remove those decorations, or simply make your JFrame bigger. Or start wondering about your requirement to have a JFrame with a content pane of exactly 600 by 600 while it is still resizable by the user.

Related

Removing JFrame Border?

I have a Java Swing GUI and everything in my JFrame is offset by a few pixels. On MacOS, I had to offset everything by 12 pixels downward to account for it. On Windows, everything is shifted to the left and downward as well. I discovered that
setUndecorated(true);
removes the JFrame border (which I suspect is the cause of my problems) but it also removes the title bar.
Is there a way I can remove the JFrame border (or some other alternative to make sure everything is centered) and still keep the title bar? I need the title bar so that I can move the JFrame around and have the maximize/minimize/close functions.
Also, the layout is set to null in case that matters. (Everything I'm doing is pixel - based so I cannot set it to anything else).
Thanks.
I found a solution after Googling a bit more. Calling
getContentPane().setPreferredSize(...)
pack();
inside the JFrame constructor will adjust everything so that the titlebar and borders will not impact the view of the content pane.
For anyone else that may need this, you have to set the preferred size of the content pane specifically as that is what you want to appear correctly.
This way you can keep the titlebar and all the normal functionality of a JFrame window that you would otherwise lose with setUndecorated(true);.
The reason why you are adding 12px is because this is consume by the Title and border.
If you use setUndecorated(true) You will loose the title bar and you have to implement the addWindowListener to add a location changing of of an application.
The best way to do is:
Class Main{
public static void main(String[] args){
//JFrame
JFrame frame = new JFrame();
//Create a Main Panel and setPreferred Size and not set Size or set Bound
JPanel mainPanel = new JPanel(); //You value down
mainPanel.setPrefferedSize(new Dimension(x, y));
frame.add(mainPanel);
//and then in last add
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//the pack method help you to setSize of the JFrame According to the
//Component size
frame.pack();
frame.setVisible(true);
}
}

Make a panel resize dynamically in Swing

I'm creating an interface composed out of a box layout which contains panels inside every space.
In this specific case I've got a "cascade" of panels, the first is supposed to be a panel with a FlowLayout as layout manager, underneath it there's a GridLayout and under it there's supposed to be another label.
The thing is that I'd need the first panel to dynamically resize as the window get resized itself.
Here's the problem: I need the first panel to have a specific size in relation to the absolute size.. the thing is that I can't set my preferred size.. in the class I do the following but the panel stays the exact same size as before..
this.setPreferredSize(new Dimension(windowWidth, 50))
I'll send the code as soon as I get home, for now the situation is the one written above.
I need the first panel to have a specific size in relation to the absolute size
Your main panel should be a BorderLayout (which is the default layout of a JFrame), not a BoxLayout.
Then you add the panel that needs to be a specific size to the BorderLayout.NORTH of the frame.
Then your second panel can use a BoxLayout and add this panel to BorderLayout.CENTER of the frame. Now this panel will get all the extra space as the frame is resize.

Set JFrame's components' size relative to window size

I have a resizable JFrame and (since I do not know much about Layouts yet) set its JPanel Layout to null.
Is there a way I can tell my program to resize each of its components' size relative to the window's size?
e.g. I have a 200x100 default sized JFrame, on which a button is 20x10. If I resize the window to 400x200, I want the button to be (still on the same position relative to the window's edges) resized to 40x20.
If that works is there a way to do the same thing with fonts? E.g. size 11 on 200x100 but something like size 14 on 400x200.
Regardless of what convention you use to scale your components and font, it is not a good idea to set a layout to null. Widgets behaving strangely when using "setlayout(null)" will give insight to why this is error prone. Automatically size JPanel inside JFrame might help with scaling when you change the size of the JFrame manually.
Add a ComponentListener to your frame and create a table with the defaultSizes of the Components for a specific windowsize. The componentResized(ComponentEvent e) will be called each time the window is resized. Inside it, you can resize the components. Or you implement your own LayoutManager.

How to auto resize JFrame when elements move on next line using FlowLayout

While you resize JFrame manually via mouse, elements on JPanel can go on next line if there is no enough space, same time new element-lines can be removed if JFrame is big enough
Example:
at this moment there are two lines to fit all JButtons
If I change JFrame width via mouse, JButtons will drop on next lines:
the problem is you can't see all JButtons, JFrame needs to be resized in height.
How to make JFrame auto resize in height to fit all elements according to set width?
This is not a very good design. Show me another professional application that works like this? People don't like to see the size of frames jump. The user is usually resizing the frame to fit the app on their desktop and will get frustrated if they fix the width and the height changes. Let the user control the size.
If you want all the buttons to display, then maybe a better solution is to just make the frame non resizable by using:
frame.setResizable( false );
frame.pack();
frame.setVisible();
Anyway, if you really want to do this then you would probably add a ComponentListener to the content pane of the frame. Then in the componentResized() method you would invoke pack().
However, this will cause multiple events to be generated every couple of pixels so you may also want to use:
Toolkit.getDefaultToolkit().setDynamicLayout( false );
So that the pack() is only done once when the mouse is released.
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
frame.setSize(400,200);

Resizing a JFrame to fit added components

I'm making a program for fun, it's basically a computer navigation GUI, details not required :)
Anyway, so far, I have a button called "new button" that, when clicked, it creates a new button named "test", to an infinite amount. Right now, i have my GUI set up like this:
Class Main extends JPanel (the main panel that holds everything in it, size set as)
Dimension size = new Dimension(300, 200);
setPreferredSize(size);
JFrame holding the Main JPanel, called like:
panel.frame = new JFrame();
panel.frame.setResizable(false);
panel.frame.setTitle(panel.title);
panel.frame.add(panel);
panel.frame.pack();
panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.frame.setLocationRelativeTo(null);
panel.frame.setVisible(true);
So, how would i have the JFrame/JPanel set their size based on the components inside it? I've tried to use panel.frame.pack(); but i get an error most of the time, and the other times it doesnt wrap, it is just a staight line. I want it to resize in the form of a square. Any ideas? Sorry if my question isnt clear/poorly phrased, i've always had issues articulating questions online, much better in person cause i can use my hands! :) Thanks in advance!
Class Main extends JPanel (the main panel that holds everything in it, size set as) Dimension size = new Dimension(300, 200); setPreferredSize(size);
Don't set the preferred size of the panel. The layout manager will determine the preferred size based on the components that you add to the panel.
and the other times it doesnt wrap,
The default layout manager for a JPanel is a FlowLayout. It is not designed to wrap automatically. Maybe use a different layout manager. Or you can try the Wrap Layout which extends FlowLayout to provide dynamic wrapping.
I've tried to use panel.frame.pack(); but i get an error most of the time
What error. I've never seen an error when using the pack() method.
Post a proper SSCCE if you need more help.

Categories