Javaswing button decrease value of JTextfield/JLabel - java

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?

Related

How to wait on a button for user input in order to set a variable

I am writing a class that has certain variables that don't need to be set except in certain fringe cases. As such, I am working on writing a requestVariable() method that asks for user input to set that variable when it is needed. However, I am struggling to figure out how to wait for that input before moving on. Let me show you what I have.
SkillApplication AR_D_Atk_Def_1_app = (Unit) -> {
if (Unit.getAttackStatus() != com.codecademy.myapplication.Unit.AttackStatus.DEFENDER) {
return;
}
else {
// Make sure structuresAR is set
Unit.setStructuresAR(requestVariable( /* some parameters */ );
int X;
if (Unit.getStructuresAR() >= 5) {
X = 4;
}
else if (Unit.getStructuresAR() == 4) {
X = 3;
}
else if (Unit.getStructuresAR() == 3) {
X = 2;
}
else {
X = 1;
}
Unit.addCombatAtk(X);
Unit.addCombatDef(X);
}
};
This is a lambda function for a certain skill's application. If this skill needs to be applied, it will run this lambda function. This is one of the fringe cases where the member "structuresAR" of Unit needs to be used. It's very rarely used, and rather than having the user set it every time, I have it set in this lambda function.
VariableRequest<Integer> requestInteger = (VariableRequest<Integer>) (questionMessage, choices, layout) -> {
final Integer[] retVal = new Integer[1];
TextView questionView = new TextView(layout.getContext());
questionView.setText(questionMessage);
EditText textEntry = new EditText(layout.getContext());
textEntry.setInputType(InputType.TYPE_CLASS_NUMBER);
Button submitButton = new Button(layout.getContext());
submitButton.setText("Submit");
layout.addView(questionView);
layout.addView(textEntry);
layout.addView(submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
retVal[0] = Integer.parseInt(String.valueOf(textEntry.getText()));
}
});
};
Here's what I have written so far for that function. It sets a question, options, and submit button to a layout that updates a return value with what is in the entry box when a button is clicked.
This problem is, this just keeps going. The rest of whatever I've written will be run while the onClickListener is still there, and I don't know how to wait until that's been clicked. I'm coming from C++ knowledge where just writing cin >> variable would pause and wait for you to enter. I'm trying to replicate that with a button.
There's also other problems I can spot with this such as getting the layout from inside a static method, but I struggle to come up with another method as I'm very new to Android development.

update variable every 1 second

hi i am new in java programming i have a big problem when i use timer or while loop for my program to show my variable and update it my program will stopped, not stopped working just nothing i cant do anything in my program my game is a easy game that we should got money from ... ways and i want to show my money every x second. every second isn't important for me just a code that not stop my program, here is my money code:
static int money = 0;
static int energy = 100;
static String energyinfo = Integer.toString(energy);
static String moneyinfo = Integer.toString(money);
private void setmoneyenergy()
{
energyinfo = Integer.toString(energy);
moneyinfo = Integer.toString(money);
jenergy.setText(energyinfo);
jmoney.setText(moneyinfo);
}
while is in a button actionperformed
while(true)
{
setpoolenergy();
}

How to make a clicked action operate only once

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.

How to collect only one value from a JToggleButton when a mouse moves over the container?

I am working on doing a word finder puzzle game. When I am trying to get to happen is a user clicks on a letter then moves his mouse across other letters to create a word. I am having some problems with the listeners. I have been going back and fourth using mouseDragged and mouseMoved. So far mouseMoved seems to work better because it dynamically grabs values. The problem is I can't figure out how to get it only grab one value. In an ideal world it would move of a Button or label grab that value once and ignore the value till it reaches a new button or label. Currently it just grabs values at every instant a mouse is on that container. The logic for my Mouse method is below:
public void mouseMoved(MouseEvent e) {
int count = countClicked;
int num = 0;
for(JToggleButton row : puzzleGrid){
if(e.getComponent() == row && count == 1) {
if(num == 0){
num++;
for(JLabel l: solWords)
{
sb.append(row.getText());
System.out.println(l.getText()+" = "+ sb.toString());
if(l.getText().contentEquals(row.getText()))
System.out.println(row.getText());
}
}
}
}
}
I am using the value gathered from the containers to check against an array of JLabels containing the solution values.
You could store the last letter in a static variable:
static String lastLetter = null;
mouseMoved(...) {
if(row.getText().equals(lastLetter)) {
continue;
}
lastLetter = row.getText();
}

Bypass final requirement in JavaFX

So i have this code - you press the button and then it drafts a card for a player. Currently i've only added a player name on the screen, random_nr will get the card number later, right now it's not needed.
The problem is i have a list, nimedlist, which consists of player names. I would like to go through the list several times while there are still existing cards but the final statement ruined it but i cannot do the int i = 0 in button aswell, because then every time i press the button, it will gain a value 0 again.
Is there any other way to make "i" value changeable? Loops are not an option.
final int i = 0;
//mis juhtub kui nextButtonile vajutada
nextButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(final ActionEvent event) {
final int random_nr = (int) (Math.random() * (kaardid.size()));
final Label nimi = new Label();
nimi.setText((String) nimedlist.get(i));
System.out.println(nimedlist.get(i));
nimi.setFont(Font.font("Verdana", 30));
nimi.setTextFill(Color.ORANGE);
alustaPiir.getChildren().add(nimi);
i++;
if (i == nimedlist.size()-1){
i = 0;
}
}
});
Define the variable as a field (instance variable) in a class scope and give a meaningful name:
private int currentIndex = 0;
and replace all is with this one in your code.

Categories

Resources