Search code examples
javaswinguser-interfacejtextfield

Show a double using text with Windowbuilder and Java?


I'm fairly new to Java coding and I am trying to get a number from a double to display into a JSwing textbox.

The code I'm having trouble with:

txtCounter = new JTextField();
txtCounter.setText(counter); //Error here Jtextcomponent not available for Double
txtCounter.setEditable(false);
txtCounter.setToolTipText("Shows time that has passed.");
txtCounter.setBounds(10, 69, 100, 20);
frmTimer.getContentPane().add(txtCounter);
txtCounter.setColumns(10);

Is their a different statement I could use for this case?


Solution

  • You'll need to pass a String representation of counter to setText(). Either of the following lines would work:

    txtCounter.setText(String.valueOf(counter));
    

    Or:

    txtCounter.setText(counter + "");