ActionEvent
class extends the AWTEvent class. The event is passed to every
ActionListener object that registered to receive such events using
the component's addActionListener method.
Methods
in ActionEvent Class
String
getActionCommand()
Returns
the command string associated with this action
public
long getWhen()
Returns
the timestamp of when this event occurred.
public
int getModifiers()
Returns
the modifier keys held down during this action event.
public
String paramString()
Returns
a parameter string identifying this action event.
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.text.SimpleDateFormat; import java.util.Date; public class ActionEventEx{ JTextArea displayArea; JFrame frame1; ActionEventEx(){ JButton click = new JButton("Click Me"); JButton submit = new JButton("Submit"); /* Initialize Display Area */ displayArea = new JTextArea(); displayArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(displayArea); scrollPane.setPreferredSize(new Dimension(450, 150)); /* Initialize Frame */ frame1 =new JFrame("Sample Frame"); frame1.pack(); frame1.setVisible(true); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.getContentPane().add(click, BorderLayout.WEST); frame1.getContentPane().add(submit, BorderLayout.EAST); frame1.getContentPane().add(scrollPane, BorderLayout.CENTER); frame1.setSize(800, 300); click.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ displayArea.append("\nAction Command is "+e.getActionCommand()); /* Get the Time Stamp when the Action Occurs */ long timeStamp= e.getWhen(); Date day = new Date(Long.parseLong(""+timeStamp)); displayArea.append("\n time is " + day); /* Get the Modifiers */ displayArea.append("\n Modifiers are "+e.getModifiers()); displayArea.append("\n Entire param String is"+e.paramString()+"\n"); } }); submit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ displayArea.append("\nAction Command is "+e.getActionCommand()); /* Get the Time Stamp when the Action Occurs */ long timeStamp= e.getWhen(); Date day = new Date(Long.parseLong(""+timeStamp)); displayArea.append("\n time is " + day); /* Get the Modifiers */ displayArea.append("\n Modifiers are "+e.getModifiers()); displayArea.append("\n Entire param String is"+e.paramString()+"\n"); } }); } public static void main(String args[]){ new ActionEventEx(); } }
Output
Click
the buttons, normally and using the combinations of keys ctrl, shift,
alt and check the output.
Note
To
invoke an ActionEvent on a Button using the keyboard, use the Space
bar.
No comments:
Post a Comment