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


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

      // Specify an action for the close button.

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

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

      // 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();
      cpanel = new JPanel();
      sliderPanel = new JPanel();

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

      // Add the panels to the content pane.

      // Pack and display the frame.
      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();

	public void calculateTax(int percentage){
		if (salesInputFld > 1){
		computedTaxFld = (salesInputFld * percentage);
		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();
8 Years
Discussion Span
Last Post by stephen84s

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:-


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.