Friday, 1 August 2014

JAVA - CUSTOM BUTTON

Coding a custom button with a custom image which  shifts between background images on roll over hover



Main class -
import javax.swing.JFrame;
 public class Main {
public static void main (String args[]) {

butotn guiB = new button();
guiB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set size  and visibility of window
guiB.setSize(300, 100  );
guiB.setVisible(true);

}
}

Custom button class

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class button extends JFrame {

//regular biutton
private JButton reg ; 
 
// a custom button
private JButton custom ;

// constructor  -  whenever the object of this class is created in heap , the window pops up
// without any need of calling any other function
public button (){
//set the title of window  on title bar
//the string argument would be passed to super class constructo i.e JFrame
super("sleepy day");

//JFrame.setLayout()
setLayout(new FlowLayout());

// initialise and add button reference and add button to window
reg =  new JButton("regular button") ; 
add(reg);

//set the icons for custom button , one each for button background and for roll over
//put the custom pngs in the same folder as of yours workplace is 
Icon bckgrnd =  new ImageIcon(getClass().getResource("Capture.PNG")); 
Icon rollOver =  new ImageIcon(getClass().getResource("Untitled.png"));

//initialise and set background for custom button
custom = new JButton("custom" , bckgrnd);
 
//set rollover image , displayed on hovering over the custom button
custom.setRolloverIcon(rollOver);
add(custom);
 
//initialise object of action event handling class for items in jframe
// handle object is registered to component using addActionListener for each item
// such that when action event is fired ffor a particualr item
// the object's , here handle , actionPerformed() is called 
Handler handle = new Handler ();
 
//add handler to item's actionListener
//its always like this >  item  -> actionListener component - > handler implementing action listener
reg.addActionListener(handle);
reg.addActionListener(handle);
}


/* @class inside class , we can do it because we just did it
* ActionListener is listener interface for recieving action events on items
* Ours Handler class is interested in handling action events , kinda bf-gf relation oooppssss,
* so it implements the methods possesed by ActionListener because his gf , the Button class ,
* likes it when Handler handle her's all  event handling stuff
*/
private class Handler  implements ActionListener

{

//override the implemented methods from ActionListener class
// lucky , that theres just one method to implement for this class :P
@Override
public void actionPerformed(ActionEvent event) {
//show the message in new window
JOptionPane.showMessageDialog(null,String.format("%s", event.getActionCommand()));

}

}


}


No comments:

Post a Comment