Event-Handling Between 2 Classes
Trying to learn where did I make a mistake!
Here is a very simple sample code:
2 classes (each has 1 (awt-style)Panel & 1 (awt-style) Button. By clicking on one of the buttons how do I change the Background (color) of the Panel in the other class?
//***********Sample Code **********************
import java.awt.*;
import java.awt.event.*;
//=====================================================
public class App extends Frame
Panel_1 ap1; //ap1 --> additional panel #1
Panel_2 ap2;
public App()
setLayout(null);
setBounds(100,100,300,200);
setBackground(Color.yellow);
//re: Panel_1
//=============
ap1=new Panel_1();
add(ap1);
//re: Panel_2
//===========
ap2=new Panel_2();
add(ap2);
public static void main(String[] args)
App app=new App();
app.setVisible(true);
//==============================================================
class Panel_1 extends Panel implements ActionListener
Panel_2 p2;
Button ap1b=new Button();
Panel_1()
setBounds(50,120,100,50);
setBackground(Color.green);
ap1b.setBackground(Color.red);
ap1b.addActionListener(p2);
add(ap1b);
public void actionPerformed(ActionEvent ae)
if (ae.getSource()==p2.ap2b)
System.out.println("Test Panel 1");
//=================================
class Panel_2 extends Panel implements ActionListener
Panel_1 p1;
Button ap2b=new Button();
Panel_2()
setBounds(50,30,200,50);
setBackground(Color.orange);
ap2b.setBackground(Color.red);
ap2b.addActionListener(p1);
add(ap2b);
public void actionPerformed(ActionEvent ae)
if (ae.getSource()==p1.ap1b)
System.out.println("Test Panel 2");
}
There are several ways to do this. Without going into all of them, let's narrow them down by thinking in terms of design. Why should the panels know anything about each other? Each panel is its own deal. The component which has both of the panels should be the one listening to the buttons.
Really there is no reason to have separate classes for the panels, but if you must, then write a listener as an inner class of the frame class, then pass that listener to the constructor of each panel as you instantiate it. Then you can have the listener work with both panels directly.
Drake
Similar Messages
-
Difference in event handling between Java and Java API
could anyone list the differences between Java and java-API in event handling??
thanks,
cheers,
Hiruthe event handling mechanisms in java language
features and API (java Application Programming
Features)features .no library can work without a
language and no language has event handling built in.
I am trying to compare and contrast the event
handling mechanisms in the language and library
combinations such as java/ java API.
all contributions are welcome!
thanks
cheersSorry, I'm still not getting it. I know what Java is, and I know what I think of when I hear API. (Application Programming Interface.) The API is the aggregation of all the classes and methods you can call in Java. If we agree on that, then the event handling mechanisms in Java and its API are one and the same.
So what do you want to know?
% -
Event handling in seperate class
hello... i have a gui w/ a series of combo boxes, text fields, etc. (call this the GUI file)
upon pressing a button, i need to run a series of event handling that will use info from many of these components
i would like to create a separate java file for the button event handling, but need this file to have access to many componets in the GUI file - does anyone know a nice way to do this?i'm not quite sure how to do that... in my GUI class,
I construct the actuall gui within the constructor..
then i have a simple driver that creates an instance
of class GUI and calls the constructor... how can I
pass a reference of the GUI to the handler in that
scenario??it would be better if you post your code. Some people like me can't just imagine. -
Event handling in global class (abap object)
Hello friends
I have 1 problem regarding events in abap object... how to handel an event in global class in se24 .
Regards
Reema jain.
Message was edited by:
Reema JainHello Reema
The following sample report shows how to handle event in principle (see the § marks)..
The following sample report show customer data ("Header"; KNB1) in the first ALV list and sales areas ("Detail"; KNVV) for the selected customer (event double-click) in the second ALV list.
*& Report ZUS_SDN_TWO_ALV_GRIDS
REPORT zus_sdn_two_alv_grids.
DATA:
gd_okcode TYPE ui_func,
go_docking TYPE REF TO cl_gui_docking_container,
go_splitter TYPE REF TO cl_gui_splitter_container,
go_cell_top TYPE REF TO cl_gui_container,
go_cell_bottom TYPE REF TO cl_gui_container,
go_grid1 TYPE REF TO cl_gui_alv_grid,
go_grid2 TYPE REF TO cl_gui_alv_grid,
gs_layout TYPE lvc_s_layo.
DATA:
gt_knb1 TYPE STANDARD TABLE OF knb1,
gt_knvv TYPE STANDARD TABLE OF knvv.
"§1. Define and implement event handler method
" (Here: implemented as static methods of a local class)
* CLASS lcl_eventhandler DEFINITION
CLASS lcl_eventhandler DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
handle_double_click FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING
e_row
e_column
es_row_no
sender.
ENDCLASS. "lcl_eventhandler DEFINITION
* CLASS lcl_eventhandler IMPLEMENTATION
CLASS lcl_eventhandler IMPLEMENTATION.
METHOD handle_double_click.
* define local data
DATA:
ls_knb1 TYPE knb1.
CHECK ( sender = go_grid1 ).
READ TABLE gt_knb1 INTO ls_knb1 INDEX e_row-index.
CHECK ( ls_knb1-kunnr IS NOT INITIAL ).
CALL METHOD go_grid1->set_current_cell_via_id
EXPORTING
* IS_ROW_ID =
* IS_COLUMN_ID =
is_row_no = es_row_no.
* Triggers PAI of the dynpro with the specified ok-code
CALL METHOD cl_gui_cfw=>set_new_ok_code( 'DETAIL' ).
ENDMETHOD. "handle_double_click
ENDCLASS. "lcl_eventhandler IMPLEMENTATION
START-OF-SELECTION.
SELECT * FROM knb1 INTO TABLE gt_knb1
WHERE bukrs = '1000'.
* Create docking container
CREATE OBJECT go_docking
EXPORTING
parent = cl_gui_container=>screen0
ratio = 90
EXCEPTIONS
OTHERS = 6.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Create splitter container
CREATE OBJECT go_splitter
EXPORTING
parent = go_docking
rows = 2
columns = 1
* NO_AUTODEF_PROGID_DYNNR =
* NAME =
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Get cell container
CALL METHOD go_splitter->get_container
EXPORTING
row = 1
column = 1
RECEIVING
container = go_cell_top.
CALL METHOD go_splitter->get_container
EXPORTING
row = 2
column = 1
RECEIVING
container = go_cell_bottom.
* Create ALV grids
CREATE OBJECT go_grid1
EXPORTING
i_parent = go_cell_top
EXCEPTIONS
OTHERS = 5.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
"§2. Set event handler (after creating the ALV instance)
SET HANDLER: lcl_eventhandler=>handle_double_click FOR go_grid1. " Or:
" SET HANDLER: lcl_eventhandler=>handle_double_click FOR all instances.
CREATE OBJECT go_grid2
EXPORTING
i_parent = go_cell_bottom
EXCEPTIONS
OTHERS = 5.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Display data
gs_layout-grid_title = 'Customers'.
CALL METHOD go_grid1->set_table_for_first_display
EXPORTING
i_structure_name = 'KNB1'
is_layout = gs_layout
CHANGING
it_outtab = gt_knb1
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
gs_layout-grid_title = 'Customers Details (Sales Areas)'.
CALL METHOD go_grid2->set_table_for_first_display
EXPORTING
i_structure_name = 'KNVV'
is_layout = gs_layout
CHANGING
it_outtab = gt_knvv " empty !!!
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Link the docking container to the target dynpro
CALL METHOD go_docking->link
EXPORTING
repid = syst-repid
dynnr = '0100'
* CONTAINER =
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* NOTE: dynpro does not contain any elements
CALL SCREEN '0100'.
* Flow logic of dynpro (does not contain any dynpro elements):
*PROCESS BEFORE OUTPUT.
* MODULE STATUS_0100.
*PROCESS AFTER INPUT.
* MODULE USER_COMMAND_0100.
END-OF-SELECTION.
*& Module STATUS_0100 OUTPUT
* text
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS_0100'. " contains push button "DETAIL"
* SET TITLEBAR 'xxx'.
* Refresh display of detail ALV list
CALL METHOD go_grid2->refresh_table_display
* EXPORTING
* IS_STABLE =
* I_SOFT_REFRESH =
EXCEPTIONS
OTHERS = 2.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*& Module USER_COMMAND_0100 INPUT
* text
MODULE user_command_0100 INPUT.
CASE gd_okcode.
WHEN 'BACK' OR
'END' OR
'CANC'.
SET SCREEN 0. LEAVE SCREEN.
* User has pushed button "Display Details"
WHEN 'DETAIL'.
PERFORM entry_show_details.
WHEN OTHERS.
ENDCASE.
CLEAR: gd_okcode.
ENDMODULE. " USER_COMMAND_0100 INPUT
*& Form ENTRY_SHOW_DETAILS
* text
* --> p1 text
* <-- p2 text
FORM entry_show_details .
* define local data
DATA:
ld_row TYPE i,
ls_knb1 TYPE knb1.
CALL METHOD go_grid1->get_current_cell
IMPORTING
e_row = ld_row.
READ TABLE gt_knb1 INTO ls_knb1 INDEX ld_row.
CHECK ( syst-subrc = 0 ).
SELECT * FROM knvv INTO TABLE gt_knvv
WHERE kunnr = ls_knb1-kunnr.
ENDFORM. " ENTRY_SHOW_DETAILS
Regards
Uwe -
Event handling and inner classes
Hi everyone,
When assigning an actionlistener object to a swing component, is making the encapsulating component implement actionlistener better performance-wise than using inner classes? I ask this because my application is pretty slow to start-up, and I'm using lots of inner classes.
I don't want to go ahead and change the event-handling unless I'm sure it will do any good, because the time restriction is too tight for wasting. Does using addActionListener(this) just pass a reference to itself or is an object generated for each call?
Thanks in advance,
RichardPassing this only passes a reference. But you won't get a big improvement over inner classes. (If you use one listener object instance and pass that instance to the event sources. Do you?)
Kurta -
Event Handler Between Reboot states using Powershell
Hi,
I need some help writing an event handler for a powershell script that would meet the following requirement:
1. Continue Upon a restart
2. Continue Upon a sleep state
3. Continue Upon a hibernation state.One way that I can see that would meed all three of your requirements is to use a permanent WMI Consumer to watch the event log for each of these type of events and then perform an action.
http://learn-powershell.net/2013/08/14/powershell-and-events-permanent-wmi-event-subscriptions/
It would be best to make a filter for each type of event rather than throwing all into one filter. Depending on your OS, the event IDs may be different, but it is nothing that a quick query via a search engine could find for you.
Boe Prox
Blog |
Twitter
PoshWSUS |
PoshPAIG | PoshChat |
PoshEventUI
PowerShell Deep Dives Book -
Event handling between JComboBox & JCheckBox
Hi.
My problem is that when i click on the JCheckBox object, the code (i have) also executes the condition for JComboBox object. I assume there is a very simple solution, but i have yet to find it.
This is the code i'm using:
public void itemStateChanged(ItemEvent e){
if(qcmCheckBox.isSelected()){
System.out.println("checkbox selected");
if(e.getStateChange() == ItemEvent.SELECTED){
System.out.println("combo selected " + comboBoxOptions.getSelectedIndex());
}My problem is, i think, that the e.getStateChange() is always returning true. I just haven't figured out a way to 'single out' when the JComboBox is selected.thanks for the tip, but that didn't exactly work.
these are my steps:
select second drop down option (out of 3)
select checkbox
deselect checkbox
old output
combo selected1
qcm selected
combo selected1new output (using instanceof)
combo selected 1
combo selected 1
checkbox selected
combo selected 1
combo selected 1here's my code:
// setting up vars
private JCheckBox checkBox = new JCheckBox();
private final String comboNames[] = {"Option 1", "Option 2", "Option 3"};
private JComboBox comboBoxOptions = new JComboBox(comboNames);
// combo box
comboBoxOptions.addItemListener(this);
// checkbox
checkBox.addItemListener(this);
// event handler
public void itemStateChanged(ItemEvent e){
if(checkBox.isSelected()){
System.out.println("checkbox selected");
//if(comboBoxOptions instanceof JComboBox){ // the suggested alternative
if(e.getStateChange() == ItemEvent.SELECTED){
System.out.println("combo selected " + comboBoxOptions.getSelectedIndex());
}For some reason, the suggested answer gives me duplicate entries for the dropdown and it still gives me duplicate entries when i click on the checkbox.
Again, what i'm trying to do is just get the checkbox not to execute the 2nd if statement "if(e.getStateChange() == ItemEvent.SELECTED){"
I think the statement "e.getStateChange()" is returning everything true because its an event happening but i don't know a way to single the checkbox event.
I would appreciate all the help I can get. Thanks.
sijis -
Event Handling between JPanels
Hi everyone,
I'm trying a three-fold assignment on java, which relates to graphs and finding strongly connected components.
In the first part, the user can create his own graph and the program should find all SCC and display them.
In the second part, the program creates random graphs and then finds all SCC and displays them.
In the third part, some statistical data is to be displayed.
So what I have done so far is that I have created a JFrame on which I have added 3 JPanels, each for every part of the assignment.
Changing between the JPanels is done by a JTabbedPane.
My problem lies on the second part. I've created a class called RandomGraphPanel, which extends JPanel. Then I've created two sub-JPanels,
called RandomGraphAreaPanel, where the graph should be displayed and RandomGraphDataPanel, where the user can choose some parameters on which the random graph is based. I don't know how I can paint the graph on the first sub-JPanel, after pressing a button on the second sub-JPanel.
I will include some code from my classes:
public class RandomGraphPanel extends JPanel
private JPanel graphArea;
private RandomGraphDataPanel data;
private RandomGraphAreaPanel area;
public RandomGraphPanel()
super();
setLayout(new BorderLayout(5,5));
data = new RandomGraphDataPanel();
area = new RandomGraphAreaPanel();
add(data, BorderLayout.SOUTH);
add(area, BorderLayout.CENTER);
}//RandomGraphPanel constructor
}//RandomGraphPanel class
public class RandomGraphDataPanel extends JPanel
public RandomGraphDataPanel()
super();
//part where RandomGraphDataPanel is filled with 2 JSliders and 1 JButton
}//RandomGraphDataPanel constructor
private class ButtonHandler implements ActionListener
public void actionPerformed(ActionEvent event)
//code that is executed when the button is pressed
}//actionPerformed
}//ButtonHandler private class
}//RandomGraphDataPanel classAny help will be appreciated.
Thanks,
philimonasProbably the best way would be to somehow implement the MVC pattern here. Another way would be to move the button handling code out of the RandomGraphDataPanel class and into the RandomGraphPanel class. You could then pass the ActionListener into the RandomGraphDataPanel object by perhaps a constructor parameter, or by a separate addActionListener method that you have created. Something like so:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
public class RandomGraphPanel extends JPanel
private JPanel graphArea;
private RandomGraphDataPanel data;
private RandomGraphAreaPanel area;
public RandomGraphPanel()
super();
setLayout(new BorderLayout(5, 5));
// *** passing ActionListener to the data class via constructor parameter
data = new RandomGraphDataPanel(new ButtonHandler());
area = new RandomGraphAreaPanel();
add(data, BorderLayout.SOUTH);
add(area, BorderLayout.CENTER);
// *** move this code into this class
private class ButtonHandler implements ActionListener
public void actionPerformed(ActionEvent event)
area.showGraph(); // or whatever is needed to show graph
}and
//import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class RandomGraphDataPanel extends JPanel
public RandomGraphDataPanel(ActionListener buttonHandler)
super();
JButton someButton = new JButton("Show Graph");
someButton.addActionListener(buttonHandler);
private class ButtonHandler implements ActionListener
public void actionPerformed(ActionEvent event)
//code that is executed when the button is pressed
} -
Event handling without new class
I'm new to Java, so please bear with me.
I'm working with a file management software called Intralink which allows automating tasks by recording keystrokes or writing instructions in Java. A typical recorded script would look like:
// Start Macro Recording
import com.ptc.intralink.client.script.*;*
*import com.ptc.intralink.script.*;
public class newpn2 extends ILIntralinkScript {
ILIntralinkScriptInterface IL = (ILIntralinkScriptInterface)getScriptInterface();
private void run0 () throws Exception {
IL.deselectAll( "WSPI" ); // recorded step: 1
IL.select( "WSPI", "New_Generic/template_sm.prt" ); // recorded step: 2
} // End of run0
public void run () throws Exception {
run0 (); // recorded
} // End of function
} // End Macro RecordingI don't know why but the run0() function has to be called from the run() function. (to clarify that, the run0() can be called from anywhere but all the statements beginning with "IL." are ignored unless it was called from the run() function) I'm trying to write something that uses a dialog box created with JFrame, contains a few combo boxes, and has a JButton to then start the run0() function. This fails because the run0() ends up being called from the actionPerformed class, similar to below.
public void actionPerformed(ActionEvent e)
try
run0();
catch (Exception ex)
System.out.println("Error..." + ex);
}I've tried doing it with an anonymous inner class but the result is the same. Is there any way to detect a button click and act on it within the same function that creates the button?Vmtr wrote:
I understand what you're suggesting but this would still require me to call the run0() function from within the listener class and (for whatever reason) the "IL." commands are ignored if they are not in a run0() function called directly from a run() function. I'm assuming that this is a quirk built in by the makers of Intralink and I am just trying to find a way around it.Something is happening behind the scenes, something that I don't see evidenced by your current code. Since Intralink is not your class, I guess it is impossible to create a true SSCCE. When I created an SSCCE based on your code, there were no problems:
//import com.ptc.intralink.client.script.*;
//import com.ptc.intralink.script.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.border.*;
//import java.util.*;
//import java.text.*;
//import java.io.*;
//import java.sql.*;
//import com.ptc.intralink.ila.*;
public class forum extends ILIntralinkScript
ILIntralinkScriptInterface IL = (ILIntralinkScriptInterface) getScriptInterface();
private void run0() throws Exception
JOptionPane.showMessageDialog(null, "In the run0 function!");
IL.openWindow("Workspace", null, null); // recorded step: 1
IL.selectTree("FTLOCALDB", "Local Database"); // recorded step: 2
} // End of run0
public void run() //throws Exception
JButton button1 = new JButton("Done");
button1.setPreferredSize(new java.awt.Dimension(310, 18));
JFrame f = new JFrame("New Part Number");
// add this so it exits nice:
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(360, 300);
f.setResizable(false);
Container content = f.getContentPane();
content.add(button1);
button1.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent ee)
try
run0();
catch (Exception ex)
System.out.println("Error..." + ex);
ex.printStackTrace();
f.setVisible(true);
// get this all to run
public static void main(String[] args)
SwingUtilities.invokeLater(new forum());
// dummy classes to allow for compilation
abstract class ILIntralinkScript implements Runnable
public ILIntralinkScriptInterface getScriptInterface()
return new ILIntralinkScriptInterface();
class ILIntralinkScriptInterface
public void openWindow(String string, Object object, Object object2)
JOptionPane.showMessageDialog(null, "in method openWindow(" + string + ", " + object + ", "
+ object2 + ")");
public void selectTree(String string, String string2)
JOptionPane.showMessageDialog(null, "in method selectTree(" + string + " ," + string2 + ")");
} -
Event handling across unrelated classes
I have a widget type of package that holds some classes that are basically JPanels with appropriate components on them. Specifically, JCheckBox, JComboBox, that kind of thing. These JPanels are used by multiple applications that describe some graphics, so one JPanel allows a user to set properties on Lines, one JPanel allows a user to set properties on Areas, that kind of thing. Several of these are put together in a JTabbedPane, which is another class in this package (this thing can be customized for what is needed).
The applications themselves use what they need for their particular thing. Here's the question. I designed this thinking that I can have these pre-created panels of what I need, and add the listeners in the application. For example, something like this:
tabbedPanePanel.getLinePanel().getColorCombo().addItemListener(new ItemListener......);
When I do this as described in the application, when the colorCombo box is changed, it doesn't trip the event (I was in debug tracing through). Obviously my design is flawed, but I don't know how to change it to make it work. There are two things I need help with improvement.
First, how can I use these re-usable widgets, when the actions are different depending on the application, and the items that the actions would affect are specific to the applications?
Second, what would a better design paradigm be for better encapsulation, and performance for these actions (item listeners and such) and the widgets themselves?
Thank you,
ValeriThink in terms of gathering data instead of actions. That is, each panel gets some user input. Put all the user input in some sort of data object, and provide a method that returns the data object. E.g. Imagine a color panel that lets the user enter R, G, and B:
public class ColorPanel extends JPanel {
// TextFields....
public Color getColor() {
// returns a color based on the values in the text fields
}It is important not to put any OK or Cancel buttons in the panels. You save the OK and Cancel buttons for another main panel that is application specific. When the OK button is pressed, you collect the data and apply it. -
Hi all,
I want to know the significance of creating event handler method in Class builder.
and how it can be used at the time of event trigger.( Is it called automatically or programmer need to call it explicitly in the program.)
Thanks,
Sushant singhHi Sushant,
1. In ABAP Objects, triggering and handling an event means that certain methods act as triggers and trigger events, to which other methods - the handlers - react. This means that the handler methods are executed when the event occurs.
2.To trigger an event, a class must
Declare the event in its declaration part
Trigger the event in one of its methods
3.You declare events in the declaration part of a class or in an interface. To declare instance events, use the following statement:
EVENTS evt EXPORTING... VALUE(e1 e2 ...) TYPE type [OPTIONAL]..
4. An instance event in a class can be triggered by any instance method in the class. Static events can be triggered by any method. However, static methods can only trigger static events. To trigger an event in a method, use the following statement:
RAISE EVENT evt EXPORTING e1 = f1 e2 = f2 ...
5. Events are handled using special methods. To handle an event, a method must
be defined as an event handler method for that event
be registered at runtime for the event.
Declaring Event Handler Methods
Any class can contain event handler methods for events from other classes. You can, of course, also define event handler methods in the same class as the event itself. To declare an event handler method, use the following statement:
METHODS meth FOR EVENT evt OF cif IMPORTING e1 e2 ...
6. Registering Event Handler Methods
To allow an event handler method to react to an event, you must determine at runtime the trigger to which it is to react. You can do this with the following statement:
SET HANDLER h1 h2 ... [FOR]...
It links a list of handler methods with corresponding trigger methods. There are four different types of event.
7. Timing of Event Handling
After the RAISE EVENTstatement, all registered handler methods are executed before the next statement is processed (synchronous event handling). If a handler method itself triggers events, its handler methods are executed before the original handler method continues. To avoid the possibility of endless recursion, events may currently only be nested 64 deep.
Handler methods are executed in the order in which they were registered. Since event handlers are registered dynamically, you should not assume that they will be processed in a particular order. Instead, you should program as though all event handlers will be executed simultaneously.
Reward if useful. -
2 Displayable objects, 1 event handler object
Hi there,
I'm working on an application using J2ME and the MIDP profile. I was wondering, is it possible to use 1 event handler object for 2 Displayable objects? Lets say I have a list on one screen and a form on another, can I use 1 event handler object to handle the events for these two elements? I am implementing my event handler in another class, and I am trying to use the same event handler for all my screens, but it does not seem to be functioning. Any hints?
Also, is it advisable to implement the event handler in a separate class for a J2ME application? Does it really make any difference design-wise and efficiency-wise?
I would appreciate the insight..
Thanks...I would start with adding debug messages in commandAction and in nextScreen:
// debug messages are within System.out.println, do not forget to remove after fix
public void commandAction(Command s, Displayable x) {
System.out.println("command: [" + s.getLabel() + "] screen: [" + x.getTitle() + "]" );
// ...here starts your code...
public void nextScreen(int x) {
System.out.println("index: [" + x + "]);
System.out.println("screen[0] is not null: [" + (screen[0] != null) + "]" );
System.out.println("screen[0] title: [" + (screen[0].getTitle()) + "]" );
System.out.println("screen[1] is not null: [" + (screen[1] != null) + "]" );
System.out.println("screen[1] title: [" + (screen[1].getTitle()) + "]" );
// ...here starts your code...
{code}
then retry run and check messages shown in WTK console... -
Problem in event handling using oo abap code
Hi,
My requirement is i need to disply 3 blocks in ALV format.I have done that.Now my problem is if i double click on aufnr of the first block, it should call the transaction code. I have written the code in oo abap but i have used function modules for ALV.Now my doubt is 'How to write an event once the user double click on the particular field of the first block, it should call the tcode " using object oriented code.
How to populate the heading for each block using oo abap code.
Thanks & Regards
Anushi.....
Use Double_click event handler method of class cl_gui_alv_grid of first block....
if not solved .
Send me Your program lines ...........
Best Regards
Prabhakar -
An event handler for several subclasses.
I've been trying to write an event handler that is parameterized by a window
being passed to it. The event handler is intended to handle the exception event
that occurs when the window completes. I have had problems trying to write this.
The scenario is as follows.
I have a task that listens to events that respresent requests for a window being
opened. On receiving these, it starts the window, also as an asynchronous task.
The windows that may be opened (say window classes B, C, and D) are all
subclasses of window class A. The event handler that I register for (after
instantiating the window) takes a window of class A as parameter. It responds to
the exception events for the Display() method of window passed in.
Now the problems I have encountered are as follows :
To allow the event handler to respond to the exception event of a window of
class A, class A has the exception event defined for it. To allow me to start a
window of class B where completion = event, I also have to define the same
exception event. This hides the return and exception for class A. The
implications of this in the event handler is that the event cannot be trapped
unless I cast the parameter passed in into class B on the ' when return_event '
line. This makes the event handler specific to class B.
(This situation is also presumable caused by the fact that each subclasses
overrides the Display method of window class A, and the exception event is
defined for the Display method.)
An alternative approach I tried was using interfaces. I defined the exception
event as an event on an interface. This was defined with the same parameters as
the exception events of classes B, C, and D would have (ie. the exception
event had two parameters - one of type GenericException, and one of ErrorMgr). I
then made classes B, C, and D implement the interface. The event handler
parameter would be the interface rather than class A. However class B would not
compile as the GenericException parameter for the event in the interface uses
the input mechanism, but the GenericException parameter for the exception event
in the display event of classes B, C, and D uses copy input. I have ben unable
to find a way to change the mechanism for event parameters.
Has anybody got any ideas as to how I may be able to achieve the goals of an
event handler that can respond the exception event of a number of subclasses.
Thanks
Steve Elvin
Systems Developer
Frontline Ltd.
UK.
To unsubscribe, email '[email protected]' with
'unsubscribe forte-users' as the body of the message.
Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/>Steve,
Try this. Have a method in your super class A , say StubDisplay
which processes the event loop.
Also make this method return Exception and/or completion events you
desire.
Instead of overriding Display() in your sublclasses, override this
StubDisplay method.
You need not have to redefine the exception/completion events in
your sublclasses B,C ..because they naturally inherit from the super class
A.
Using interfaces may not be a good idea in this case because, you
will be forced to implement them in your subclasses even if you dont
need them in some specific cases.
Good luck!
Ajith Kallambella M.
Forte Systems Engineer,
Internationational Business Corporation.
From: [email protected][SMTP:[email protected]]
Reply To: [email protected]
Sent: Wednesday, May 13, 1998 4:42 AM
To: [email protected]
Subject: An event handler for several subclasses.
I've been trying to write an event handler that is parameterized by a
window
being passed to it. The event handler is intended to handle the exception
event
that occurs when the window completes. I have had problems trying to write
this.
The scenario is as follows.
I have a task that listens to events that respresent requests for a window
being
opened. On receiving these, it starts the window, also as an asynchronous
task.
The windows that may be opened (say window classes B, C, and D) are all
subclasses of window class A. The event handler that I register for (after
instantiating the window) takes a window of class A as parameter. It
responds to
the exception events for the Display() method of window passed in.
Now the problems I have encountered are as follows :
To allow the event handler to respond to the exception event of a window
of
class A, class A has the exception event defined for it. To allow me to
start a
window of class B where completion = event, I also have to define the same
exception event. This hides the return and exception for class A. The
implications of this in the event handler is that the event cannot be
trapped
unless I cast the parameter passed in into class B on the ' when
return_event '
line. This makes the event handler specific to class B.
(This situation is also presumable caused by the fact that each subclasses
overrides the Display method of window class A, and the exception event is
defined for the Display method.)
An alternative approach I tried was using interfaces. I defined the
exception
event as an event on an interface. This was defined with the same
parameters as
the exception events of classes B, C, and D would have (ie. the
exception
event had two parameters - one of type GenericException, and one of
ErrorMgr). I
then made classes B, C, and D implement the interface. The event handler
parameter would be the interface rather than class A. However class B
would not
compile as the GenericException parameter for the event in the interface
uses
the input mechanism, but the GenericException parameter for the exception
event
in the display event of classes B, C, and D uses copy input. I have ben
unable
to find a way to change the mechanism for event parameters.
Has anybody got any ideas as to how I may be able to achieve the goals of
an
event handler that can respond the exception event of a number of
subclasses.
Thanks
Steve Elvin
Systems Developer
Frontline Ltd.
UK.
To unsubscribe, email '[email protected]' with
'unsubscribe forte-users' as the body of the message.
Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/>
To unsubscribe, email '[email protected]' with
'unsubscribe forte-users' as the body of the message.
Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/> -
What is an event handler..?
I'm trying to learn how to make responsiveness UI. I've read that I can not use SwingUtilities.invokeLater() method if I am in a event handler. The question can seems stupid but I'm only a beginner: what is a event handler? From what I know it might be the actionPerformed method..is it right? Could somebody give me an example? thanks!
Basically an event handler is a class that performs code.
It can be any class (even the same that fires the event), but it has to implement the proper interface for the event you want to catch. If you want to perform special code when someone clicks on a button you want to catch an Action, therefor you need an ActionListener, a class that either implements the ActionListener interface (and therefor the method actionPerformed) or your class has to extend AbstractAction (and therefor has an actionPerformed, too).
It you want to perform special code when someone chooses an element from a list, you want to be informed about a ListSelectionEvent and therefor need a class that implements the ListSelectionListener Interface (and provides the method selectionChanged).
The class that should react on the event has to be declared as being a listener to the object that fires the events. So an actionListener on a Button would be added by
button.addActionListener(listener);or
button.setAction(action);(Note that all variables used are just for clarifiing the type you need and have to be exchanged by the names of your declared objects)
There are many more events ... really, read the tutorial!
Maybe you are looking for
-
Please help! Just purchased my new ipone 4. I have wireless conection at home, the phone finds it, I enter the password, it seems like it connects to it, but I only get the wireless icon for a few seconds and when I open Safari it says that internet
-
I scan a BW negitive and saved it to my computer as a Jpeg or tiff File I open it up in Photoshop Elements 9 editor I go to Select at the top and click on All then go back to Select and click on inverse then a note comes up saying no pixcles where se
-
I cannot add shortcuts to my desktop using your suggested way!
When I drag the icon to the desktop, it looks fine, but when I release the mouse button. there is no shortcut there
-
Hi, im new to web design but im experienced with print. Im learing how to use Adobe Muse and like it. However, before starting my trial and subrscribing to the Creative Cloud just for Muse and later maybe business catalyst i have to make sure hebrew
-
Hi, I have bought Photoshop and Premiere Elements for windows, but I now have a iMac and would like to deactivate on the PC and transfer the programs to the iMac. Is that possible even do I bought it for Windows?