How to generate an ActionEvent progrematically
I need to generate an ActionEvent for a JComboBox automatically. I am using following code
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)?
Re: How to generate an ActionEvent progrematically
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")
Re: How to generate an ActionEvent progrematically
The problem is to create an action event automatically not manually.
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.
Quote:
The problem is to create an action event automatically not manually.
Not sure what you mean by this, can you elaborate?
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
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).
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:
Code :
// 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);
...