I'm working on a seemingly simple application that has 2 text fields and a slider. In the 1st text field the user enters an amount and the associated sales tax is displayed in the 2nd text field. Then, by moving the slider up and down the specified range, the user adjusts the associated tax rate accordingly.

I'm having trouble accessing the fields to set and get values. I've never used a slider before and would appreciate any help getting this to work. Thx in advance!

Currently get the following 3 errors:

----jGRASP exec: javac -g C:\Program Files\SalesTaxCalc.java

SalesTaxCalc.java:91: operator > cannot be applied to javax.swing.JTextField,int
if (salesInputFld > 1){
^
SalesTaxCalc.java:92: operator * cannot be applied to javax.swing.JTextField,int
computedTaxFld = (salesInputFld * percentage);
^
SalesTaxCalc.java:94: incompatible types
found : int
required: javax.swing.JTextField
computedTaxFld = 0;
^
Here is my code:

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

public class SalesTaxCalc extends JFrame
{
   private JLabel label1, label2;     // Labels
   private JTextField salesInputFld; // Sales total
   private JTextField computedTaxFld; // Computed tax
   private JPanel spanel;         // Sales panel
   private JPanel cpanel;         // Computed tax panel
   private JPanel sliderPanel;    // Slider panel
   private JSlider slider;        // Tax adjuster

   /**
      Constructor
   */

   public SalesTaxCalc()
   {
      // Set the title.
      setTitle("Sales Tax Calculator");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create the message labels.
      label1 = new JLabel("Sales Total: ");
      label2 = new JLabel("Sales Tax: ");

      // Create the text fields.
      salesInputFld = new JTextField();
      salesInputFld.setEditable(true);
      computedTaxFld = new JTextField();
      computedTaxFld.setEditable(false);

      // Create the slider.
      slider = new JSlider(JSlider.HORIZONTAL, 0, 10, 0);  
      slider.setMajorTickSpacing(2); // Major tick every 2
      slider.setMinorTickSpacing(1);  // Minor tick every 1
      slider.setPaintTicks(true);     // Display tick marks
      slider.setPaintLabels(true);    // Display numbers
      slider.addChangeListener(new SliderListener());

      // Create panels and place the components in them.
      spanel = new JPanel();
      spanel.add(label1);
      spanel.add(salesInputFld);
		
      cpanel = new JPanel();
      cpanel.add(label2);
      cpanel.add(computedTaxFld);
		
      sliderPanel = new JPanel();
      sliderPanel.add(slider);

      // Create a GridLayout manager.
      setLayout(new GridLayout(3, 1));

      // Add the panels to the content pane.
      add(spanel);
      add(cpanel);
      add(sliderPanel);

      // Pack and display the frame.
      pack();
      setVisible(true);
   }
	
    /**
      Private inner class to handle the change events
      that are generated when the slider is moved.
   */

   private class SliderListener implements ChangeListener
   {
      public void stateChanged(ChangeEvent e)
      {
			String input;
			int tax=0;
			DecimalFormat fmt = new DecimalFormat("0.0");
			JSlider slider = (JSlider)e.getSource();
			int percentage = (int)slider.getValue();
			calculateTax(percentage);
			computedTaxFld.setText(Integer.toString(tax));
		}
	}

	public void calculateTax(int percentage){
		if (salesInputFld > 1){
		computedTaxFld = (salesInputFld * percentage);
		}else{
		computedTaxFld = 0;
	}
}

   /*
      The main method creates an instance of the
      class, which displays a window with a slider.
   */

   public static void main(String[] args)
   {
      new SalesTaxCalc();
   }
}

just read those errors. they provide you with more information than you need to solve them

You also want to read this:
JTextField

@clueless101
Why did you put what you are saying inside [QUOTE] tags. Any specific reason for it ?
I remember you doing that in some other thread too.

Also just like javaAddict, recommend you read the Javadocs more often in fact whenever you are planning to use a certain class from the java library, it always advisable to go through its Javadocs and the Javadocs of the classes related to it, that way not only will you be able to clear your concepts about the classes you are planning to use, but you might actually discover a lot more valuable information along the way which could be useful in your future adventures with Java.

The following is the link to the core Java SE 6 docs:-

http://java.sun.com/javase/6/docs/api/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.