java - Catch action events from a JPanel component in a JFrame parent window -
how catch action events jpanel
component in jframe
parent window in java swing?
i'm having hard time trying make custom component in swing.
the idea got making custom jpanel in swing contains jbutton
, , catching action events on buttons in jframe parent window.
i implement method addactionlistener()
custom jpanel if button.
do have extend jcomponent
instead of jpanel
?
i appreciate , time.
package pizzeria.interfaz; import java.awt.borderlayout; import java.awt.component; import java.awt.container; import java.awt.dimension; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.box; import javax.swing.boxlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.event.ancestorevent; import javax.swing.event.ancestorlistener; public class aplicacioncliente extends jframe{ public aplicacioncliente() { container contentpane = getcontentpane(); contentpane.setlayout(new borderlayout()); panelmenu panelmenu = new panelmenu(); //panelmenu extends jpanel , has buttons jbutton bot = new jbutton("prueba"); bot.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent arg0) { system.out.println("botonazo!"); }}); contentpane.add(bot); contentpane.add(panelmenu, borderlayout.line_end); } }
why want implement addactionlistener
instead of addmouselistener
?
¿por quĂ© quieres implementar addactionlistener
en vez de addmouselistener
?
edit: check code, may works
public class aplicacioncliente extends jframe implements mouselistener{ public aplicacioncliente() { container contentpane = getcontentpane(); contentpane.setlayout(new borderlayout()); this.setvisible(true); panelmenu panelmenu = new panelmenu(); //panelmenu extends jpanel , has buttons jbutton bot = new jbutton("prueba"); bot.addmouselistener(this); bot.setname("bot"); contentpane.add(bot); contentpane.add(panelmenu, borderlayout.line_end); } @override public void mouseclicked(mouseevent e) { } @override public void mousepressed(mouseevent e) { if(((jbutton)e.getsource()).getname().comparetoignorecase("bot")==0){ system.out.println("botonazo!!"); } } @override public void mousereleased(mouseevent e) { } @override public void mouseentered(mouseevent e) { } @override public void mouseexited(mouseevent e) { }
Comments
Post a Comment