How to remove underlying JButton image?
Hi
I have this code to place an image over the standard JButton. But if the image is smaller or transparent, the underlying standard image is still seen. Is it possible to remove this image and just have the background of the panel showing?
[code]
public class ImageButton extends JButton {
public ImageButton(ImageIcon icon, String text) {
setSize(icon.getImage().getWidth(null),
icon.getImage().getHeight(null));
setIcon(icon);
setMargin(new Insets(0,0,0,0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(text);
setHorizontalTextPosition(this.CENTER);
}
/code]
Thanks for any help.
Chris
[722 byte] By [
fdgfda] at [2007-11-27 10:35:52]

# 1
public class ImageButton extends JButton {
public ImageButton(ImageIcon icon, String text) {
setSize(icon.getImage().getWidth(null),
icon.getImage().getHeight(null));
setIcon(icon);
setMargin(new Insets(0,0,0,0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(text);
setHorizontalTextPosition(this.CENTER);
}
fdgfda at 2007-7-28 18:37:38 >

# 2
No idea what you are talking about.
If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",
see http://homepage1.nifty.com/algafield/sscce.html,
that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the "Code Formatting Tags",
see http://forum.java.sun.com/help.jspa?sec=formatting,
# 3
When I use the code above to create a button, the image I pass is overlayed on top of the actual original JButton image. I can see it on the screen when I set my image to transparent. I want to get rid of the underlying button image.
fdgfda at 2007-7-28 18:37:38 >

# 4
A verbal description is not a SSCCE. I still have no idea what you are talking about.
# 5
OK, you have to choose your own background and button image. But the button image should be transparent, as I want the background image to show through, but it's not.
Here's the ImageButton, setting the button image.
import javax.swing.*;
import java.awt.*;
public class ImageButton extends JButton {
public ImageButton(ImageIcon icon, String text) {
setSize(icon.getImage().getWidth(null),
icon.getImage().getHeight(null));
setIcon(icon);
setMargin(new Insets(0,0,0,0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(text);
setHorizontalTextPosition(this.CENTER);
}
}
Here's the ImagePanel setting the background:
import java.awt.*;
import javax.swing.*;
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null),img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img,0,0,null);
}
}
And here is the calling code. I want the background image to be visible through my transparent button image. But it looks like a normal JButton is showing instead.
import javax.swing.*;
public class ImageTest {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
final ImageButton button = new ImageButton("images/buttonup-blank75.png");
button.setLocation(30,30);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
fdgfda at 2007-7-28 18:37:38 >

# 6
I don't have a transparent image to use, but I believe you need to make the button non-opaque so the parent components is painted but the background of the button is not painted.
setOpaque(false);
Read the Swing tutorial on Painting for more information:
http://java.sun.com/docs/books/tutorial/uiswing/painting/concepts.html
# 7
Thanks, but I already tried that. It's deifinitely actually painting a standard JButton beneath by image. I'll try and read the docs some more.
fdgfda at 2007-7-28 18:37:38 >

# 8
Weird thing is that it works fine with a JCheckBox (the background shows through) but when I change the code back to JButton, the JButton is displayed underneath my image.
fdgfda at 2007-7-28 18:37:38 >

# 9
So how are you testing this?
The code you posted doesn't even compile!
Once I fixed the problems (thanks for wasting my time) it worked fine, except you get the grey background when you click on the button. To fix this you use:
setContentAreaFilled(false);
# 10
Thanks, is your email address available on here?
Drop me an email and I'll sort you out for your time if you want.
chris.milner@gmail.com
fdgfda at 2007-7-28 18:37:38 >
