java - How to fire an ActionEvent from a JPanel -


i'm trying repaint simple massage in panel firing actionevent.

i have messagepanel extends jpanel, in defined addactionlistener method , processevent method process event:

import java.awt.graphics; import javax.swing.jpanel;  import java.util.*; import java.awt.event.*;  public class messagepanel extends jpanel {     private string message = new date().tostring();     arraylist<actionlistener> actionlistenerlist;      public messagepanel(string message) {         this.message = message;     }      public void setmessage(string message){         this.message = message;     }      public void addactionlistener(actionlistener listener) {         if (actionlistenerlist == null) {             actionlistenerlist = new arraylist<>(2);         }         if (!actionlistenerlist.contains(listener)) {             actionlistenerlist.add(listener);         }     }      public void removeactionlistener(actionlistener listener) {         if (actionlistenerlist != null &&                 actionlistenerlist.contains(listener)) {             actionlistenerlist.remove(listener);         }     }      public void processevent(actionevent e) {         arraylist<actionlistener> list;          synchronized(this) {             if (actionlistenerlist == null) {                 return;             }             list = (arraylist<actionlistener>)actionlistenerlist.clone();         }          (int = 0; < list.size(); i++) {             actionlistener listener = (actionlistener)list.get(i);             listener.actionperformed(e);         }          }      @override     protected void paintcomponent(graphics g){          super.paintcomponent(g);          g.drawstring(message, 0, 0);     } } 

here's test class:

import java.awt.event.*; import javax.swing.*;  import java.util.*;  public testmessaepanel extends jframe {     messagepanel messagepanel = new messagepanel(new date().tostring());      public testmessagepanel() {         add(messagepanel);         messagepanel.setcentered(true);          messagepanel.addactionlistener(new actionlistener(){             @override             public void actionperformed(actionevent e){                 messagepanel.setmessage(new date().tostring());             }         });     }      public static void main(string[] args) {         jframe frame = new testmessagepanelwithactionevent();         frame.setsize(300, 200);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);     } } 

i'm trying have panel repaint current time every click of panel (without using other event sets) can't figure out event supposed fire , invoke processevent method. i'm not sure if need processevent, if can implement process elsewhere.

edit textbook problem (below) 

(enable messagepanel fire actionevent) messagepanel class in listing 15.7 subclass of jpanel; can fire mouseevent, keyevent, , componentevent, not actionevent. modify messagepanel class can fire actionevent when instance of messagepanel class clicked. name new class messagepanelwithactionevent. test java applet displays current time in message panel whenever message panel clicked, shown in figure 36.9.

i'm trying have panel repaint current time every click of panel (without using other event sets)

an actionlistener used events supposed trigger it, such timer or abstractbutton. should instead use mouselistener components respond mouse events.


edit assignment:

the messagepanel class in listing 15.7 subclass of jpanel; can fire mouseevent, keyevent, , componentevent, not actionevent. modify messagepanel class can fire actionevent when instance of messagepanel class clicked. name new class messagepanelwithactionevent. test java applet displays current time in message panel whenever message panel clicked, shown in figure 36.9.

  • you're going have give messagepanel mouselistener, 1 on mousepressed calls actionlistener(s).
  • in mouselistener, you're going have create actionevent object. since assignment, i'm not going show how instead suggest go actionevent api see object needs, , give try.
  • then have call actionperformed(...) actionevent object you've created on actionlisteners need called.

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -