Java have a int value using setText

后端 未结 7 986
梦如初夏
梦如初夏 2021-01-28 03:02

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:

7条回答
  •  北海茫月
    2021-01-28 03:45

    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.

提交回复
热议问题