I'm trying to display another image with 1 button and go back to previous image with same button.
private void button2ActionPerformed(java.awt.event.ActionEvent evt)
{
if (labelIcon == labelIcon)
centerLabel.setIcon(labelicon2);
if (labelIcon == labelicon2)
centerLabel.setIcon(labelIcon);
}
I'm stuck with if changing only to another image and not going back. Do I need to get label properties somehow (don't know how) to execute second if statement or i need some kind of loop?
I can do it with 2 buttons but I think it can be done with 1. Am I wrong?
Add an int that keeps track of which image you are on, then if it's on 1, you change to 2, if it's on 2, you change to 1.
i.e.
int imageNumber = 1;
...
if (imageNumber == 1)
{
//change image to image 2
//also change imageNumber to 2
}
else if (imageNumber == 2)
{
//change image to image 1
//also change imageNumber to 1
}
if (labelIcon == labelIcon)
this code will always return TRUE becouse you are comparing the same object.
I think you ment something like
Icon labelIcon = centerLabel.getIcon();
if (labelIcon == this.labelIcon)
centerLabel.setIcon (labelicon2);
if (labelIcon == this.labelicon2)
centerLabel.setIcon(labelIcon);
}
Since the previous three answers all have their faults, here's mine. Use a boolean to check if the button has been clicked.
private boolean clicked = false;
private void button2ActionPerformed(ActionEvent evt) { //importing stuff is always nice...
if (!clicked) {
centerLabel.setIcon (labelicon2);
clicked = true;
} else {
centerLabel.setIcon(labelIcon);
clicked = false;
}
}
Related
I need the same JButton to perform a different actions once it's clicked again. Like the first time I click the button, a text will appear in the first row of my JTextField, then the second time I click it, a text will appear in the second row if text field. How should I do it?
Here is my code BTW:
private void addActionPerformed(java.awt.event.ActionEvent evt) {
String items1 =(String)list.getSelectedItem();
String qty1 = qty.getText();
String price1 = price.getText();
int qty2 = Integer.parseInt(qty1);
int price2 = Integer.parseInt(price1);
if(evt.getSource() == add){
order1.setText(Integer.toString(qty2));
order2.setText(Integer.toString(price2));
order3.setText(items1);
}
I literally have no idea what to do next.
Here is the pic for the design GUI: http://prntscr.com/pfh96z
Take a Boolean isClickedOnce and change its state upon clicking on your button
private Boolean isClickedOnce = false;
//..
private void addActionPerformed(java.awt.event.ActionEvent evt) {
if(!isClickedOnce) {
//first click
//..
} else {
//second click
//..
}
isClickedOnce = !isClickedOnce;
}
Note: it'll consider every odd number click as first click and every even number of click as second click. it will toggle through your first and second row.
If your case is different lets say you have n number of rows, above procedure won't work and you might wanna do something similar with a list.
I have made this application:
For example when I click on the clear button when the counter JLabel (pointsAvailable) is 19 then the counter JLabel goes blank as expected, however when I start adding points again it starts from 19 not 40 as set on the start. I would like to make it to reset back to 40 instead of just making it blank
Code for the clear button
private void JButtonActionPerformed(java.awt.event.ActionEvent evt) {
speedPoints.setText("");
attackPoints.setText("");
defencePoints.setText("");
powerPoints.setText("");
agilityPoints.setText("");
focusPoints.setText("");
availablePoints.setText("");
}
Code for Jlabel counter
public class addingPointsUI extends javax.swing.JFrame {
int pointsAvailable=40;
int speed=0;
int power=0;
int focus=0;
int agility=0;
int defence=0;
int attack=0;
Code for buttons +/-: to allow me to add or decrease value "example power - button"
private void powerMinusActionPerformed(java.awt.event.ActionEvent evt) {
if (power > 0 ){
if (pointsAvailable <= 0) {
JOptionPane.showMessageDialog(null, "You are out of available points");
return;
}
power = power - 1;
pointsAvailable = pointsAvailable +1;
availablePoints.setText(String.valueOf(pointsAvailable));
powerPoints.setText(String.valueOf(power));
}else {
JOptionPane.showMessageDialog(null,"You cannot take anymore points from Power");
}
}
Thank your for your kind replies.
Use a JSpinner with SpinnerNumberModel. Change the value of the model. The component will update and further changes will act on the current value of the model.
I can quickly think of two solutions:
1. In the event handler of your clear button include the following:
private void JButtonActionPerformed(ActionEvent evt){
...
pointsAvailable=40;
speed=0;
power=0;
focus=0;
agility=0;
defence=0;
attack=0;
}
This will reset all of your stats.
2. Or you can add an if statement to the listeners of every button that adds or subtracts a stat that will check if the specific statistic is empty. For example, for the speed buttons the code would look like so:
if (speedPoints.getText() == ""){
pointsAvailable += speed;
speed = 0;
}
Found my own solution on google:
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {
speedPoints.setText(String.valueOf(0));
powerPoints.setText(String.valueOf(0));
agilityPoints.setText(String.valueOf(0));
defencePoints.setText(String.valueOf(0));
focusPoints.setText(String.valueOf(0));
attackPoints.setText(String.valueOf(0));
availablePoints.setText(String.valueOf(40));
}
Works perfectly
I've created a number picker with some string values.
public void setValues() {
boolean defaultAvaliable = false;
int defaultRow = 0;
String[] values = new String[pickValues.size()];
for (int i = 0; i < pickValues.size(); i++) {
if (pickValues.get(i).equals("01:00")) {
defaultAvaliable = true;
defaultRow = i;
}
values[i] = pickValues.get(i);
}
timePicker.setMaxValue(values.length - 1);
timePicker.setMinValue(0);
timePicker.setWrapSelectorWheel(false);
timePicker.setDisplayedValues(values);
if (defaultAvaliable) {
timePicker.setValue(defaultRow);
}
}
And when I scroll to edge values (first or last) the picker first jumps to that item in a weird, not smooth way and after that when i try to go back to other values it doesn't scroll. The picker glitches on the edge value and after some time it finally goes back. In some way it looks like I would scroll over the edge value and it needs some scrolling to get back.
Has anyone else had this problem or does anyone have any suggestions what to check?
EDIT 1:
So I've got to he point that I know the problem is when I change the font size in my numberpicker, and don't know why. As soon as I set the font size over 39 it starts to get stuck on first and last item.
private void updateView(View view) {
if(view instanceof EditText){
((EditText) view).setTextSize(39);
((EditText) view).setTextColor(getResources().getColor(R.color.mp_white));
((EditText) view).setFocusable(false);
}
}
Does anyone know what is the row size?
Or if I have to change the size of it, so it detects the scrolling correctly?
Update: I just added a second set of buttons above with no method so the two would not interfere.
To be clear, the following is for a simon says type game, where the computer plays a pattern of buttons and the user must click them in the correct order.
I am calling programmatically a sequence of buttons as a demo to the user, who is expected to press those buttons. But as the demo buttons are pressed their methods are being called which is messing up things for the user.
How can I call just the button animations so that the demo is still presented but the onClick methods are NOT called?
Here is some relevant code:
public void clickBut(View view, int[] list, int level) {
Button redBut = (Button)findViewById(R.id.redBut);
Button blueBut = (Button)findViewById(R.id.blueBut);
Button greenBut = (Button)findViewById(R.id.greenBut);
Button yellowBut = (Button)findViewById(R.id.yellowBut);
for (int i = 0; i < level; i++) {
if (list[i] == 0) {
redBut.setPressed(false);
redBut.performClick();
redBut.setPressed(true);
redBut.invalidate();
redBut.setPressed(false);
redBut.invalidate();
}
else if (list[i] == 1) {
redBut.setPressed(false);
blueBut.callOnClick();
blueBut.setPressed(true);
blueBut.invalidate();
blueBut.setPressed(false);
blueBut.invalidate();
}
else if (list[i] == 2) {
redBut.setPressed(false);
greenBut.callOnClick();
greenBut.setPressed(true);
greenBut.invalidate();
greenBut.setPressed(false);
greenBut.invalidate();
}
else if (list[i] == 3) {
redBut.setPressed(false);
yellowBut.callOnClick();
yellowBut.setPressed(true);
yellowBut.invalidate();
yellowBut.setPressed(false);
yellowBut.invalidate();
}
}
}
Forgive me if the setPressed(boolean) lines are superfluous or erratic I'm trying out different combinations.
I need this to play and click the correct buttons without activating the methods, then open it up to the user so he/she can click buttons and have the appropriate methods called.
Let me know if any more of the code would be helpful.
Replace-
redBut.setPressed(false);
redBut.performClick();
redBut.setPressed(true);
redBut.invalidate();
redBut.setPressed(false);
redBut.invalidate();
by-
following code to appear button clicked for 1 second:-
button.performClick();
button.setPressed(true);
runnable = new Runnable() {
#Override
public void run() {
//called after 1000 millisecond/ 1 second
button.setPressed(false);
}
};
handler.postDelayed(runnable, 1000);
And If you want to show button selected then use-
button.setPressed(true);
The following should do the trick
mButton.setOnClickListener(null);
The object of the code so far is the trade turns back and forth between player 1 and player 2 and allow the player whoose turn it is to turn one of their pieces invisible (Set icon to null). It works right now, turns trade back and forth and pieces turn invisible on click, but sometimes it is not the first click. It might take 3 or 4 clicks on a correct piece before it changes to null. Is there a reason this would be happening?
Robo2 is the icon for the first players pieces, robo1 is the icon for the second players pieces. The pieces are stored in an array of JButtons in the program with the icon set as the image of player 1 or player 2 piece.
public void mouseClicked(MouseEvent me) {
JButton clicked = (JButton)me.getSource();
if (player1) {
if (clicked.getIcon() == Robo2) {
clicked.setIcon(null);
player1 = false;
player2 = true;
}
else {
}
}
else if (player2) {
if (clicked.getIcon() == Robo1) {
clicked.setIcon(null);
player1 = true;
player2 = false;
}
else {
}
}
}
Figured out a solution, changing the mouse listener to an action listener solved the missing clicks problems. Using the events sent when the button gets clicked rather than detecting clicks themselves on the button. Thanks for the help.
when you double-click (or triple-click, or quadruple-click) something in Java you get this:
1st click: MouseEvent, clickCount = 1
2nd click: MouseEvent, clickCount = 2
3rd click: MouseEvent, clickCount = 3
etc.
so imagine you're double clicking the button by player1. The first event would change the player to player 2; the second event would change it right back to player1!
To remedy this situation - check clickCount (me.getClickCount()) and ignore the event if it isn't 1. Like
if (me.getClickCount() > 1) {
return;
}
// or else proceed as you do now