0

problem in alignment of JFrame in NetBeans. How can I set it in center of main window?

I have tried

public AdminLogin() {
    initComponents();
    this.setLocationRelativeTo(null);
}
Krrish
  • 23
  • 8
  • 2
    Possible duplicate of [How to set JFrame to appear centered, regardless of monitor resolution?](http://stackoverflow.com/questions/2442599/how-to-set-jframe-to-appear-centered-regardless-of-monitor-resolution) – Frakcool Dec 22 '15 at 19:08
  • Two lines of code does not give us the context of your code and how the method is used. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem when you ask a question so we don't have to guess what the code is doing. – camickr Dec 22 '15 at 21:02

1 Answers1

4

You need to invoke the setLocationRelativeTo( null ) after the size of the frame has been determined.

The basic structure for the code should be:

frame.add(...);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
camickr
  • 321,443
  • 19
  • 166
  • 288