Removing JFrame Border? - java

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);
}
}

Related

Display a JPanel on full screen

Hello I have a JPanel who contained a JTable and a TextField. I want that the display screen is displayed in full screen because in my case it appears half of the screen.here's not a parameter of screen size that I have to specify or change?
This is the result:
public MainWindow()
{
super("Fullscreen");
getContentPane().setPreferredSize(
Toolkit.getDefaultToolkit().getScreenSize());
pack();
setResizable(false);
show();
getContentPane().setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
pack();
setResizable(false);
show();
A few problems with the code:
You can't just set the size of the content pane to be the size of the screen. A frame has decorations (title bar, borders) that need to be considered.
The order of pack() and setResizable(...) is wrong. The pack() statement needs to be invoked AFTER you add components to the screen and after you set the resizable property of the frame.
The show() method is not used anymore.
If you just want to display the frame maximized the code is:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
I suggest you look at the FrameDemo found in the Swing tutorial on How to Make Frames for working code to get you started. I don't know where you found your current code example but I would lose the code and learn how to do Swing coding properly. Bookmark the Swing tutorial for examples of all Swing basics.
the frame has a setSize method, where you can pass the dimension you want.
to get the screen size you can use
Toolkit.getDefaultToolkit().getScreenSize()
setExtendedState(JFrame.MAXIMIZED_BOTH);
Add a ComponentListener with componentResized() method. When the size changed make equal your JPanel's and JTable's size to JFrame's size. And also, while defining JPanel and JTable make their sizes' equal to JFrame's size.

Java - jframe does not fit set dimensions

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.

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);

Centering a JPanel in a JFrame

I have a JPanel that is used to draw the menu for my game and a JFrame that it is added to, I have to add the menu to the middle of the frame so that it will look just as good on a higher resolution as on a lower. I have tried frame.add(menu, BorderLayout.CENTER) and menu.setLocation(windowW / 2, windowH / 2) but none of these options work. What is the easiest way to accomplish this on?
Position a JComponent at the center of a JFrame
use GridBaglayout without override GridBagConstraints, or BoxLayout then JComponent will be placed into center and isn't resizable with container
You need to call the method frame.setLocationRealtiveTo(null) or as parameter the window where the new frame should be referenced to.
EDIT: Think I understood it wrong. You want to add a component to a JFrame to the middle of all. Add instead a JPanel to the JFrame and add the component to the JPanel (BorderLayout.Center).

Why JFrame colour is changing by changing content pane colour?

If the frame is placing on top of the content pane the exterior colour to the user is colour of JFrame. Here even i'm painting the frame after content pane but content pane colour will be displayed. Why?
public class GUI {
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Color myColor = new Color(100,100,100);
frame.setLocationRelativeTo(null);
frame.getContentPane().setBackground(myColor);
frame.setBackground(Color.red);
}
}
You shouldn't be setting the background of the JFrame itself. You CAN, yes, but it doesn't work very well.
It's got a content pane that covers the whole frame, so any color "underneath" will be covered up, as you've found.
All layout and styling should happen in the content pane.
You can set the content pane to a container of your choice, though, with a special layout or whatnot.
Also, when you say "painting the frame after the content pane" that's not actually happening. :) You're setting the background color after you set the background of the content pane, but it doesn't actually get repainted until its repaint flag is triggered by the application runtime.
Then it goes and checks what color is set, and paints. The order that you call the setters doesn't really matter.

Categories