Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Place form in center of screen

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Place form in center of screen

    My form is wedged against the NW corner of my screen; I want it in the middle. In fact, I will always want the main form (in every project) in the middle. How can I make that the default behavior?


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Place form in center of screen

    I found this on a similar thread:

    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int left = (d.width - this.getWidth()) / 2;
    int top = (d.height - this.getHeight()) / 2;
    this.setLocation(left, top);

    but the "getX()" are not recognized. Do I need an import statement to get it to work? I currently have io, net, swing, awt, and awtevent

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Place form in center of screen

    What do you mean by 'form'? A form isn't a typical swing component...if you mean a JFrame, then the above code snippet should work to position the JFrame in the center of the window.

    but the "getX()" are not recognized
    What getX? I don't see this in the code you posted.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Place form in center of screen

    Do I need an import statement to get it to work?
    An import statement is used by the compiler to find a class definition. It specifies a package that contains classes that are being used in the program.
    It is not involved with locating methods for a class.

    For your posted code, what class is represented by "this"? Is the code in a method in an instance of a class or in the static main method?

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Place form in center of screen

    Why reinvent the wheel?

    // center frame on screen
    frame.setLocationRelativeTo(null);

  6. The Following User Says Thank You to dlorde For This Useful Post:

    KevinWorkman (July 7th, 2011)

  7. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Place form in center of screen

    What getX? I don't see this in the code you posted.
    By "getX()" I meant getWidth() and getHeight().

    The code snippet above won't compile; I get,
    "Description Resource Path Location Type
    The method getHeight() is undefined for the type RTTCPSortSimMain RTTCPSortSimMain.java /RT_TCP_SortSimUtil/src line 48 Java Problem"
    (and similar err msgs for getWidth() and setLocation().

    I also tried this:

    //frame.setLocationRelativeTo(null); <- by default, it sits NW, but this sends it SE

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Place form in center of screen

    what class is represented by "this"? Is the code in a method in an instance of a class or in the static main method?

  9. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Place form in center of screen

    Quote Originally Posted by Norm View Post
    For your posted code, what class is represented by "this"? Is the code in a method in an instance of a class or in the static main method?
    It is in "go":

    public static void main(String[] args) {
    new RTTCPSortSimMain().go();
    }

    public void go() {
    // here is where the previously posted code lives
    ...

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Place form in center of screen

    what class is represented by "this"?
    What class is go in? What Java class is extended by RTTCPSortSimMain?

  11. #10
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Place form in center of screen

    Quote Originally Posted by Blackbird View Post
    I also tried this:

    //frame.setLocationRelativeTo(null); <- by default, it sits NW, but this sends it SE
    If you want any more help, you're going to have to tell us what class you're using for 'frame' and what the environment is. The stuff you've been given works fine on standard Java frames & windows in a normal environment.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Place form in center of screen

    sends it SE
    I noticed that happening when I hadn't set the size yet. The upper left corner of the frame was centered. Because the frame had a size of 0,0.
    Giving the frame a size centered it as expected.

  13. #12
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Place form in center of screen

    Quote Originally Posted by Norm View Post
    I noticed that happening when I hadn't set the size yet. The upper left corner of the frame was centered. Because the frame had a size of 0,0.
    Giving the frame a size centered it as expected.
    I imagine it must be difficult to position an object that has no size!

  14. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Place form in center of screen

    Quote Originally Posted by dlorde View Post
    If you want any more help, you're going to have to tell us what class you're using for 'frame' and what the environment is. The stuff you've been given works fine on standard Java frames & windows in a normal environment.
    I guess I'm new enough that I don't understand the question, so I'll just provide the entire class:
    package com.jcp.tds;

    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class RTTCPSortSimMain {

    JTextArea incoming;
    JTextArea outgoing;
    JTextField tfHost1;
    JTextField tfHost2;
    JTextField tfPort1;
    JTextField tfPort2;
    JLabel lblHost1;
    JLabel lblHost2;
    JLabel lblPort1;
    JLabel lblPort2;
    JLabel lblSent;
    JLabel lblReceived;
    BufferedReader reader;
    PrintWriter writer;
    Socket sock;
    /**
    * @param args
    */
    public static void main(String[] args) {
    new RTTCPSortSimMain().go();
    }

    public void go() {
    JFrame frame = new JFrame("JCP TDS RT TCP Util (AKA 'Fred')");
    JPanel mainPanel = new JPanel();

    // The "Send" (contents from file) TextArea
    outgoing = new JTextArea(15, 50);
    outgoing.setLineWrap(true);
    outgoing.setWrapStyleWord(true);
    outgoing.setEditable(false); // should this be true for outgoing?
    JScrollPane qScrollerOutgoing = new JScrollPane(outgoing);
    qScrollerOutgoing.setVerticalScrollBarPolicy(Scrol lPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    qScrollerOutgoing.setHorizontalScrollBarPolicy(Scr ollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    // The "Receive" (contents from remote machine) TextArea
    incoming = new JTextArea(15, 50);
    incoming.setLineWrap(true);
    incoming.setWrapStyleWord(true);
    incoming.setEditable(false);
    JScrollPane qScrollerIncoming = new JScrollPane(incoming);
    qScrollerIncoming.setVerticalScrollBarPolicy(Scrol lPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    qScrollerIncoming.setHorizontalScrollBarPolicy(Scr ollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    JButton sendButton = new JButton("Send and Receive");
    sendButton.addActionListener(new SendButtonListener());

    tfHost1 = new JTextField(16); //might replace the IP Address hosts w. combo boxes later
    tfHost2 = new JTextField(16);
    tfPort1 = new JTextField(4);
    tfPort2 = new JTextField(4);
    lblHost1 = new JLabel("Host 1 (Dispatch) - IP Address:");
    lblHost2 = new JLabel("Host 2 (Disposition) - IP Address:");
    lblPort1 = new JLabel("Port 1");
    lblPort2 = new JLabel("Port 2");
    lblSent = new JLabel("Sent");
    lblReceived = new JLabel("Received");

    mainPanel.add(lblHost1);
    mainPanel.add(tfHost1);
    mainPanel.add(lblPort1);
    mainPanel.add(tfPort1);

    mainPanel.add(lblHost2);
    mainPanel.add(tfHost2);
    mainPanel.add(lblPort2);
    mainPanel.add(tfPort2);
    mainPanel.add(sendButton);
    mainPanel.add(lblSent);
    mainPanel.add(qScrollerOutgoing);
    mainPanel.add(lblReceived);
    mainPanel.add(qScrollerIncoming);

    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);

    //frame.setLocationRelativeTo(null); <- by default, it sits NW, but this sends it SE

    // Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    // int left = (d.width - this.getWidth()) / 2;
    // int top = (d.height - this.getHeight()) / 2;
    // this.setLocation(left, top);

    setUpNetworking();

    Thread readerThread = new Thread(new IncomingReader());
    readerThread.start();

    frame.setSize(640, 800); // width, height
    frame.setVisible(true);
    }

    private void setUpNetworking() {
    try {
    sock = new Socket("127.0.0.1", 5000);
    InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
    reader = new BufferedReader(streamReader);
    writer = new PrintWriter(sock.getOutputStream());
    System.out.println("networking established");
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    }

    public class SendButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent ev) {
    try {
    writer.println(outgoing.getText());
    writer.flush();

    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    outgoing.setText("");
    outgoing.requestFocus();
    }
    }

    class IncomingReader implements Runnable {
    @Override
    public void run() {
    String message;
    try {
    while ((message = reader.readLine()) != null) {
    System.out.println("client read " + message);
    incoming.append(message + "\n");
    }
    } catch (IOException ex)
    {
    ex.printStackTrace();
    }
    }
    }

    }

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Place form in center of screen

    When posing code Please wrap your code in code tags. Go Advance and use the # icon or
    BB Code List - Java Forums

    Did you see post#11 setSize before setting location.

  16. #15
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Place form in center of screen

    Okay, one of the replies here gave me a clue: I had to move the code in question AFTER the "setSize()" for it to work. Now the sun is shining, the birds are singing again, &c.

    frame.setSize(640, 800); // width, height
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

Similar Threads

  1. Where to place my own library?
    By hexwind in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 06:25 AM
  2. need programmer for my online call center
    By erinbasim in forum Paid Java Projects
    Replies: 6
    Last Post: March 30th, 2010, 01:45 AM
  3. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 09:51 AM
  4. Want to move my button away from center.
    By Fendaril in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2009, 10:05 PM
  5. The Frame to be Center Position
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 10:36 AM