Set Swing Action Name in Java
Description
The following code shows how to set Swing Action Name.
Example
/*from w ww. j a v a 2 s .co m*/
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Loading/Saving Example");
Container content = frame.getContentPane();
Action loadAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("load");
}
};
loadAction.putValue(Action.NAME, "Load");
JButton loadButton = new JButton(loadAction);
Action clearAction = new AbstractAction() {
{
putValue(Action.NAME, "Clear");
}
public void actionPerformed(ActionEvent e) {
System.out.println("clear");
}
};
JButton clearButton = new JButton(clearAction);
frame.add(loadButton,"North");
frame.add(clearButton);
frame.setSize(250, 150);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »