8 Replies - 1993 Views - Last Post: 17 March 2009 - 07:59 AM Rate Topic: -----

#1 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Problem displaying double values in a GUI

Posted 12 March 2009 - 08:11 AM

Hello,
I'm having a problem with my SalesTax program. The problem is my array of doubles won't show up in the frame "amount". The doubles show up in the console view in eclipse but i'm trying to get the doubles such as (Enter Item, Next Item) to show up in the frame "amount". In the constructor for "amount" i haven't instantiated the doubles because so far i have only learned to instantiate things like textfields, textareas, labels, & buttons.

I would like to for these doubles to display in the amount frame similar to the console view in eclipse.
Thanks in advance

import static java.lang.Math.*;
import java.awt.GridLayout;
import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.*;
public class SalesTax extends JFrame {
	
	public SalesTax(){
		super("Amount");
		setSize(300,300);
		setLocation(100,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBackground(java.awt.Color.lightGray);
		
		setLayout(new GridLayout(12,12));
		setVisible(true);
		//Create a Scanner
		Scanner input = new Scanner(System.in);

		// Enter Your Item Value
		System.out.print("Enter Item: ");
		double retail = input.nextDouble();

		//Enter 2nd Item Value
		System.out.print("Next Item:");
		double retail2 = input.nextDouble();

		//Add both items
		double total = (retail + retail2);

		// Calculate Tax on Both Items
		double tax = (total)  * .0725;

		//Total Amount Due
		double amount = (total + tax);

		// Display result
		System.out.printf("Your total is $%.2f", total, Math.ceil(total));
		System.out.println();
		System.out.printf("Your tax is $%.2f", tax, Math.ceil(tax));
		System.out.println();

		System.out.printf("Total due is $%.2f", amount, Math.ceil(amount));

		
		setVisible(true);
	}

	
public static void main(String[] args) {
	SalesTax test = new SalesTax();

	


}
}


This post has been edited by pbl: 12 March 2009 - 04:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem displaying double values in a GUI

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Problem displaying double values in a GUI

Posted 12 March 2009 - 09:46 AM

Your problem is that you are displaying the output via the System.out.print() and printf() statements, and these statements are only able to display output in console and NOT in Frames, one way to display output in a frame is to create a JLabel in your JFrame and then set the text of your JLabel as your output, here is a simple example:
after you instantiate your JLabel you could set the text using the setText() method:
Output.seText("Your total is $%.2f", total, Math.ceil(total)); // were output is the name of your JLabel


here are links for tutorial about JLabel:
http://www.apl.jhu.e...ial-JLabel.html
http://www.java2s.co...060__JLabel.htm
http://java.sun.com/...ents/label.html
and here is the API reference of the JLabel class:
http://java.sun.com/...ing/JLabel.html
Hope this helps and good luck ;)
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem displaying double values in a GUI

Posted 12 March 2009 - 04:45 PM

Topic name changed to be more descriptive
Was This Post Helpful? 0
  • +
  • -

#4 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Problem displaying double values in a GUI

Posted 13 March 2009 - 08:13 AM

Thank you for the help... I made some modifications to the source code. I'm still receiving an error from eclipse though. The error is on the setText statements. And the error reads:

The method setText(String) in the type JLabel is not applicable for the arguments (String, double, double)




import static java.lang.Math.*;
import java.awt.GridLayout;
import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.*;

public class SalesTax extends JFrame {
	
	private javax.swing.JLabel Output;
	private javax.swing.JLabel sum;
	private javax.swing.JLabel retail;
	private javax.swing.JLabel retail2;
	private javax.swing.JLabel tax;
	private javax.swing.JPanel dataPanel;
	
	public SalesTax(){
		super("Amount");
		setSize(300,300);
		setLocation(100,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBackground(java.awt.Color.lightGray);
		
		retail = new javax.swing.JLabel();
		retail2 = new javax.swing.JLabel();
		tax = new javax.swing.JLabel();
		sum = new javax.swing.JLabel();
		Output = new javax.swing.JLabel();
		Scanner input;
		double first = input.nextDouble();
		double next = input.nextDouble();
		double total = (first + next);
		double tax2 = (total) * .0725;
		double amount2 = (total + tax2);
		
		retail.setText("Enter Item $%.2f", first, Math.ceil(first));
		retail2.setText("Next Item $%.2f", next, Math.ceil(next));
		sum.setText("Subtotal is $%.2f", total, Math.ceil(total));
		tax.setText("Your tax is $%.2f", tax2, Math.ceil(tax2));
		Output.setText("Your total is $%.2f", amount2, Math.ceil(amount2));
		
		
		dataPanel.setLayout(new java.awt.GridLayout(5, 2, 1, 5));
		
		
		dataPanel.add(retail);
		dataPanel.add(retail2);
		dataPanel.add(sum);
		dataPanel.add(tax);
		dataPanel.add(Output);
			
		setVisible(true);
	}

	
public static void main(String[] args) {
	SalesTax test = new SalesTax();


}
}


Was This Post Helpful? 0
  • +
  • -

#5 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Problem displaying double values in a GUI

Posted 13 March 2009 - 08:28 AM

Quote

retail.setText("Enter Item $%.2f", first, Math.ceil(first));


You canot do setText like this, you can only provide a String as the argument, so you will have to create the String yourself, as it won't do like printf
Was This Post Helpful? 0
  • +
  • -

#6 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Problem displaying double values in a GUI

Posted 16 March 2009 - 12:07 PM

View PostBigAnt, on 13 Mar, 2009 - 07:28 AM, said:

Quote

retail.setText("Enter Item $%.2f", first, Math.ceil(first));


You canot do setText like this, you can only provide a String as the argument, so you will have to create the String yourself, as it won't do like printf


So your saying to create a string that will perform the calculations and then insert that same string into the setText statement?
Was This Post Helpful? 0
  • +
  • -

#7 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Problem displaying double values in a GUI

Posted 16 March 2009 - 12:16 PM

Yes you have to build the String to set the text

examples:
String t = "Hello "  + Math.sqrt(4.0) " me.";

retail.setText(t);

//or

retail.setText("Hello "  + Math.sqrt(4.0) " me.");


Was This Post Helpful? 1
  • +
  • -

#8 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Problem displaying double values in a GUI

Posted 17 March 2009 - 07:12 AM

View PostBigAnt, on 16 Mar, 2009 - 11:16 AM, said:

Yes you have to build the String to set the text

examples:
String t = "Hello "  + Math.sqrt(4.0) " me.";

retail.setText(t);

//or

retail.setText("Hello "  + Math.sqrt(4.0) " me.");



I made some more modifications to my code and was able to get rid of the errors that eclipse was showing. However, the frame won't show up on my screen when i run the program.
import java.util.Scanner;
import javax.swing.JFrame;

public class SalesTax extends JFrame {
	
	private javax.swing.JLabel Output;
	private javax.swing.JLabel sum;
	private javax.swing.JLabel retail;
	private javax.swing.JLabel retail2;
	private javax.swing.JLabel tax;
	private javax.swing.JPanel dataPanel;
	
	
	public SalesTax(){
		super("Amount");
		setSize(300,300);
		setLocation(100,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBackground(java.awt.Color.lightGray);
		
		retail = new javax.swing.JLabel();
		retail2 = new javax.swing.JLabel();
		tax = new javax.swing.JLabel();
		sum = new javax.swing.JLabel();
		Output = new javax.swing.JLabel();
		
		Scanner input = new Scanner(System.in);

		double first = input.nextDouble();
		double next = input.nextDouble();
		double total = (first + next);
		double tax2 = (total) * .0725;
		double amount2 = (total + tax2);
		
		String t = "Enter Item $%.2f" + Math.ceil(first);
		String n = "Next Item $%.2f" + Math.ceil(next);
		String s = "Subtotal is $%.2f"+ Math.ceil(total);
		String t2= "Your tax is $%.2f" + Math.ceil(tax2);
		String t3 = "Your total is $%.2f" +  Math.ceil(amount2);
		
		retail.setText(t);
		retail2.setText(n);
		sum.setText(s);
		tax.setText(t2);
		Output.setText(t3);
		
		
		dataPanel.setLayout(new java.awt.GridLayout(10,2,4,10));
		
		
		dataPanel.add(retail);
		dataPanel.add(retail2);
		dataPanel.add(sum);
		dataPanel.add(tax);
		dataPanel.add(Output);
			
		setVisible(true);
	}

	
public static void main(String[] args) {
	SalesTax test = new SalesTax();


}
}

Am I missing something in the code?
Was This Post Helpful? 0
  • +
  • -

#9 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Problem displaying double values in a GUI

Posted 17 March 2009 - 07:59 AM

Quote

Scanner input = new Scanner(System.in);

double first = input.nextDouble();
double next = input.nextDouble();
double total = (first + next);
double tax2 = (total) * .0725;
double amount2 = (total + tax2);


Well you prompt at the command and then do GUI, which one do you want, if both you have to type in for your program to continue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1