Aligning GroupLayout in the center of screen in Java Swing?
-
12-03-2021 - |
Question
So if i have a GUI like this:
import javax.swing.GroupLayout;
public class MyTest extends javax.swing.JFrame {
public MyTest() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("This is a sample GUI");
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 244, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 85, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyTest().setVisible(true);
}
});
}
}
Now when i run the program, it is aligned to (0,0) position on the top-left of the screen. Is there anyway i can align this to the center of the screen or to another custom position?
This works fine if i only use a frame without using GroupLayout, like with setLocation(left,top), but with this implementation, how can i change the default positioning of this GUI?
Solution 3
I guess you did answer the question but it wasn't that clear to me, regarding how to use it. Simply by calling below before pack(); was exactly what i was looking for:
this.setLocation(500,300); //value left, top in pixes
pack();
So basically what i needed to know was the exact use of setLocation(x,y);
and that with the use of keyword "this"
, thank you.
OTHER TIPS
Put the contianer with a GroupLayout
as the single component of a GridBagLayout
with no constraint. The entire container will be centered.
i just need to center the whole GUI (with everthing in it) to the center of the scree,
That has nothing to do with GroupLayout
and everything to do with setLocationRelativeTo(null)
. But read How to best position Swing GUIs for an even better way to position a GUI on-screen (i.e. at a more sensible place than the center of the screen - example below).
Like I mentioned in your earlier question, before I thought you were referring to centering the frame itself - use a GridBagLayout
with no constraint. That will have the effect of putting the component in the center of the parent container. Note that since a frame has window decorations, that means the 'parent container' itself is slightly below screen center. If that is a problem, the only 'fix' I can think of is to use an undecorated frame.
I put 'fix' in inverted commas, because I think it is the requirement that is broken.
import java.awt.*;
import javax.swing.*;
public class MyTest extends JFrame {
public MyTest() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("This is a sample GUI");
getContentPane().setLayout(new GridBagLayout());
JPanel gui = new JPanel();
gui.setBackground(Color.GREEN);
GroupLayout layout = new GroupLayout(gui);
gui.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 244, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 85, Short.MAX_VALUE)
);
add(gui);
pack();
setExtendedState(Frame.MAXIMIZED_BOTH );
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyTest().setVisible(true);
}
});
}
}