How can I determine the midpoint of the users display? - java

I've written a Swing application. I want to set the JFrame to be centred in the user's screen. I can set it to the approximate center of my screen using:
JFrame frame = new JFrame("Test Frame");
Rectangle r = new Rectangle(600, 300, 400, 400);
frame.setBounds(r);
Is there a way to define the Rectangle r such that the center of it is at the center of any screen?
Is setBounds() the wrong method for varying the frame position dynamically?

Do you want to center a JFrame in the screen? If so then simply call setLocationRelativeTo(null) on the JFrame after calling pack(). You really don't want to set the size of the JFrame if you can avoid it, but instead have your layout managers do the size setting for you. The pack() method will ask the containers' layout managers to lay out their components and set the best sizes for their components.
Edit
Regarding your question:
Thanks, that's great. Any chance you could tell me where I could find the method details within docs.oracle
Since this method can be called on a JFrame, simply go to the JFrame API. If this is a method of a parent class, the API will tell you and provide the link to the parent class and its method.

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

Resize a JFrame, which has no border, using a mouse

I am working on a Java desktop application using javax.swing.
I want make my application border-less and at the same time resizeable.
When I remove the border using the frame.setUndecorated(true); method I can no longer resize the frame using mouse clicks and drags.
How can I hide the border of the frame and still let the user resize it?
Frames allow a user to interact with the JFrame using their mouse. If you remove the frame then you cannot use it to move or resize the frame. You can reimplement this functionality yourself (not sure why you'd want to but of course experimenting is always fun!).
First you must somehow get user mouse events. By creating a custom JComponent, maybe called ResizeGrip, and placing it in the bottom right of your frame you can visually show your user that they can still resize the frame. You can then implement a MouseListener to see when the user has clicked and dragged your ResizeGrip.
Then you need to turn those event detections into instructions to resize the frame pragmatically, that is via someJFrame.setSize(newWidth, newHeight);. You will also need to do something similar if you want to move the frame.
Check out Resizing Component for a general purpose class that will allow you to resize any component.
The class is a MouseListener that installs itself on the components you specify.
The basic code to add it to your frame would be:
JFrame frame = new JFrame("SSCCE");
frame.setUndecorated(true);
ComponentResizer cr = new ComponentResizer();
cr.registerComponent(frame);
You can control which sides can be dragged by specifying the "drag insets". The default value is 5 for all sides which means you can resize any size. You could limit the frame to be resized only horizontally by using:
cr.setDragInsets( new Insets(0, 0, 0, 5) );

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

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