0

When I pressed Jbutton and called serverChatform method, a blank frame showsup. But when I run the serverChatform seperatly it shows all GUI component.

private void sendActionPerformed(java.awt.event.ActionEvent evt) { 
this.dispose();
try {
serverChatform serverChatform= new serverChatform(); 
serverChatform.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
}

serverChatform.java (This code run when seperatly but not when called from another class method)

package chichat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class serverChatform extends JFrame implements ActionListener {
static ServerSocket server;
static Socket conn;
JPanel panel;
JTextField NewMsg;
JTextArea ChatHistory;
JButton Send;
DataInputStream dis;
DataOutputStream dos;
public serverChatform() throws UnknownHostException, IOException {
panel = new JPanel(); 
NewMsg = new JTextField();
ChatHistory = new JTextArea();
Send = new JButton("Send");
this.setSize(500, 500);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.setLayout(null);
this.add(panel);
System.out.println("ksjfsdk");
ChatHistory.setBounds(20, 20, 450, 360);
panel.add(ChatHistory);
NewMsg.setBounds(20, 400, 340, 30);
panel.add(NewMsg);
Send.setBounds(375, 400, 95, 30);
panel.add(Send);
this.setTitle("Server");
ChatHistory.setVisible(true);
panel.setVisible(true);
Send.addActionListener(this);
//server = new ServerSocket(2000, 1 , InetAddress.getLocalHost());
server=new ServerSocket(2000,1);
ChatHistory.setText("Waiting for Client");
conn = server.accept(); 
System.out.println(conn);
ChatHistory.setText(ChatHistory.getText() + 'n' + "Client Found");
while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
ChatHistory.setText(ChatHistory.getText() + 'n' + "Client:"
+ string);
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + 'n'
+ "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ((e.getSource() == Send) && (NewMsg.getText() != "")) {
ChatHistory.setText(ChatHistory.getText() + 'n' + "ME:"
+ NewMsg.getText());
try {
DataOutputStream dos = new DataOutputStream(
conn.getOutputStream());
dos.writeUTF(NewMsg.getText());
} catch (Exception e1) {
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
NewMsg.setText("");
}
}
public static void main(String[] args) throws UnknownHostException,
IOException {
new serverChatform();
}
}

The serverChatForm shows all gui component while separetly run the file.

Cookie Ninja
  • 1,156
  • 15
  • 29
  • 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 31 '16 at 12:57
  • 3) `panel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jul 31 '16 at 12:58

0 Answers0