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?
Printable View
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?
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
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.
What getX? I don't see this in the code you posted.Quote:
but the "getX()" are not recognized
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.Quote:
Do I need an import statement to get it to work?
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?
Why reinvent the wheel?
Code java:// center frame on screen frame.setLocationRelativeTo(null);
By "getX()" I meant getWidth() and getHeight().Quote:
What getX? I don't see this in the code you posted.
The code snippet above won't compile; I get,(and similar err msgs for getWidth() and setLocation().Quote:
"Description Resource Path Location Type
The method getHeight() is undefined for the type RTTCPSortSimMain RTTCPSortSimMain.java /RT_TCP_SortSimUtil/src line 48 Java Problem"
I also tried this:
//frame.setLocationRelativeTo(null); <- by default, it sits NW, but this sends it SE
what class is represented by "this"? Is the code in a method in an instance of a class or in the static main method?
what class is represented by "this"?
What class is go in? What Java class is extended by RTTCPSortSimMain?
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.Quote:
sends it SE
Giving the frame a size centered it as expected.
I guess I'm new enough that I don't understand the question, so I'll just provide the entire class:
Quote:
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();
}
}
}
}
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.
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);