How to make a clicked action operate only once - java

I just want an action to occur once, just once when clicked. Here is the code:
String previousValue = Counter.getText(" ");
Counter.setText("0");
AmouseClicked(java.awt.event.MouseEvent evt) {
If(callquestion==1 && A.isFocusable()) {
int d= Integer.parseInt(Counter.getText());
int e= 10;
int f=d+10;
Counter.setText(f+" ");
}
}
Note- Counter is a JLabel that displays the output, A is Jlabel that holds the correct answer .I have A,B,C,D jlabels.
The initial value is 0. So I want each time a button is cliked, 10 just be added ones, if u click again. The 10 remains. I have callquestion1 to 20. So after a play gets the correct answer, he clicks the next to go to the next Qus.
I just need each button to add 10 if clicked again do nothing.

Related

How to set in a textview how many times a button is being clicked?

If I click a button 1 time, so it should to show number "1" in a textview. If I click again, so its should to show "2"...
#Override
public void onClick(View p1){
int id = p1.getId();
double x = 0;
//button clicked
if(id == R.id.button_contar){
x++; /*its only shows "1". When I click again, shows "1" again*/
this.mViewHolder.contados.setText(String.format("%.0f", x));
}
}
One possible approach is to initially set 0 in the textview, and with every button click you first fetch the current value in textView and then increment the value and set the new Value
int id = p1.getId();
//button clicked
if(id == R.id.button_contar){
int current = Integer.parseInt(this.mViewHolder.contados.getText().toString());
current++;
this.mViewHolder.contados.setText(String.format("%.0f", current));
}
I don't have any editor right now, so there might be some syntax errors with the above code. It will give you a rough idea on how you should solve your issue

Javaswing button decrease value of JTextfield/JLabel

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int full = 20;
int feed = -10;
int jHooitemp = Integer.parseInt(jHooi1.getText());
jHooi1.setText(jHooitemp+ feed+ " kg");
}
I managed to decrease the pre defined value of the JTextfield(jHooi1) but it only works once.
It made the value wich i entered(20) drop to 10. but if i press the JButton another time it displays errors in the run screen.
Is there a way to make it decrease to 0 and send a message when it hits 0?

Make multiple JTextFields change color Java

I'm trying to make randomized JTextField's yellow by using Arrays. I've only managed to make 1 textfield yellow, and then white after 1 sec at each click. What I want to do is when you press the button the first randomized Textfield get yellow and then white. And when you press the second time the first randomzied textfield AND the seconds will turn yellow and then White and so on.. The array works as you can see I Printing it and when you press the button it prints all the randomized numbers in order.
The problem is that JTextField can't handle int's, and it apparently have to be "Final" so it makes it very hard to make multiply JTextField's yellow. I pasted my arrays to you so that you can get a better understanding what I'm trying to do. Does anyone know the solution?
//my Arrays
JTextField[] boxes = new JTextField[9]; //Array for the textfields
int[] clicked = new int[100];
int clickAmount = 0;
//At startup it fills the boxes array with the textfield:
boxes[0] = textfield1;
boxes[1] = textfield2;
boxes[2] = textfield3;
boxes[3] = textfield4;
boxes[4] = textfield5;
boxes[5] = textfield6;
boxes[6] = textfield7;
boxes[7] = textfield8;
boxes[8] = textfield9;
public void timePaus (final JTextField textfield) {
new Timer(1000, new ActionListener() {
public void actionPerformed (ActionEvent e) {
textfield.setBackground(Color.white);
// stop the timer
((Timer) e.getSource()).stop();
}
}).start();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //Button
int randomint = this.randomBox(); //Finds a number between 0-8
final JTextField ThatTextfield = boxes[randomint]; //Puts a textfield into an array
clicked[clickAmount] = randomint+1; //adds textfield number into an array
clickAmount++;
ThatTextfield.setBackground(Color.yellow); //make choosen textfield yellow
for (int i = 0; i < clickAmount; i++)
{
timePaus(ThatTextfield); //make choosen textfield white after 1 sec
System.out.println(clicked[i]);
}
}
Considerations:
Place your JTextFields in an ArrayList<JTextField>, but create them in a for loop for the sake of avoiding unnecessary repetation.
When you want to randomize the ArrayList, call Collections.shuffle(myTextFieldList) on your List of JTextFields.
Give your class an int maxIndex field.
In your Swing timer, iterate through the now randomized List of JTextFields, displaying each one up to the value held by the maxIndex and not above the size of the List.
Increment the maxIndex in the timer.
Alternatively, you could create two more ArrayList<JTextField>, one to shuffle the collected text fields. Then remove the 0th JTextField from this list in your button's listener, and place it into the other ArrayList, and then iterate through the 2nd ArrayList's JTextfield in your Timer.

Changing label text when button is clicked

I am trying to create a swing app that is a quiz. I need the jLabel to change on a button click but when I click the button, the app locks up. Can someone point me in the right direction?
My button click code is below:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String[] questions = {"test0","test1","test2","test3","test4","test5","test6"};
String[] answers = {"","","","","","",""};
int i = 0;
do {
jLabel2.setText(questions[i]);
index.setText(String.valueOf(i));
if (txtAnswer.getText().toLowerCase().equals(answers[i].toLowerCase())) {
i++;
jLabel2.setText(questions[i]);
}
else {
add(lblWrong);
}
}
while(i < 7);
}
I am getting a warning that the evt parameter has not been used, could this be a problem?
Thank you
You don't want the do while loop. It's trapping you in the button press method as if you get an answer wrong you keep entering the else and can't leave it, stopping the app from working. Replace it with an if statement checking if i < 7.
In the else condition of your loop, you don't add 1 to i at all - therefore you can potentially end up in the situation where it's never incremented, thus it will be an infinite loop (locking your program up.)

Java, Page through array

Test[] array = new Test[3];
array[0] = new RowBoat("Wood", "Oars", 10);
array[1] = new PowerBoat("Fiberglass", "Outboard", 35);
array[2] = new SailBoat("Composite", "Sail", 40);
I have the above array and I need to display the results to a swing GUI with a next button that will display the first index values, and when the next button is clicked it will display the next index values and so on.
for (int i=0;; i++) {
boatMaterialTextField.setText(array[i].getBoatMaterial());
boatPropulsionField.setText(array[i].getBoatPropulstion());
}
I have the above code working and of course it displays the last item in the array.
My question is: How would I display the first index in the array and when the user clicks next display the next item in the array as well as go to the previous index when a back button is clicked?
Simply put I need to page through each index when a button is clicked.
You do not need a loop. When the frame first loads you can simply display the first item in the array. You can then create a next button.
JButton nextBtn;
int currentIndex;
...
currentIndex = 0;
//display the first item in the array.
boatMaterialTextField.setText(array[currentIndex].getBoatMaterial());
boatPropulsionField.setText(array[currentIndex].getBoatPropulstion());
nextBtn = new JButton("Next>>");
nextBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(currentIndex < array.length){
boatMaterialTextField.setText(array[++currentIndex].getBoatMaterial());
boatPropulsionField.setText(array[currentIndex].getBoatPropulstion());
}
}
});
You can add another button for previous that simply decrements the currentIndex each time ensuring to check that it never becomes negative.

Categories

Resources