954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

GUI Jslider gummin up my calculator...

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();
   }
}
clueless101
Junior Poster in Training
52 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

You also want to read this:
JTextField

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

@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/

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You