Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
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);
		
		//Starting client service
		client1Impl = new Client1Impl(CLIENT_ADDRESS, 9090);

		//setting actions for main button
		btnMainButton.addListener(SWT.Selection, new Listener() {	
			@Override
			public void handleEvent(Event arg0) {
				// TODO Auto-generated method stub
				actionOnMainButton();
			}
		});
		
		btnExitButton.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				// TODO Auto-generated method stub
				client1Impl.getPrintWriter().println(EXIT);
				client1Impl.getPrintWriter().flush();
				
				display.dispose();
			}
		});
		
		
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Client1Interface(Composite parent, int style) {
		super(parent, style);
//		setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		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.heightHint = 31;
		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(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		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() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}


What I have tried:

I tried using Java Swing but it won't print a line in the window
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900