Thursday, 10 November 2011

Java Swing setting location of the JFrame form


Setting the location of the JFrame Form...




Tutorial content,

  • Continue from previous work
  • Setting the JFrame location 

If you have completed the previous tutorial, you might realize that, JFrame Form pops out from the top left side of the computer screen.

In order to get this window to pop out from the centre of the screen, you will have to do a small adjustment in the code.

Go to the Source code and find the constructor of the JFrame Form. In this case its NamePrinter() method.

Inside that method (the constructor), after the initComponents() method, call the method, setLocationRelativeTo(null)

so now the method should look like this,

 public NamePrinter() {
        initComponents();
        setLocationRelativeTo(null); // instead of null, you can add different relative terms as well
                                                   // play around with different values
    }


Now run this file. You will notice that the JFrame Form pops out from the centre of the screen.



I'll add navigation and some more stuff in my next tutorial :D 


1 comment: