Moving an image icon within a JToggleButton? - java

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!

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);

How to set transparent icon background in JButton "unclickable"

I created a JButton in java and I set a png logo icon to it. The icon is round with transparent background and I want that the button is activated only when I click on the image and not on the transparent background. How can I do it?

Responsive size of a JButton Icon

I am using the drag and drop system of netbeans to create my GUI.
I created a button and added an icon to it, my problem is that the image is to big for the size I want the button to be.
Is there any way of making this icon having the same size as the button?
Is there any way of making this icon having the same size as the button?
The size of the button is determined by the size of the Icon.
You can create a new Image by using the Image.getScaledInstance(...) method and then create an new ImageIcon from the scaled image.
Depending on your exact requirement and usage of layout managers, you might be able to use the Stretch Icon which will adjust the size of the image as the size of the button is changed.

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);

How to Remove an icon on a JButton?

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

Categories