java - Execute JButton when ENTER key is pressed in panel - Swing -
i have login jbutton
on panel , need execute when press enter key.
do have code snippet that?
you can use inputmap
, actionmap
this.
import javax.swing.*; import java.awt.*; import java.awt.event.*; class ac1 { public static void main(string args[]) { jframe f=new jframe(); f.setvisible(true); f.setsize(400,400); f.setlayout(new flowlayout()); final jbutton b=new jbutton("button"); f.add(b); f.getrootpane().getinputmap(jcomponent.when_in_focused_windo w).put(keystroke.getkeystroke(keyevent.vk_enter,0),"clickbutton"); f.getrootpane().getactionmap().put("clickbutton",new abstractaction(){ public void actionperformed(actionevent ae) { b.doclick(); system.out.println("button clicked"); } }); } }
Comments
Post a Comment