1

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?

Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • There is no component to set layout on. I have textAreas, ScrollPanel and some Buttons inside layout, but nothing to put the layout on. I just call pack(); and setVisible(true); after defining layout and it starts showing the GUI. I took this idea from Oracle website but it does not show alignment option. something like this: http://docs.oracle.com/javase/tutorial/uiswing/layout/groupExample.html – Johnydep Dec 09 '11 at 10:30
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 09 '11 at 10:33
  • *"Is there anyway i can align it to the center of the screen or to another custom position?"* OK, now I run that code it just raises more questions. Specifically what do you mean by 'it'? I thought you meant the entire panel containing the group layout, but now I'm not so sure. – Andrew Thompson Dec 09 '11 at 12:32
  • Also, what is the point of the label that is never added to the GUI? – Andrew Thompson Dec 09 '11 at 12:33
  • @AndrewThompson, i mean i just need to center the whole GUI (with everthing in it) to the center of the scree, and for label not being added to GUI, i just follwed as is shown here: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/FindProject/src/layout/Find.java – Johnydep Dec 09 '11 at 15:44
  • GroupLayout is not meant for humans (except if they like being tortured which is extremely rare :-) Learn and use a decent third-party manager, any of the big three will do: FormLayout, MigLayout, DesignGridLayout – kleopatra Dec 12 '11 at 11:47
  • What is that example supposed to be good for? One might imagine that if trying to center a layout in a frame, one might **put components in it** for ease of testing (e.g. "is the button in the center yet?"). That code really is pointless for demonstrating anything. – Andrew Thompson Dec 12 '11 at 11:51
  • @AndrewThompson, sorry for annoying you, is there any way the layout itself is in the center, it does not have any parent component. Anything inside the layout can be centered. But i want the actualy layout (the parent of everthing) to be centered. And as you already mentioend setLocationRelativeTo(null) that does not work with layout. But it works fine with JFrame. I hope you understand my point. – Johnydep Dec 12 '11 at 12:25

3 Answers3

4

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

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • can you show me an example. GroupLayout is not a component, then how can i add it to GridBagLayout? – Johnydep Dec 09 '11 at 10:49
  • Can you post an SSCCE of your current code (as I've already suggested)? That answer is the best you'll get from me short of compilable code from you. – Andrew Thompson Dec 09 '11 at 10:51
  • +1 There's a nice example and explanation [here](http://stackoverflow.com/a/8276631/230513) that leverages that question's [SSCCE](http://sscce.org/). – trashgod Dec 09 '11 at 14:08
  • @AndrewThompson, where should i apply the method setLocationRelativeTo(null). Again if i have a frame then i can call it with frame.setLocationRelativeTo(null); but in this case how can i do it? – Johnydep Dec 12 '11 at 11:38
3

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.

Centered GroupLayout

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);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • fine, i guess just the layout can not be centered in the screen. What you have shown is another component inside layout centered relative to layout. So i will have to leave it as is. Thank you – Johnydep Dec 12 '11 at 12:46
0

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.

Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • I don't know what question that claims to be answering, but it is surely not *"Aligning GroupLayout in the center of screen in Java Swing?"* – Andrew Thompson Dec 15 '11 at 15:15
  • I have a GroupLayout layout in the constructor, and all i wanted was to center this GUI in the screen, so please tell me what sholud i have asked then? – Johnydep Dec 15 '11 at 15:52