user2092012
user2092012

Reputation: 15

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.

Upvotes: 0

Views: 37570

Answers (7)

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

Upvotes: 0

user2173738
user2173738

Reputation:

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.

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

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

Upvotes: 6

1959bob
1959bob

Reputation: 150

Assuming age field is of type int, you could try something like:

        ageTF.setText( Integer.toString(e.getAge()) );

Upvotes: 0

Rahul Arora
Rahul Arora

Reputation: 1

I think you should write the code as

seatsTF.setText(e.getNoOfSeats().toString());

Upvotes: -1

1218985
1218985

Reputation: 8012

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

Upvotes: 0

Smit
Smit

Reputation: 4715

USe

seatsTF.setText(""+e.getNoOfSeats());

OR

seatsTF.setText(String.valueOf(e.getNoOfSeats()));

Upvotes: 1

Related Questions