JButton

JButton component represents a pushing button in SWING package. On the button created by the JButton class, you can place an image and text. This button can generate an action event. There are two ways to receive the action event of the button. One way is using the ActionListener interface. The alternative is using the Action interface. By using AbstractAction class that implements the Action interface, you can only receive the event but also can specify the properties (icon and text) of the button. See the example code below.

Constructors of the JButton class:
 -JButton()
 creates a new button without image or text.
 -JButton(String text)
 creates a new button with text to describe it.
 -JButton(Action action)
 creates a new button with its properties (icon and text) taken from the Action interface implementation.
 -JButton(Icon icon)
 creates a new button with an icon image.
 -JButton(String text,Icon icon)
 creates a new button with text and an icon image.

Useful methods of the JButton class:
 -getText():String
 gets the text of the button.
 -setAction(Action action):void
 sets the Action implementation to the button.
 -setMnemonic(int key):void
 sets the mnemonic key to the button. This key when combined with the mouseless modifier key (usually Alt) will activate the button when it is focused.

 -setText(String text):void
 sets text of the button.

Examples:

By Using Code
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
class JButtonShow extends JFrame {

JButtonShow(String title) {
setTitle(title);
setSize(400, 300);
setLayout(new FlowLayout(FlowLayout.LEFT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane = getContentPane();
contpane.setBackground(Color.WHITE);
Image imgnew = Toolkit.getDefaultToolkit().getImage("FilesNew.png");
JButton btnew = new JButton();
btnew.setAction(new Action("New", new ImageIcon(imgnew)));
btnew.setMnemonic(KeyEvent.VK_N);
Image imgopen = Toolkit.getDefaultToolkit().getImage("Open.png");
JButton btopen = new JButton();
btopen.setAction(new Action("Open", new ImageIcon(imgopen)));
btopen.setMnemonic(KeyEvent.VK_O);
contpane.add(btnew);
contpane.add(btopen);
setVisible(true);
}
class Action extends AbstractAction {
Action(String text, ImageIcon icon) {
super(text, icon);
}
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
}
public class JButtonClass {
public static void main(String[] args) {
new JButtonShow("JButton");
}
}

JFrame Look Like This


JFrameWithJButtons
JFrame With JButtons

By Using GUI

Drag & drop JButtons from your Palette and set their following properties,

Drag&DropJButton_JavasGUI
Drag & Drop JButton in JFrame



  • Goto Property Window of JButton Select icon property. 

JButtonPropertyWindow_JavaGUI
JButton Property Window
  • Now select the Package where you want store .png .jpeg image and then click Import to Project for select image ,and last click OK  which you want.

SelectIconForJButton_JavaGUI
Select icon for JButton 

  • That's it ,your window look like this

JButtonWithIcon_JavaGUI
JButton With Icon

CONVERSATION

0 comments:

Post a Comment

Back
to top