Make a button not display the icon until it is pressed - java

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

Related

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.

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

Open java Applet or Frame inside a parent Applet method and wait for input

I have a Java applet (lets call it parentApplet) with a public method which must return information regarding the status of the performed actions (let's call it getUserInput()) . This method opens another Applet which needs user button input, by adding it as a child with add(childApplet), and afterwards adding itself (the parent) as an ActionListner of the buttons in the childApplet, being able to run other methods when the user clicks on the buttons in the childApplet.
My question is, how can I halt getUserInput() execution until the user has clicked the childApplet buttons?
I tried to have a static variable that tracks the return information, and spinning on a while(var == null) Thread.Sleep(1000); but it blocks the main thread, as it should.
PS: Having the childApplet as an applet can be changed to anything that could better fulfil the requirement of opening another panel on top of the parent applet.
Details on getUserInput()
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and OK/Clear/Cancel buttons. When the user presses OK, I need to get the BufferedImage drawn. Do you know if this can be accomplished by extending a JDialog?
You really need to restructure your app. You can't do it the way you want.
Try creating a JDialog set it up as you like with input fields and an OK/Cancel button.
Then to show the dialog do:
MyDialog dialog = new MyDialog(null, true); //true = modal
//dialog.setModalityType(ModalityType.DOCUMENT_MODAL); //or specify modal here
dialog.setVisible(true); //waits until dialog is closed
if (dialog.wasAccepted()) {
//grab values from dialog
dialog.getCanvas();
}
In the dialog you would have:
private boolean accepted = false;
public boolean wasAccepted() {return accepted;}
public Canvas getCanvas() {return canvas;}
public ? getWhateverElseYouWant() {return ...;}
The OK button would:
accepted=true;
dispose();
The Cancel button would:
accepted=false;
dispose();
The JDialog will pump events while it's visible. So the setVisible() function will halt execution until the dialog is closed.
That should work better, and then you can return many user input fields.
You can even change the JDialog constructor to pass default value(s) in.
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and ok/clear/cancel buttons. When the user presses OK, I need to get the BufferedImage drawn.
First of all, it should not be an applet but a JPanel (it is not impossible to do it as an applet, but also not trivial). Then you can show the JPanel in a one of three ways.
JOptionPane.showMessageDialog(..). The idea would be to use the ready made OK/Cancel buttons of the option pane to tell the main app. whether to actually use image drawn. I (as a user) would tend to expect the Clear button to not be in the group of buttons that dismisses the dialog. Put that button in the panel you pass to the option pane.
A modal JDialog. Creating a dialog is more work than using an option pane, but also more versatile. For instance, if the panel you put into the dialog has a 'set size of drawing' option, it is easier to resize a dialog than an option pane.
Another card of a CardLayout. The two previous suggestions have the dis/advantage that they will block the app. and the browser until dismissed. This is good in that you can simply query the state of the drawing immediately after it is shown, confident that the user has finished drawing. But it is bad in that the user might have the dialog or option pane sitting on screen for 30 minutes, and the rest of the browser will be inaccessible to them in that time. By instead flipping to a card that shows the drawing panel, the browser is not blocked, and the app. can query the state of the drawing as soon as the user makes one of the OK/Cancel selections.

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!

Buttons Background Dynamically changed

I have to change the background of a JButton on some action.
I have two classes. The first one is an Action subclass and the second is a GUI class.
In the GUI class I have put buttons inside panels just like a matrix.
On some action, which is captured in the Action class, I want to inform the GUI class to change the background of a JButton.
How can I achieve this?
Say you want to change your button background to blue.
You do this:
JButton button = new JButton("Click to Change Background");
button.setBackground(Color.BLUE);

Categories