java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JButton
Swaing button let you do a lot more than AWT buttons. Some of the things you can do with the JButton class include using setToolTipText to add a tooltip to the button, using setMargin to set the inserts in the button itself, using doClick to click the button from code, adding images to the button, and adding mnemonics (Keyboard shortcuts) to the button. Of course, you can do standard AWT things with JButton too, such as using setEnabled to enable or disable the button, using addActionListener to register an action listener with the button and adding action commands to JButton objects with setActionCommand.
Constructor Summary | |
JButton() Creates a b0utton with no set text or icon. | |
JButton(Action a) Creates a button where properties are taken from the Action supplied. | |
JButton(Icon icon) Creates a button with an icon. | |
JButton(String text) Creates a button with text. | |
JButton(String text, Icon icon) Creates a button with initial text and an icon. |
Method Summary | |
protected void | configurePropertiesFromAction(Action a) Factory method which sets the AbstractButton 's properties according to values from the Action instance. |
AccessibleContext | getAccessibleContext() Gets the AccessibleContext associated with this JButton . |
String | getUIClassID() Returns a string that specifies the name of the L&F class that renders this component. |
boolean | isDefaultButton() Gets the value of the defaultButton property, which if true means that this button is the current default button for its JRootPane . |
boolean | isDefaultCapable() Gets the value of the defaultCapable property. |
protected String | paramString() Returns a string representation of this JButton . |
void | removeNotify() Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane , and if so, sets the RootPane 's default button to null to ensure the RootPane doesn't hold onto an invalid button reference. |
void | setDefaultCapable(boolean defaultCapable) Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane. |
void | updateUI() Resets the UI property to a value from the current look and feel. |
Example:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jbutton extends JApplet{
JTextField text=new JTextField(20);
JButton button=new JButton("Click Here");
public void init() {
Container frame1=getContentPane();
frame1.setLayout(new FlowLayout());
frame1.add(text);
frame1.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
text.setText("Hello from Swing");
}
});
}
}
Output:
Displaying Images in Button
It is easy to display images in buttons using the ImageIcon class, because several JButton constructors let you add icons to buttons. You can also set the alignment of the text and image using these AbstractButton methods.
setVerticalTextAlignment - Sets the vertical text alignment relative to the image.
setHorizontalAlignment - Sets the horizontal text alignment relative to the image.
setVerticalAlignment - Sets the vertical alignment of the button's contents.
setHorizontalAlignment - Sets the horizontal alignment of the button's contents.
Lets' look at an example in which i add an image from button.jpg to the button example from the previous,
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jbutton extends JApplet{
JTextField text=new JTextField(20);
JButton button=new JButton("asass", new ImageIcon("button1.jpg"));
public void init() {
Container frame1=getContentPane();
frame1.setLayout(new FlowLayout());
frame1.add(text);
frame1.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
text.setText("Hello from Swing");
}
});
}
}
Default Buttons and Mnemonics
Besides making buttons into default buttons, you can also give each button a mnemonic, which is a keyboard shortcut, much like those you see in menus. You underline one (case-insensitive) letter in a button's caption, and when the button has the focus, typing that character activates the button.
Example:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jbutton extends JApplet{
JTextField text=new JTextField(20);
JButton button1=new JButton("Click Here");
JButton button2=new JButton("Click Here 1");
public void init() {
Container frame1=getContentPane();
frame1.setLayout(new FlowLayout());
button2.setMnemonic('l');
getRootPane().setDefaultButton(button2);
frame1.add(text);
frame1.add(button1);
frame1.add(button2);
getRootPane().requestFocus();
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
text.setText("Hello from Swing");
}
});
}
}
Output: