Jump to content

.setText issue with data type, please help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
thieflock

thieflock

    Newbie

  • Members
  • PipPip
  • 29 posts
If you look at line 89, at the bottom, "tMaturity.setText(maturity);". When I try to compile I get the error message:

CDMaturity.java:89: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (double)
tMaturity.setText(maturity);

I have my data types correct and my parsing is correct, so why can't I use .setText to display in the GUI?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CDMaturity extends JFrame {

  // declare GUI components
  JLabel lAmount, lYears, lInterest;
  JTextField tAmount, tYears, tInterest, tMaturity;
  JButton bCalculate, bReset;
  Container c;
  
  // declare variables
  double amount, interest, maturity;
  int years;
  
  // override default constructor
  public CDMaturity() {
  
    // contruct the components
	lAmount = new JLabel("Amount Deposited:");
	lYears = new JLabel("Years:");
	lInterest = new JLabel("Interest Rate:");
	tAmount = new JTextField(10);
	tYears = new JTextField(5);
	tInterest = new JTextField(5);
	tMaturity = new JTextField(10);
	bCalculate = new JButton("Calculate");
	bReset = new JButton("Reset");
	
	// add components to container
	c = getContentPane();
	c.setLayout(new GridLayout(5,2));
	c.add(lAmount);
	c.add(tAmount);
	c.add(lYears);
	c.add(tYears);
	c.add(lInterest);
	c.add(tInterest);
	c.add(bCalculate);
	c.add(tMaturity);
	c.add(bReset);
	
	// make calculate non-editable
	tMaturity.setEditable(false);
	
	// render the window object
	setSize(300,100);
	setLocation(500,500);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setTitle("CoD on Maturity Calculator");
	setResizable(false);
	setVisible(true);
	
	// create Button Handler object
	ButtonHandler buttonHandler = new ButtonHandler();
	
	// register this listener with the button
	bCalculate.addActionListener(buttonHandler);
	bReset.addActionListener(buttonHandler);
  
  }
  
    private class ButtonHandler implements ActionListener {
      public void actionPerformed (ActionEvent ae) {
	    if (ae.getSource().equals(bReset)) {
	      // clear all the text fields
		  tAmount.setText("");
		  tYears.setText("");
		  tInterest.setText("");
		  tMaturity.setText("");
		  return;
	  
	    // store entered information
	    try {
	      amount = Double.parseDouble(tAmount.getText());
	      years = Integer.parseInt(tYears.getText());
	      interest = Double.parseDouble(tInterest.getText());
	    }
	    catch (Exception e) {
	      JOptionPane.showMessageDialog(null, "Please enter numberic data.");
	    return;
	    }
	  
	    // compute and display maturity
	    maturity = amount * Math.pow((1 + interest / 100),15);
	    tMaturity.setText(maturity);
	  }
	}
	  
    public void main(String[] args) { 
      CDMaturity myApp = new CDMaturity();
    }
  }
}


#2
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
change it to
tMaturity.setText(""+maturity);
That will force the variable maturity to be wrapped in a string.

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

Quote

I have my data types correct
Yet your compiler says different? I usually trust the compiler. The problem is indeed your types are incorrect. The setText method requires a string paramater, and when you pass in the param it is set as a double

[highlight="Java"]maturity = amount * Math.pow((1 + interest / 100),15);
tMaturity.setText(maturity);[/highlight]
maturity is a double. You can either do as gszauer suggested, or you can use the Double.toStirng method:
[highlight="Java"]tMaturity.setText(Double.toString(maturity));[/highlight]

Either work, not sure if one is better.