I am implementing a socket program in java. The server reads a log file and sent it to the client line by line. The client will display the received lines in received order on SWT window. Up to here, the program works fine. But if I click somewhere of the desktop or on the SWT window, the program gets stuck. Any idea to solve this issue. Thanks in advance!
SWT window source code:
public class Client1Interface extends Composite {
private static Text rowsPerSec;
private static Text displayArea;
private static Label lblRowsPerSecond;
private static Button btnMainButton;
private static Button btnExitButton;
private static Client1Impl client1Impl;
private static final String NUMBER_OF_ROWS_PER_SECOND = "Number of Rows/Second :";
private static final String ENTER_AUTHENTICATION = "Enter Authentication :";
private static final String AUTHENTICATE = "Authenticate";
private static final String ENTER = "Enter";
private static final String EXIT = "Exit";
private static final String CLIENT_ADDRESS = "127.0.0.1";
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Client1Interface client1Interface = new Client1Interface(shell, SWT.NONE);
client1Impl = new Client1Impl(CLIENT_ADDRESS, 9090);
btnMainButton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
actionOnMainButton();
}
});
btnExitButton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
client1Impl.getPrintWriter().println(EXIT);
client1Impl.getPrintWriter().flush();
display.dispose();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public Client1Interface(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(6, false));
lblRowsPerSecond = new Label(this, SWT.NONE);
GridData gd_lblRowsPerSecond = new GridData(SWT.CENTER, SWT.CENTER, true, true, 4, 3);
gd_lblRowsPerSecond.widthHint = 142;
gd_lblRowsPerSecond.heightHint = 30;
lblRowsPerSecond.setLayoutData(gd_lblRowsPerSecond);
lblRowsPerSecond.setText(ENTER_AUTHENTICATION);
rowsPerSec = new Text(this, SWT.BORDER);
rowsPerSec.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 3));
btnMainButton = new Button(this, SWT.NONE);
GridData gd_btnMainButton = new GridData(SWT.CENTER, SWT.CENTER, true, true, 5, 2);
gd_btnMainButton.widthHint = 113;
btnMainButton.setLayoutData(gd_btnMainButton);
btnMainButton.setText(AUTHENTICATE);
btnExitButton = new Button(this, SWT.NONE);
GridData gd_btnExitButton = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 2);
gd_btnExitButton.widthHint = 76;
btnExitButton.setLayoutData(gd_btnExitButton);
btnExitButton.setText(EXIT);
displayArea = new Text(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL );
GridData gd_displayArea = new GridData(SWT.CENTER, SWT.CENTER, false, true, 6, 3);
gd_displayArea.widthHint = 768;
gd_displayArea.heightHint = 300;
displayArea.setLayoutData(gd_displayArea);
}
@Override
protected void checkSubclass() {
}
public static void actionOnMainButton() {
String line;
if(btnMainButton.getText().trim().equalsIgnoreCase(AUTHENTICATE)) {
if( client1Impl.authenticateUser(rowsPerSec.getText().trim())) {
lblRowsPerSecond.setText(NUMBER_OF_ROWS_PER_SECOND);
rowsPerSec.setText("");
btnMainButton.setText(ENTER);
}
}
else {
client1Impl.getPrintWriter().println(rowsPerSec.getText().trim());
client1Impl.getPrintWriter().flush();
try {
displayArea.setText(client1Impl.getBufferedReader().readLine() + "\n");
while(! (line = client1Impl.getBufferedReader().readLine()).equalsIgnoreCase(EXIT)) {
displayArea.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
What I have tried:
I tried using Java Swing but it won't print a line in the window