Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


Multiplying Textboxes


this might sound like an easy one but im having trouble taking the value in a text box and then multiplying that to the value in another. a bit like a calculator.. If anybody can help by showing me some example code that i could copy and apply then that would be a great help????  thanks

have you looked at the textbox api? might have a getText()

iv been looking at what you said but i am realy stuck. iv only been doing java for a few days. i have something called jTextField1, which i want to type a number into, and am just trying to mutliply it bu 0.12...  if anyone could lend a hand it would be great  thanks  Edited by: phil028 on Dec 6, 2007 4:09 AM

Here's a demo. Enter a number into the first two boxes and click calculate.  The third text field will show you the result of the multiplication of the two values you entered: import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent;   class Calculator extends JFrame {   	private JTextField field1; 	private JTextField field2; 	private JTextField result; 	private JButton calculate;   	public Calculator() { 		field1 = new JTextField(30); 		field2 = new JTextField(30); 		result = new JTextField(30); 		result.setEditable(false);   		calculate = new JButton("Calculate"); 		calculate.addActionListener(new ActionListener() { 			public void actionPerformed(ActionEvent evt) { 				Double a = Double.parseDouble(field1.getText()); 				Double b = Double.parseDouble(field2.getText()); 				Double mult = a * b; 				result.setText(String.valueOf(mult)); 			} 		});   		this.getContentPane().setLayout(new java.awt.FlowLayout()); 		this.getContentPane().add(field1); 		this.getContentPane().add(field2); 		this.getContentPane().add(result); 		this.getContentPane().add(calculate); 		this.pack(); 		this.setSize(400, 150); 		this.setLocationRelativeTo(null); 		this.setDefaultCloseOperation(EXIT_ON_CLOSE); 		this.setTitle("Calculator Demo");   	}   	public static void main( String[] argv ) { 		SwingUtilities.invokeLater( new Runnable() { 			public void run() { 				new Calculator().setVisible(true); 			} 		}); 	} }   

thanks. i kind of see whats happening there. i had this..  String Units;         Units = jTextField1.getText();         jLabel1 = Units * 0.12;  which is probely rubbish, and the last line wont compile anyway. i.e. the value in the text field 1 is multiplied by 0.12 and displayed in jLabel1 but not sure what im doing or going wrong..  thanks though

phil028 wrote: thanks. i kind of see whats happening there. i had this..  String Units;         Units = jTextField1.getText();         jLabel1 = Units * 0.12;  which is probely rubbish, and the last line wont compile anyway. i.e. the value in the text field 1 is multiplied by 0.12 and displayed in jLabel1 but not sure what im doing or going wrong..  thanks though  You can't multiply strings.  If you look at my code, you'll see that I'm converting the string value that was entered into a double value.  Then, where you have: jLabel1 = Units * 0.12  That's wrong in many different ways.  First of all, if you want to set the text of a label, you must call "setText()".  Second of all, you're trying to multiply a string and a double and - as I said a second ago - that doesn't work.  Try this instead: String Units; Units = jTextField1.getText(); jLabel1.setText(String.valueOf(Double.parseDouble(Units) * 0.12)); 

thanks for that. i was just reading about that parse thing and was wondering if i needed to use that.  was a great help thanks


Related Links

Extracting java class to specific directory from jar
how to run this simple program ?
Updating image pressing a button
creating reports using java
Threads
Help with updating the grid(CONNECT 4)
URLReader
Opening/Anaylsing an unknown file
What is a serialized file?
Difference between J2SE 5.0 and JRE(Java Runtime Enviroment)
woot! Must read for people learning Java
Events....
greek letters in JavaHelp
Questions on ESC/JAVA2 Installation
2D objects Serialization problem
.jar file to an applet?