-1

I want to add images to my JButtons. I have tried to add the rollover icon command to one of my buttons, but I keep getting an exception even though eclipse doesn't say that there are any errors. I have saved the images in workspace/projectname/src where the class files are, and they are called a and b, and they are JPEG images.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

    public class button extends JFrame {

    private static final long serialVersionUID = 1L;
    private JButton hi;
    private JButton custom;

    public button() {
        super("The title");
        setLayout(new FlowLayout());

        hi = new JButton("Hi button");

        Icon a = new ImageIcon(getClass().getResource("a.JPEG"));
        Icon b = new ImageIcon(getClass().getResource("b.JPEG"));
        custom = new JButton("custom", a);
        custom.setRolloverIcon(b);
        add(custom);
        add(hi);

        HandlerClass handler = new HandlerClass();
        hi.addActionListener(handler);
        custom.addActionListener(handler);

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

import javax.swing.JFrame;
public class buttonm {
    public static void main(String[] args) {
        button hello = new button();
        hello.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        hello.setSize(350,100);
        hello.setVisible(true);
    }
}
traderjosh
  • 321
  • 5
  • 16
  • 4
    Is this a guessing game.. "Guess what my exception is"? – Paul Samsotha Aug 19 '14 at 16:46
  • I have to agree with @peeskillet. If you can give us more information with which to help you, perhaps then we can help you. Please read the [help] and [how-to-ask](http://stackoverflow.com/help/how-to-ask) sections on what information we need. – Hovercraft Full Of Eels Aug 19 '14 at 16:48
  • 1
    _"but I keep getting an exception even though eclipse doesn't say that there are any errors"_ - You may also want to do some research on exceptions vs. compile errors. Generally they have nothing to do with each other (unless you are getting an uncompilable source exception) – Paul Samsotha Aug 19 '14 at 16:52
  • 1
    If I have to _guess_ though, I'd guess a NullPointerException from invalid path. If your images are directly at the root of the src and your class is in some package below it, You want to put a `/` in the front of your path. If your image is in the same package as the java file, then your path is correct, and you really _should_ post your exception (stacktrace) for further help – Paul Samsotha Aug 19 '14 at 16:54
  • 1) Always copy/paste error or exception output. 2) See also [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Aug 20 '14 at 00:14

1 Answers1

0

the Button class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Button extends JFrame {

    private JButton button;
    private JPanel p;

    public Button() {
        super("The title");
        p = new JPanel(new BorderLayout());
        button = new JButton();

        ImageIcon icon = new ImageIcon(getClass().getResource("button2.jpg"));
        button.setIcon(icon);
        button.setDisabledIcon(icon);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 400);
        p.add(button);
        add(p);

    }
}

your main class

public static void main(String[] args) {
        Button btn = new Button();
        btn.setVisible(true);
    }
letsjak
  • 359
  • 3
  • 14