.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?
# 2
the problem in this line.. double invoiceTotal = subtotal + discountPercent;what is the subtotal and what is discountPercent
# 3
Oops he is write it's discountTextField.setTextField(); Sorry ;)
# 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
# 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?
# 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);
}}}
# 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.
# 8
oh so would it go like this invoiceTotalTextField.setText("" + invoiceTotal); ?
# 9
> oh so would it go like this > > invoiceTotalTextField.setText("" + invoiceTotal); ?Why don't we do that more intelligently:invoiceTotalTextField.setText(Double.toString(invoiceTotal));
# 10
Try it and see!(I was close with the variable name.)
# 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);
}}}
# 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