Tuesday, February 24, 2009

JButton

JButton

Jbutton is the standrad command button used in Java.It is mainly used for event driven programming.A JButton when clicked fires an event which is handled by an event handler which in response do some work as indicated in hadler.On click the JButton takes some set of input and process that.The JButton is a Swing Component.


JButton Class has following constructors:-

JButton() // Creates a button with no text or icon

JButton(Action a) //Creates a button where are properties are tak
en from action supplied

JButton(Icon icon)//Creates a button with a icon

JButton(String text)//Creates a button with some text

JButton(String text ,Icon icon)//Creates button with both text and icon

Features of JButton:-

1.JButton applies event handling.
2.JButton has a different look and feel for enabled and disabled buttons.
3.You can set mnemonics for JButtons.
4.TooltipText can also be set for JButtons.

Following example shows the application of all above mentioned features:-
In this example there are buttons which on click disable and enable the corresponding buttons.
The program follows:-

import javax.swing.*;
import java.awt.event.*;
public class ButtonDemo extends JPanel
implements ActionListener {
protected JButton but1, but2, but3;

public ButtonDemo() {
ImageIcon leftButtonIcon = createImageIcon("d:/image/left.gif");
ImageIcon middleButtonIcon = createImageIcon("c:/image/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("c:/image/right.gif");

but1 = new JButton("Disable middle button", leftButtonIcon);
but1.setActionCommand("disable");
//setting up mnemonics
but1.setMnemonic(KeyEvent.VK_D);
but2 = new JButton("Middle button", middleButtonIcon);
but2.setMnemonic(KeyEvent.VK_M);

but3 = new JButton("Enable middle button", rightButtonIcon);
//Use the default text position of CENTER, TRAILING (RIGHT).
but3.setMnemonic(KeyEvent.VK_E);
but3.setActionCommand("enable");
//enabling the command button
but3.setEnabled(false);

//Listeners for actions on buttons 1 and 3.
but1.addActionListener(this);
but3.addActionListener(this);

but1.setToolTipText("Click this button to disable the middle button.");
but3.setToolTipText("Click this button to enable the middle button.");

//Add Components to this container, using the default FlowLayout.
add(but1);
add(but2);
add(but3);
}

public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
but2.setEnabled(false);
but1.setEnabled(false);
but3.setEnabled(true);
} else {
but2.setEnabled(true);
but1.setEnabled(true);
but3.setEnabled(false);
}
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ButtonDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

// Create the GUI.
private static void createAndShowGUI() {

//Create and set up the JFrame.
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
ButtonDemo newContentPane = new ButtonDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the JFrame.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP