.setTextField cannot be dereferenced

double invoiceTotal = subtotal + discountPercent;

discountPercentTextField.setText("" + discountPercent);

discountTextField.setText("" + discountAmount);

invoiceTotal.setTextField("" + invoiceTotal);

then i get the error message

C:\java1.5\ch15\InvoiceApp.java:90: double cannot be dereferenced

invoiceTotal.setTextField("" + invoiceTotal);

^

1 error

Tool completed with exit code 1

what is going on?/

Message was edited by:

machman

[513 byte] By [machmana] at [2007-11-26 12:22:32]
# 1
invoiceTotal is a double (like 42.0 or something) so it doesn't have asetTextField() method - or any methods.invoiceTextField.setText() perhaps?
pbrockway2a at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 2
the problem in this line.. double invoiceTotal = subtotal + discountPercent;what is the subtotal and what is discountPercent
Ayman_javaa at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 3
Oops he is write it's discountTextField.setTextField(); Sorry ;)
Ayman_javaa at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 4
but what i dont get is i need the calculations to show up in these text panels and if i dont set the text to anything nothing shows up in the text panels and i tried to do the invoiceTotal.setTextField(); and the same error message came up
machmana at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 5

> but what i dont get is i need the calculations to

> show up in these text panels and if i dont set the

> text to anything nothing shows up in the text panels

> and i tried to do the invoiceTotal.setTextField();

> and the same error message came up

Then you should be invoking a method on the text panel not the double. In your code "invoiceTotal" is a double, not a text panel, so what exactly do you think you're doing?

kablaira at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 6

but invoice total is a text field i can post the whole program if you would like

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;

import java.util.Scanner;

public class InvoiceApp

{

public static void main (String[] args)

{

JFrame frame = new InvoiceFrame();

frame.setVisible(true);

}

}

class InvoiceFrame extends JFrame

{

public InvoiceFrame()

{

setTitle("Invoice Total Calculator");

setSize(267, 200);

centerWindow(this);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new InvoicePanel();

this.add(panel);

}

private void centerWindow (Window w)

{

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension d = tk.getScreenSize();

setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);

}

}

class InvoicePanel extends JPanel implements ActionListener

{

private JTextField subtotalTextField, discountPercentTextField, discountTextField, invoiceTotalTextField;

private JLabel subtotalLabel, discountPercentLabel, discountLabel, invoiceTotalLabel;

private JButton calculateButton, exitButton;

public InvoicePanel()

{

JPanel displayPanel = new JPanel();

displayPanel.setLayout (new FlowLayout (FlowLayout.CENTER));

subtotalLabel = new JLabel("Subtotal:");

displayPanel.add(subtotalLabel);

subtotalTextField = new JTextField(10);

displayPanel.add(subtotalTextField);

discountPercentLabel = new JLabel("Discount Percent:");

displayPanel.add(discountPercentLabel);

discountPercentTextField = new JTextField(10);

displayPanel.add(discountPercentTextField);

discountLabel = new JLabel("Discount:");

displayPanel.add(discountLabel);

discountTextField = new JTextField(10);

displayPanel.add(discountTextField);

invoiceTotalLabel = new JLabel("Invoice Total:");

displayPanel.add(invoiceTotalLabel);

invoiceTotalTextField = new JTextField(10);

displayPanel.add(invoiceTotalTextField);

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

calculateButton = new JButton("Calculate");

calculateButton.addActionListener(this);

buttonPanel.add(calculateButton);

exitButton = new JButton("Exit");

exitButton.addActionListener(this);

buttonPanel.add(exitButton);

this.setLayout(new BorderLayout());

this.add(displayPanel, BorderLayout.CENTER);

this.add(buttonPanel, BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent e)

{

Object source = e.getSource();

if (source == exitButton)

System.exit(0);

else if (source == calculateButton)

{

double subtotal =

Double.parseDouble(subtotalTextField.getText());

double discountPercent = 0.0;

if (subtotal >= 200)

discountPercent = .2;

else if (subtotal >= 100)

discountPercent = .1;

else

discountPercent = 0.0;

double discountAmount = subtotal * discountPercent;

double invoiceTotal = subtotal + discountPercent;

discountPercentTextField.setText("" + discountPercent);

discountTextField.setText("" + discountAmount);

invoiceTotal.setTextField("" + invoiceTotal);

}}}

machmana at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 7
> but invoice total is a text field i can post the> whole program if you would like No it's not. "invoiceTotal" is a double, "invoiceTotalTextField" is a text field.
kablaira at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 8
oh so would it go like this invoiceTotalTextField.setText("" + invoiceTotal); ?
machmana at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 9
> oh so would it go like this > > invoiceTotalTextField.setText("" + invoiceTotal); ?Why don't we do that more intelligently:invoiceTotalTextField.setText(Double.toString(invoiceTotal));
kablaira at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 10
Try it and see!(I was close with the variable name.)
pbrockway2a at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 11

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;

import java.util.Scanner;

public class InvoiceApp

{

public static void main (String[] args)

{

JFrame frame = new InvoiceFrame();

frame.setVisible(true);

}

}

class InvoiceFrame extends JFrame

{

public InvoiceFrame()

{

setTitle("Invoice Total Calculator");

setSize(267, 200);

centerWindow(this);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new InvoicePanel();

this.add(panel);

}

private void centerWindow (Window w)

{

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension d = tk.getScreenSize();

setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);

}

}

class InvoicePanel extends JPanel implements ActionListener

{

private JTextField subtotalTextField, discountPercentTextField, discountTextField, invoiceTotalTextField;

private JLabel subtotalLabel, discountPercentLabel, discountLabel, invoiceTotalLabel;

private JButton calculateButton, exitButton;

public InvoicePanel()

{

JPanel displayPanel = new JPanel();

displayPanel.setLayout (new FlowLayout (FlowLayout.CENTER));

subtotalLabel = new JLabel("Subtotal:");

displayPanel.add(subtotalLabel);

subtotalTextField = new JTextField(10);

displayPanel.add(subtotalTextField);

discountPercentLabel = new JLabel("Discount Percent:");

displayPanel.add(discountPercentLabel);

discountPercentTextField = new JTextField(10);

displayPanel.add(discountPercentTextField);

discountLabel = new JLabel("Discount:");

displayPanel.add(discountLabel);

discountTextField = new JTextField(10);

displayPanel.add(discountTextField);

invoiceTotalLabel = new JLabel("Invoice Total:");

displayPanel.add(invoiceTotalLabel);

invoiceTotalTextField = new JTextField(10);

displayPanel.add(invoiceTotalTextField);

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

calculateButton = new JButton("Calculate");

calculateButton.addActionListener(this);

buttonPanel.add(calculateButton);

exitButton = new JButton("Exit");

exitButton.addActionListener(this);

buttonPanel.add(exitButton);

this.setLayout(new BorderLayout());

this.add(displayPanel, BorderLayout.CENTER);

this.add(buttonPanel, BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent e)

{

Object source = e.getSource();

if (source == exitButton)

System.exit(0);

else if (source == calculateButton)

{

double subtotal =

Double.parseDouble(subtotalTextField.getText());

double discountPercent = 0.0;

if (subtotal >= 200)

discountPercent = .2;

else if (subtotal >= 100)

discountPercent = .1;

else

discountPercent = 0.0;

double discountAmount = subtotal * discountPercent;

double invoiceTotal = subtotal + discountPercent;

discountPercentTextField.setText("" + discountPercent);

discountTextField.setText("" + discountAmount);

////////////invoiceTotal.setTextField("" + invoiceTotal);// instead of this line u go for next line... here the problem

invoiceTotalTextField.setText("" + invoiceTotal);

}}}

drvijayy2k2a at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...
# 12
well it works thanks for the help now im off to figure out the math part because it isnt showing up in the panel right
machmana at 2007-7-7 15:16:00 > top of Java-index,Archived Forums,Socket Programming...