• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to separate the design from event handling in swing

 
Ranch Hand
Posts: 47
Eclipse IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am using eclipse with windows builder plugin to develop my desktop application. The plugin used is generating the entire code into a single java file for gui.Now, I want to have the separate file for event handling.
For example,
Lets say we have one button and one textfield (code generated in file-Main.java),on click of button, the textfield should be filled with "Hello" .
For that we need to add actionListener to button and need to create an anonymous class and add method actionPerformed(),I want to have this class implementing ActionListener in separate java file(to separate the code we need to have the reference of the button and textfield in event handling class).How to get that reference.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this old post, and the links to older posts in it.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you handle events 4 ways that i know of
1. implement the listeners. you will find this can get long and complicated with lots of if else statements
2. anonymous inner class
3. named inner class //my favorite so far
4. a separate top level class.

i misunderstood your question. you can do that a couple different ways also
1. pass a reference to "this" to the listener and make a variable package access so the listener can access it
2. pass a reference to "this" to the listener and supply get methods //i would go with 1 instead

in other word you have to write a constructor for your event handler sort of like this
myEventHandler(MyClass parent){}

there might be other ways i am unaware of

of course if your main class and the listener class are in different folders it kind of complicates things
you will have to do imports, and option 1 would change to making the variables pulic.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic