How to create a custom JButton in java with an image base?

19,815

Solution 1

I asked this question earlier as well. The solution I found worked best was actually doing this, instead of drawing.

ImageIcon icon = new ImageIcon("pathOfImageHere.png");
JButton button = new JButton(icon);

So that sets the button to the image. Now what I chose to do was make the button invisible and remove all the borders. So I did this next:

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setFocusPainted(false);

Solution 2

For one, you should use ImageIO.read(new File("somefile.png")) to load an Image. Note that if there is no absolute path specified, it default to relative from the working directory. If you're running out of eclipse, it's the project folder. Out of a jar, it's the folder the jar is in (unless otherwise specified).

See http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html for an explanation of how to load an image correctly (also says how to do it from within an applet).

Also, you should load the image once, then reuse it for each paint iteration:

BufferedImage image;

public nextButton() {
    try {
        image = ImageIO.read(new File("nextButton.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null);
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(image.getWidth(), image.getHeight());
}

Let me know if this works for you (make sure to put your png in the working directory!).

Solution 3

Why don't you just use the JButton constructor that takes an Image?

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

Share:
19,815
Admin
Author by

Admin

Updated on June 18, 2022

Comments

  • Admin
    Admin almost 2 years

    I recently read this thread (Creating a custom button in Java) on creating custom buttons in java by extending the JButton class, however all the solutions on this thread use graphics drawn in java.

    I wanted to have my button based on a button image I had drawn in photoshop. So I tried to apply what I read in that thread with this result:

    import javax.swing.*;
    import java.awt.*;
    
    public class nextButton extends JButton {
        @Override
            protected void paintComponent(Graphics g) {
            Image image = new ImageIcon("nextButton.png").getImage();
            g.drawImage(image,0,0,this);
    }
    
        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            size.setSize(75, 150);
            return size;
        }
    }
    

    When I run the main program having added this button to a JPanel it doesn't display. I am assuming it could be one of several reasons:

    a) The size of the JButton doesn't match the image? b) I haven't loaded the image properly. In the notes my lecturer gave me he writes out the display image code with just "imageName.png" with no file path so I have no idea if this is the correct way to do it, or how the program will know to load the image. c) Something else which is beyond my knowledge so far.

    If anyone knows how to solve this I'd be very grateful.

    Thanks so much!