aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Listeners in SWT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Listeners in SWT" Watch "Listeners in SWT" New topic
Author

Listeners in SWT

K Terr
Greenhorn

Joined: Jun 20, 2006
Posts: 14
Hi,

i was wondering if someone can help me. i'm new to swt, and i am trying to work with two listeners. I have a login screen that a user enters their username and password and then clicks login button. when they click login their is a SelectionListener that will envoke a login method. i also have a listener on the shell that if the user clicks the close (X) button then the entire application just exits. the problem i'm having is that when the user clicks login it is actually exiting the application when it shouldn't. i have added some code at the end. Any help would be greatly appreciated.

Thanks in advanced

k

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Most of these classes are unfamiliar to me, but I think you have added a Listener to your Shell with the possibility of calling System.exit(0). That will of course close your entire app.
Was that what you intended?
K Terr
Greenhorn

Joined: Jun 20, 2006
Posts: 14
Hi,

Thanks for replying. yeah if they hit the X button in the top right hand corner then i want the application to close. if the hit the login button i want the next screen to appear. i think its because i put a listener on the shell but i dont know a way around this.

thanks again

k
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
The more usual way to close the app on clicking the "X" at the top right is to set the properties of the enclosing JFrame. I presume you are familiar with the three things you have to do when setting up a JFrame?There are several alternatives for setBounds, eg setSize (I don't know about setPreferredSize).

The default close operation for a JFrame is "HIDE_ON_CLOSE," so you usually change it to EXIT_ON_CLOSE. That of course obviates any need for a closing listener, unless you have a button or menu item marked "Close," or "Exit."

And try adding all your Listeners to the buttons; if you add the Listeners to other components you end up with a somethingOrOtherPerformed(SomeEventOrOther e){} method with a long list of if and else if blocks in.

I have my own ideas, and fulminate regularly about ActionListeners on these boards, for example here. Add a Listener to each of your buttons; if the Listener has lots to do, just put a method call in its actionPerformed() method which deals with its action.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listeners in SWT