Reputation: 291
I am making a client server application in which the server code is executed by a JFrame This is my server code when I call Show method from main method of MyServer class then it is working but when I call it from Key Event of Jframe then it is not showing the another Jframe. Please help.
public class MyServer
public void Show() throws IOException {
ServerSocket ss = new ServerSocket(6666);
new IPScanning().dispose();
int count = 0;
while (true) {
Socket s = null;
try {
s = ss.accept();
SocketThread socketThread = new SocketThread(s, count);
socketThread.start();
} catch (Exception e) {
ss.close();
s.close();
System.out.println(e);
} finally {
count++;
}
}
}
class SocketThread extends Thread {
public void run() {
try {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ShowTable.INSTANCE.showdata(count, host, ip, username, os_name, os_arch, pro_detail, Mac_add, disk_size, max_memory);
}
});
} catch (Exception e) {
System.out.println("Server Problem");
System.out.println(e);
}
}
}
}`
This is my Key Event
private void jStartActionPerformed(java.awt.event.ActionEvent evt) {
MyServer ms=new MyServer();
try {
ms.Show();
} catch (IOException ex) {
System.out.println(ex);
}
}
Code of another JFrame calling through SocketThread class.
public enum ShowTable{
INSTANCE;
private JFrame f = new JFrame();
private JTable jt = new JTable(new DefaultTableModel());
private DefaultTableModel model = (DefaultTableModel) jt.getModel();
private ShowTable() {
jt.setBounds(30, 40, 200, 300);
jt.setFocusable(false);
jt.setRowSelectionAllowed(false);
JScrollPane sp = new JScrollPane(jt);
f.add(sp);
f.setSize(1300, 600);
}
public void showdata(int count,String host,String ip,String username,String os_name,String os_arch,String pro_detail,String Mac_add,float disk_size,float max_memory){
f.setVisible(true);
}
}
Upvotes: 0
Views: 142
Reputation: 347214
private void jStartActionPerformed(java.awt.event.ActionEvent evt) {
MyServer ms=new MyServer();
try {
ms.Show();
} catch (IOException ex) {
System.out.println(ex);
}
}
Suggests that this method is been called in response to some action from the UI (like a button press), which would mean that the event is been dispatched from within the context of the Event Dispatching Thread.
This means that MyServer#show
is been called within the context of the Event Dispatching Thread.
So if we have a look...
public void Show() throws IOException {
ServerSocket ss = new ServerSocket(6666);
new IPScanning().dispose();
int count = 0;
while (true) {
Socket s = null;
try {
s = ss.accept();
SocketThread socketThread = new SocketThread(s, count);
socketThread.start();
} catch (Exception e) {
ss.close();
s.close();
System.out.println(e);
} finally {
count++;
}
}
}
We can see that Show
is creating an infinite loop AND calling a blocking method all within the context of the Event Dispatching Thread - blocking it and prevent the UI from ever been updated again.
Swing is both single threaded and not thread safe. This means:
You need to go and have a read through Concurrency in Swing to gain a better understanding into the issue.
You should also pay close attention to Worker Threads and SwingWorker as they will help provide the answer to solving your question.
However, a overly simplest solution might be to simply use a another Thread
to run the MyServer#Show
method in...
private void jStartActionPerformed(java.awt.event.ActionEvent evt) {
new Thread(new Runnable() {
@Override
public void run() {
try {
MyServer ms=new MyServer();
ms.Show();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
Upvotes: 3