Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 577
user1060899 Avatar asked Nov 23 '11 00:11

user1060899


People also ask

How do I create a JButton image?

To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon); Above, we have set icon for button 7.

How do I change the image of a JButton in Java?

you can use this code: Icon i=new ImageIcon("image. jpg"); jButton1. setIcon(i);

How do you make a JButton transparent in Java?

JButton can become transparentIf the value of the opaque property of a JButton is set to false, the background becomes transparent allowing whatever is behind the button to show through. Only the text and the border of the button remain opaque.


2 Answers

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);
like image 116
Van-Sama Avatar answered Sep 28 '22 09:09

Van-Sama


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!).

like image 22
jli Avatar answered Sep 28 '22 08:09

jli