Quote: @Morpheus: favourite, silly brittish spelling
blade2k » articles & tutorials

jump to:
Tutorials » Tech » Computers

Custom Java Button


Reading Article
Author: xtremedrummerguy
Posted on 01/16/2008
print :: back to list
A quick and easy way to create pretty custom buttons.

W

hat you need:
> 3 image files for your button
>> 1 default button ("b_up.png")
>> 1 pressed button ("b_down.png")
>> 1 disabled button ("b_disabled.png")
> Java compiler
> Text editor
> Java program ready for button

Put the image files into the folder where the program will run.

code example
01  
02  JButton button = new JButton ();
03  
04  //Makes the background disappear
05  button.setContentAreaFilled (false);
06  
07  //Makes the border disappear
08  button.setBorderPainted (false);
09  
10  //Sets the corresponding images
11  button.setIcon ("b_up.png");
12  button.setPressedIcon ("b_down.png");
13  button.setDisabledIcon ("b_disabled.png");
14  


Now take this and add an ActionListener to it and you've got a custom JButton!

If you want to make this an extended class from JButton, that'd make it easier, but don't expect the icons to change by themselves. You have to make a separate one for each button unless you make an overloaded constructor with a switch ...

If you need more help, email me at ddukki@gmail.com

~xtremedrummerguy