Results 1 to 4 of 4
  1. #1
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default Need Help with events

    Hello All-

    So i created a custom widget component in netbeans. The widget component has 5 buttons which when clicked I need it to send and even or message to the containing frame to call a specific function with parameters that the widget does not know about.

    For example... the widget has a button called open. I want the the main frame to call widget.open(param1, param2). I am not sure what event to use to tell the frame to call that function. I tried to create a custom event, but I get lost after I create the event and the listener interface.

    Tutorials on custom events with step by step guide would be great. I'm very visual and procedural so the more description would be excellent.

    Thanks In Advance
    Gatts79

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    widget has a button called open. I want the the main frame to call widget.open(param1, param2).
    This doesn't make sense. What you show here is a method called "open" being called with 2 params.
    When/why does the main class call the widget open method?
    If widget has the buttons, then wouldn't it be the listener in widget that would do the calling when the button is pressed?

    Could you step thru the event sequence again? Is it started by a button press? And then what?

  3. #3
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default Rundown

    Hey Norm-

    So basically I created a widget which is a panel with 5 buttons that performs file IO based on information that the containing frame and 3 other widget contains.

    In this case widget1.open(parm1, parm2) loads a ms3d file and then resets 2 other gui widgets that contain transform and animation controls (parm1 and parm2). Each widget does not know about the other widgets and I would like to keep it that way if possible.

    So what I want to happen is when the open button on widget1 is pressed; the frame containing the widget1 calls the method widget1.open(parm1, parm2). widget1 can't call it's open method because it doesn't know about parm1 or parm2. So, as I said in the first post; I tried to do a custom event and listener for so that the frame containing widget1 can listen and react to the events that widget1 puts out. Unfortunately I got lost going over the tutorials I saw online about custom events and listeners. I then tried a very unoptimized approach of having the buttons turn booleans to true if it was selected/clicked and having the frame check the booleans and call the appropriate methods. I wanted to use an actionlistener for the widget but the widget wouldn't allow the adding of an actionlistener. I tried having the button do a firePropertyChange(string, null, null) and have the string represent which button was clicked, but i never get the change event. I tried a mouselistener, but it doesn't seem to get the click, release or pressed events; it only seems to get the mouse enter and exit events.

    So what I really need to know is what people think is the best course of action, if i want each widget to not know each other.

    Thanks In Advance
    Gatts79

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    when the open button on widget1 is pressed; the frame containing the widget1 calls the method widget1.open(parm1, parm2).
    Confusion here. How do you get a frame to call your code?
    What is the relationship between the widget and the button?
    Where is the actionListener that receives the event?
    Is it in the class you call "frame"?
    frame containing widget1 can listen and react to the events that widget1 puts out.
    Create an interface class for your event listener.
    Create a event class for your event.
    Have the "frame" class implement the interface.
    Have the "frame" class call widget1's add<YourEvent>Listener(this) method with a this pointer.
    in widget1 save the listener in a local variable.
    When widget1 has an event to pass to its listeners, create an event object and call the listener that was saved above.

    This should work exactly like you use any other listener. Look at ActionListener for example.
    Last edited by Norm; 09-23-2008 at 04:33 AM.

Similar Threads

  1. swing events
    By chitra in forum AWT / Swing
    Replies: 3
    Last Post: 09-20-2008, 05:57 PM
  2. Replies: 0
    Last Post: 07-25-2008, 03:34 PM
  3. Need Help With Button Events
    By adlb1300 in forum New To Java
    Replies: 5
    Last Post: 11-03-2007, 06:36 AM
  4. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 03:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •