Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to generate an ActionEvent progrematically

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default How to generate an ActionEvent progrematically

    I need to generate an ActionEvent for a JComboBox automatically. I am using following code

                                   String[] List = {"View", "Delete" };
    	                 	ViewEdit = new JComboBox(List);
    	                 	ViewEdit.addActionListener(ViewEditListener);.....
                                    ........
                                    ViewEdit.setActionCommand("comboBoxChanged");
    				ViewEdit.setSelectedItem(1);

    but last tow lines of this code doesn't call actionPerformed method in ViewEditListener. my question is how I should force my program to jump to that method (actionPerformed)?


  2. #2
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to generate an ActionEvent progrematically

    long time, no answer

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to generate an ActionEvent progrematically

    You could manually create an action event and then call the actionPerformed method, or alternatively create the action event and then call a method inside JComboBox to fire the event (I'm not sure what the name is, look it up with google, I think it's something like "dispatchEvent", or "fireEvent")

  4. #4
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to generate an ActionEvent progrematically

    The problem is to create an action event automatically not manually.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to generate an ActionEvent progrematically

    If you want to immediately jump to the code in the listener, rather than trying to create new ActionEvent's and dispatching them, perhaps you could strip out the code from within the actionPerformed method of the listener and encapsulate it in an object or method, in which case you can call that code from either context.
    The problem is to create an action event automatically not manually.
    Not sure what you mean by this, can you elaborate?

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to generate an ActionEvent progrematically

    In a part of my program, I want to execute a procedure which is done normally after one of the items of JComboBox is selected manually. suppose that when I select the first Item of a JComboBox it opens a JEditorPane and displays something. In another part of my program I change the content of this JEditorPane.After changing the content I want the JEditorPane to be refreshed and show new content. so I need the procedure which was done by selecting the first Item of JComboBox previously, executes this time automatically (progremmaticaly) and not by selecting that option manually

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to generate an ActionEvent progrematically

    write a helper method that will perform a task and create the action event for you, or see if the JComboBox has one already (some components in Java do).

  8. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How to generate an ActionEvent progrematically

    Why not just make the code in your combo box ActionListener.actionPerformed method available to the other part of the program? For example make it a method in a class that both parts of the program can access, or pass the action code to the other part of the program, for example, something like this:
    // create an Action to refresh the editor
    Action refreshEditorAction = new AbstractAction("Refresh Editor") {
       public void actionPerformed(ActionEvent e) {
          ... // refresh editor
       }
    }
    ...
    // elsewhere add the action to the combo box
    comboBox.addActionListener(refreshEditorAction);
    ...
     
    // elsewhere again - method to update the display
    void updateDisplay(String newContent, Action refreshEditor) {
       updateEditorContent(newContent);
       refreshEditor.actionPerformed(null);
    }
    ...
     
    // elsewhere - calling the updateDisplay method
    String newEditorContent = getNewEditorContent();
    updateDisplay(newEditorContent, refreshEditorAction);
    ...

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM