Thursday, July 8, 2010

JButton  

0 comments


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();
}
});
}
}

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



0 comments: to “ JButton

Extension Factory Builder