GUI added component but blank frame showsup

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

Replay

Category: java Time: 2016-07-31 Views: 60

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.191 (s). 12 q(s)