Programming & Scripting Tutorials

Java: JButton



Buttons are obviously very useful, today we will be creating a regular (normal) button, and a custom button with icons (that change at different button states).
Ok, so first of all we are going to create three pictures, they will be 40x40 png's (these seem to work best).
Here are the ones I created, they are very simple but put the point across well:



Now lets import these images, all you need to do is paste them in the src folder of our project. So if you set-up the java environment like we teach here you might paste the images somewhere like here:

C:\JavaProjects\JavaApplication1\src

Ok. Lets move onto some coding then, firstly lets create a new App(.java) class to handle everything to do with the application.
Now we have all of the import statements:

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

These imports are pretty self-explanatory.. Now we can create our class body, and create two JButton variables (regular and custom):

public class App extends JFrame{

    private JButton regular;
    private JButton custom;

You should know what all of this means from the previous lessons.
In our class we will now create a constructor the do everything for us, and set the basics of the form (as we have learnt to do in previous lessons):

   public App(){
        super("Our Applications Title!");
        setLayout(new FlowLayout());

setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 150);
        setLayout(new FlowLayout());

We can now create the regular button, and add it to the form:

regular = new JButton("Regular Button");
        add(regular);

We have done things similar to this before, all it does is set the button variable to a new button with the text "Regular Button".

Now we come to our custom button..
Firstly we can set our icon resources:

Icon a = new ImageIcon(getClass().getResource("a.png"));
        Icon b = new ImageIcon(getClass().getResource("b.png"));
        Icon c = new ImageIcon(getClass().getResource("c.png"));

As has been said previously this simply creates the icon resource so we can refer to it later.
Now we can set the variable to a new button:

custom = new JButton("Custom Button", a);

We have used two parameters, the first for the text on the button, and the second for our icon.
Now we can get to the more event related bit, which is the icon changing on the rollover and click of the button(note that to stop the rollover sequence if your mouse goes off-screen while the pop-up is up you must put your mouse somewhere else on the form(so the form knows where the mouse is):

custom.setRolloverIcon(b);
custom.setPressedIcon(c);

As you might have figured out this simply changes the icon when the button is in a rollover and click state.
Now we can add our button to the form!

add(custom);

Ok. So now lets add our event handling and actionlistener stuff and objects although we haven't created the class yet (that's why you will get errors at first), and of course set the form to be visible (and end the constructor curly brackets):

Handler handler = new Handler();

regular.addActionListener(handler);

custom.addActionListener(handler);

        setVisible(true);
    }

Ok! So now lets make our Handler class with our ActionPerformed function (you should understand this from the previous lessons):

private class Handler implements ActionListener{
        public void actionPerformed(ActionEvent event){
        JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
        }

    }

Now lets end the curly brackets for our class:

}

Now go back to your main file (Main.java), and simply create an object for our class:

App Object = new App();

We can now successfully run our application!

You should also now know how to use JButton and have some basic understanding of icons.





This Java tutorial was written by


Back to Java

Advertisement: