using swing components - java

User enters a value in JFormattedText, I need to get this value and put it in class definition
private static final int x = <here must be entered variable>;
And how to put System.out.println result to JTextArea ( or maybe I should use another component?)

private static final int x = <here must be entered variable>;
No way. You can't assign a user entered value to a static final field. private static final is the Java way of declaring a system wide constant value.

ANd how to put system.outprinln result to JtextArea
See the Message Console for one way.

Related

Random number in text field

I have JUST started with Java and i just want to make a little program, a little game based on luck, where i have to guess a randomly chosen number and whenever i guess it, a window pops up giving me a message.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String haha = jTextField1.getText();
Random celka = new Random();
int ciprx = celka.nextInt(6)+1;
if (haha.contains(ciprx)){
JOptionPane.showMessageDialog(null, "BUHÄ€!");
}
}
The problem is that i cant write ciprx after .contains, since that is and integer and i cannot put that there, but i need the text field to contain the (secretly) randomly generated number in case to show me the pop-up message. Its fine, however, if i just put an "a" after the .contains, for example. How can i fix this?
You just need to convert it to a string first:
if (haha.contains(String.valueOf(ciprx)))

Printing integer to textfield area

Im having a trouble in java. Im creating a HRRN scheduling. I want to print the integer that I input into a textfield area. Please help me to solve this problem. Thankyou!
private void AWTActionPerformed(java.awt.event.ActionEvent evt) {
int firstprocess=1;
if (bt1.getText().equals("")){
double tempbt1 = Double.parseDouble(bt1.getText());
awttotalprocess = (firstprocess + (tempbt1));
AWTCLICK = 0;
jtf_awt.setText(String.valueOf(awttotalprocess+"ms"));
}
I want to print the awttotalprocess into jtf_awt.
Bracketing issue:
jtf_awt.setText(String.valueOf(awttotalprocess)+"ms");
Many classes come with what's called a .toString() method that prints a pre-specified output when joined with a string. You can concatenate or join a string and a variable -in this case an integer- like this:
int i = 50;
String join() {
return "I'm a string, next is a number: " + 50;
}
Keep in mind that int and Integer are different in that the first is a primitive data type, and the second is the object. This isn't an issue for you in this code but in the future if you try to concatenate a string with an object it may end up printing out the memory address as written in the .toString() default method and would require you to #override the method to specify your own string output. The primitive data types are "easier" to combine and don't require such .toString() overriding or .valueOf() shenanigans.

Class constant final variable

This is my code. I need wordOfTheDay and answer to stay the same. I need a user to input an answer for "What is the word of the day" and "What is the answer to 3*8" and depending on their answer it will either be accepted as the correct answer or rejected and they try again.
I keep getting this compiler error
Error: cannot assign a value to final variable wordOfTheDay
Error: cannot assign a value to final variable answer
//The word of the day is Kitten
import java.util.Scanner;
public class SchmeisserKLE41 {
public static final Scanner input = new Scanner(System.in);
public static final String wordOfTheDay = "Kitten";
public static final int answer = 24;
public static void main(String[] args) {
int attempts = 3;
System.out.printf("Please enter the word of the day:");
wordOfTheDay = input.nextLine();
do{
-- attempts;
if(attempts == 0){
System.out.printf("Sorry! You've exhausted all your attempts!");
break;
}
System.out.printf("Invalid! Try again %d attempt(s) left.", attempts);
wordOfTheDay = input.nextLine();
}
while(!wordOfTheDay.equals("Kitten"));
System.out.printf("\nWhat is the answer to 3 * 8?");
answer = input.nextInt();
System.exit(0);
}
}
You need two different variables. One to store the word of the day, and the other to store the user's guess. So you'll need to have two different names for them. Maybe wordOfTheDay and usersGuess. Then you can compare them after the user guesses, by changing the condition at the end of your loop to while(!wordOfTheDay.equals(usersGuess));
wordOfTheDay = input.nextLine(); You've already set wordOfTheDay. it's final, so you can only set it once....
public static final String wordOfTheDay = "Kitten";
since wordOfTheDay declared as final, it can not be assigned any value after this.
all final variables can not be assigned a value more than once.
so remove final from it as below.
public static String wordOfTheDay = "Kitten";
now you can assign value any number of times.

Java have a int value using setText

I'm trying to set an int value using jTextField and the setText method. But of course setText wants a String. How do I get round this? I'll give you a snippet of the code:
private void setAllTextFields(FilmSystem e){
getFilmNameTF().setText(e.getFilmName());
lectureTF.setText(e.getLecture());
ageTF.setText(e.getAge());
priceTF.setText(e.getTicketCost());
seatsTF.setText(e.getNoOfSeats());
seatsTF is a jTextField and getNoOfSeats is a method in another class that returns a int value.
Thanks again for answering this question. Now how would I go about getting the value of the int to do something to do?
public void buyTicket() {
String newFilmName = filmNameTF.getText();
String newLecture = lectureTF.getText();
String newAge = ageTF.getText();
String newPrice = priceTF.getText();
int newSeats = seatsTF.
As you can see the code, the String values I can get easy with getText. I can then print them out or whatever with them. How can I do this with the seats int? Thanks again.
String#valueOf convert your int to String.
String.valueOf(e.getAge()); will return the string representation of the int argument.
seatsTF.setText(String.valueOf(e.Age()));
...
USe
seatsTF.setText(""+e.getNoOfSeats());
OR
seatsTF.setText(String.valueOf(e.getNoOfSeats()));
Normal ways would be
seatsTF.setText(Integer.toString(e.getNoOfSeats()));
or
seatsTF.setText(String.valueOf(e.getNoOfSeats()));
but, this can be achieved with a concatenation like this:
seatsTF.setText("" + e.getNoOfSeats());
Assuming age field is of type int, you could try something like:
ageTF.setText( Integer.toString(e.getAge()) );
Setting an int converting it to a String not a big deal. Displaying a value is a problem. To take care of how the value is displayed properly in the textfield you may use a DecimalFormat to format the numeric value. But may be the number is locale specific then you need NumberFormat instance
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setMaximumIntegerDigits(12);
nf.setMaximumFractionDigits(0);
nf.setMinimumFractionDigits(0);
String s = nf.format(e.getNoOfSeats());
seatsTF.setText(s);
You may also need to read the tutorial on how to use the DecimalFormat.
To convert Integer Value to String you should
MedicineTM medicine=tblmedicine.getSelectionModel().getSelectedItem();
txtmedicine.setText(medicine.getMID());
txtDescription.setText(medicine.getDescription());
txtQty.setText(String.valueOf(medicine.getQty())); // this is what i did
cmbApproval.setValue(medicine.getApproval());
I think you should write the code as
seatsTF.setText(e.getNoOfSeats().toString());

Java - Set a "Int" in a textfield

I'd like to set a int in a textfield to represent a ID. This int will be incremented when the user clicks the button Next. I'm using awt. I tried to do this but it gives a error because it expects a string. :( Is there a solution?
Thanks
Sounds like you will need to keep an internal int variable which you can increment, then update the textfield with the String representation when it's changed. Similarly make sure to update the int if the user manually edits the textfield.
How about public static String String#valueOf(int)
One can convert an int to a String with either
int i = 100;
String s2 = String.valueOf(i);
String s1 = "" + i;
I believe that valueof() is the preferred approach because it can re-use static data for the value.

Categories

Resources