How to Remove an icon on a JButton? - java

I'm trying to make a memory matching game, and I have icon images that I place onto a JButton when it is clicked. My question is, is there a way to remove the Icon from the JButton? I want to make so when a user clicks, the image is displayed, and if the second button the user clicks does not have the same image as the first button, then it disappears...any ideas?

Simple, Set the icon to null. It doesn't have an icon, so it removes it.
button.setIcon(null);

Use the following code:
JButton button = new JButton();
button.setIcon(null);

The best way to do it, is to replace the existing icon with a transparent icon of the same size. This will ensure the button does not change size, and potentially disturb the placement of other GUI elements that occur after it in the layout. E.G.
BufferedImage ourIcon = ...
BufferedImage invisibleIcon = new BufferedImage(
ourIcon.getWidth(), ourIcon.getHeight(), BufferedImage.TYPE_INT_ARGB);
Then, simply:
// use a JToggleButton instead of a JButton - it will remain pressed
JToggleButton button = new JToggleButton(new ImageIcon(ourIcon));
button.setPressedIcon(new ImageIcon(invisibleIcon));
// start a timer to change the state back, if required..

Related

Make a button not display the icon until it is pressed

Im trying to make a simple memory game where if two buttons rather the icons on them match, the score increases. Im quite stuck on how to make the icon on the button show after it has been clicked. So far I have used:
public class NextFrame extends javax.swing.JFrame {
/**
* Creates new form NextFrame
*/
public NextFrame() {
initComponents();
btnCard1.setIcon(null);
btnCard2.setIcon(null);
btnCard3.setIcon(null);
btnCard4.setIcon(null);
btnCard5.setIcon(null);
btnCard6.setIcon(null);
//list goes on....
}
private void btnCard1ActionPerformed(java.awt.event.ActionEvent evt) {
//Not sure what goes here....
}
Using java Swing I have placed my icons within the JButtons. So only when a user clicks on it I want the selected button to display the icon. By setting the property of the button to pressedIcon only shows the icon when I press and click on the button. How do I make it so that the icon is rather fixed as I click once.
Also since my images are already there in GUI and I've only just set each button to null in the beginning, is there a method to just change from null to the actual icon.
You're overthinking things.
You know how to create an icon and set it as a button icon, yes?
Image image = new ImageIcon("C:\\some_image.png");
JButton myButton = new JButton(imageIcon);
What if you actually created two icons like so:
Image image1 = ImageIcon(getClass().getResource("real_image.png")
Image image2 = ImageIcon(getClass().getResource("blank_image.png")
When you create the button, use the blank image, which is the same size as the real image, but doesn't contain the actual image. The reason for using a blank image rather than setting the image to null is because a button without an image may actually change size once you add the image, which can potentially mess with your layout.
JButton myButton = new JButton(image2);
Now in your ActionPerformed handler which runs when the user clicks the button, you set the real image on the button:
myButton.setIcon(image1);

Updating JLabel Icon with GIF eventually stops animations [duplicate]

I've got a non-looping gif that I use as ImageIcon for two JLabels, but not at the same time.
My problem is that when I set the second JLabel's icon to be the gif, the animation has already been played, so it only show the last frame of it.
Do you know a way to get the animation when the gif is set to the second JLabel?
On the newly created icon try using:
icon.getImage().flush();
Ok,
discarded old answer. After some more searching around I found the way to do it.
ImageIcon icon = ..[the animated gif without looping]..
....
label1.setIcon(icon); //animation plays once
....
// now time to remove icon from label1 and add it to label2
label1.setIcon(null);
icon.getImage().flush(); //reset resource used by the image
label2.setIcon(icon);
....
Java Api: Image#flush()
btw. if you leave the line label1.setIcon(null); out it will still repeat the animation of the icon only for label2. label1 stays at the last frame.

How do i get rid of a blue border surrounding my button? For Java GUI

Because it wont let me add an image - new account
This is utilizing the Java GUI
The above image is an image pasted onto a button, I've tried to make the button transparent so that the user can't see it but I can't seem to get rid of this blue border.
Code I have so far
boss2 = new JButton(); //declared the static button earlier on in the code
boss2.setSize(300, 300);
boss2.setLocation(315, 200);
boss2.setIcon(new ImageIcon("dragon.gif"));
boss2.setRolloverIcon(new ImageIcon("dragon.gif"));
boss2.setOpaque(false);
boss2.setContentAreaFilled(false);
boss2.setBorder(null);
Is there a way to get rid of the blue border surrounding my image?
edit - sorry for the earlier mishap, uploaded the wrong file
I would suggest that what you are seeing is the focus rectangle, used to "highlight" the button as having keyboard focus.
You can use boss2.setFocusPainted(false); to stop it from been painted.
To not have a boarder drawn for a JButton (assuming that you are using javax.swing.JButton) you can simply do:
boss2.setBorderPainted(false);

NETBEANS - How to make radio buttons match background image

I'm using a netbeans jFrame form, and i have inserted a Background image/ THe only problem i have is that i don't know how to get the color/theme of the radio button to be the same as my background image.
Any Tips ?
You can use this:
setOpaque(false);
This will prevent the background of the JRadioButton being drawn, thus revealing whatever is in the background.
JRadioButton rdb1 = new JRadioButton("RDB1");
rdb1.setOpaque(false);
I set the first radiobutton (RDB1) with rdb1.setOpaque(false) and this is what you will get.

Moving an image icon within a JToggleButton?

Here's a picture of part of my interface. The image is circular with a transparent background but it doesn't center properly within the button. I was wondering if I could manually move it into position.
All you have to do is create a JToggleButton and then set the icon of the button to the image you are using
JToggleButton button = new JToggleButton(new ImageIcon("path to image"));
that should create the button, and put the image into the center of the button.
Hope that helps!

Categories