Hi all, I'm newb & I know it. Stuck at some trouble doing my learning activities. The task is, find out whether the given string is a palindrome. All but one work fine.
The problem is: the main window's opened, I put the string into, then press "Check" and an answer window opens. Then I press OK in the answer window. And if I try to put another string into the main window and press Check again - the whole thing crashes. I have to close it and start again if I wanna reuse it. So I think I got a dumb piece of code somewhere in my program. What's the hitch?
(PS sry for poor Eng, it's not my mt)
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Label; public class PalindromeCheck { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Display display = new Display(); Shell shell = new Shell(display); shell.setText("Palindrome check"); shell.setSize(255,150); shell.setMinimumSize(255,150); shell.open(); final Button checkme = new Button(shell,SWT.PUSH); checkme.setText("Check string"); checkme.setBounds(55,50,140,60); final Text checkit = new Text(shell,SWT.SHADOW_IN); checkit.setBounds(10,10,230,30); final Shell dialog = new Shell(shell,SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM); dialog.setSize(250,100); dialog.setMinimumSize(250,100); final Label result = new Label(dialog,SWT.NONE); result.setBounds(10,10,230,30); final Button ok = new Button(dialog,SWT.PUSH); ok.setBounds(105,35,35,30); ok.setText("OK"); Listener dialoglistener = new Listener() { public void handleEvent(Event event) { dialog.close(); } }; ok.addListener(SWT.Selection,dialoglistener); Listener checkmelistener = new Listener() { public void handleEvent(Event event) { boolean answer=true; String check = checkit.getText(); for (j=0; j <= ((check.length() - 1) - j); j++) { String a = Character.toString(check.charAt(j)); String b = Character.toString(check.charAt((check.length() - 1) - j)); if (a.compareToIgnoreCase(b) != 0) { answer=false; break; } } result.setText((answer) ? "The string is a palindrome indeed!" : "The string is not a palindrome!"); dialog.open(); } }; checkme.addListener(SWT.Selection,checkmelistener); while (!dialog.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
i got the following when it crashes:
Exception in thread "main" org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:4361)
at org.eclipse.swt.SWT.error(SWT.java:4276)
at org.eclipse.swt.SWT.error(SWT.java:4247)
at org.eclipse.swt.widgets.Widget.error(Widget.java:4 80)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget. java:417)
at org.eclipse.swt.widgets.Label.setText(Label.java:5 71)
at PalindromeCheck$2.handleEvent(PalindromeCheck.java :56)
at org.eclipse.swt.widgets.EventTable.sendEvent(Event Table.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.ja va:1276)
at org.eclipse.swt.widgets.Display.runDeferredEvents( Display.java:3554)
at org.eclipse.swt.widgets.Display.readAndDispatch(Di splay.java:3179)
at PalindromeCheck.main(PalindromeCheck.java:67)