WONDERGG WONDERGG - 7 months ago 17
Java Question

Starting a class from a seperate class

I'm creating a TCP server/client system. Currently I've created a ConnectionFrame.java file, including a JFrame with a JButton, JTextArea and a JTextField. When starting my client, I want this JFrame to appear (therefore when I start my client.jar, the ConnectionFrame should be executed as well.

I assumed I could do this by putting following line into the clients main method:

ConnectionFrame cf = new ConnectionFrame();


But with no luck. Another try was writing:

ConnectionFrame.main(argv);


Which for some reason seemed to mess up my original code.

EDIT: Adding code for context

The ConnectionFrame:

public class ConnectionFrame extends JFrame implements ActionListener {

JButton buttonLayout = new JButton("Connect to server");
JTextField textFieldLayout = new JTextField(1);
JTextArea consoleOutput = new JTextArea(1,20);

public void addComponentToPane(Container pane) {

buttonLayout.addActionListener(this);
textFieldLayout.setHorizontalAlignment(JTextField.CENTER);
consoleOutput.setEditable(false);

pane.add(buttonLayout, BorderLayout.PAGE_START);
pane.add(textFieldLayout, BorderLayout.CENTER);
pane.add(consoleOutput, BorderLayout.PAGE_END);
}

public static void ConnectionFrame() {
ConnectionFrame frame = new ConnectionFrame();
frame.addComponentToPane(frame.getContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent e) {
if("Connect to server".equals(e.getActionCommand())){
consoleOutput.append("CONSOLE: Triggered");
}
}

public static void main(String[] args) {
ConnectionFrame();
}
}


The Client:

public class TCPClient {

public static void main(String argv[]) throws Exception {

ConnectionFrame cf = new ConnectionFrame();

String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 54343);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

CanvasFrame myFrame = new CanvasFrame();
myFrame.setVisible(true);
ArrayList<Point> points = myFrame.location;

outToServer.writeInt(points.size());

for(Point p : points)
{
outToServer.writeInt(p.x);
outToServer.writeInt(p.y);
}

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}

Answer

You need to schedule the creation task for the event-dispatching thread like this:

SwingUtilities.invokeLater(
        new Runnable() {
            public void run() {
                ConnectionFrame();
            }
        }
);

In java 8 it will be:

   SwingUtilities.invokeLater(ConnectionFrame::ConnectionFrame);