1,105,371 Community Members

value of textfield

Member Avatar
rithish
Posting Whiz in Training
281 posts since Apr 2011
Reputation Points: 13 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

consider a instance


jtextfield t=new jtextfield();
jtextfield t1=new jtextfield();
jtextfield t2=new jtextfield();
i want to get the values of textfield t and t1 and display in t2
like i want to add values of t and t1 and store in t2

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Have a look at the getText() and setText(String text) methods that JTextField inherits from JTextComponent.
ps Watch out for your capitalisation - java is case-sensitive

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

on focusLost() http://download.oracle.com/javase/tutorial/uiswing/events/focuslistener.html

Runnable doRun = new Runnable() {

   @Override
   public void run() {
     t1.setText(t.getText())
   }
 };
SwingUtilities.invokeLater(doRun);
Member Avatar
rithish
Posting Whiz in Training
281 posts since Apr 2011
Reputation Points: 13 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class calc extends JFrame 
{
	JTextField t1=new JTextField();
	JTextField t2=new JTextField();
	JTextField t3=new JTextField();
JButton	b1=new JButton("+");
  JButton b2=new JButton("-");
  JButton b3=new JButton("*");
  JButton b4=new JButton("/");
  JPanel panel=new JPanel(new GridLayout(2,2));

	calc()
	{
		
		panel.add(t1);
		panel.add(t2);
		panel.add(t3);
		panel.add(b1);
		panel.add(b2);
		panel.add(b3);
		panel.add(b4);
		add(panel,BorderLayout.CENTER);
		
	
		b1.addActionListener(new ActionListener(){
			
			public void actionperformed(ActionEvent e)
			{
			 t3.settext=t1.gettext()+t2.getText();
			 
			}
		}
			
			b1.addActionListener(new ActionListener(){
				public void actionperformed(action event e)
				{
					t3.settext=t1.gettext()-t2.getText();
				}
			}
		
		
		
		
	}
	public static void main(string args[])
	{
		calc frame=new calc();
		frame.setVisible(true);
		frame.setSize(300,100)
	}
	
}

this is my problem i want to do a simple calculator please solve me

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

good one, and what's with that

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Have another look at setText... your syntax is wrong in
t3.settext=t1.gettext()-t2.getText();

We will help you with your problem, but it's your homework, we won't do it for you. You have made a start, so keep going! If you get stuck post your latest code and the details of why you're stuck, and someone will help.

Member Avatar
rithish
Posting Whiz in Training
281 posts since Apr 2011
Reputation Points: 13 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

sir i didnt tel you to solve my problem sir my doubt is in

t3.settext=t1.gettext()-t2.getText();

i dont know what to do over there
if i do t1.gettext() how will it get value sir ????? i just need to get the value of two text fields and display in t3

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

t1.getText() will give you the current contents of the field as a String.
Next you have to convert that to a numeric value (eg int or float).
Then you can add/subtract etc the values
Then convert the result back to a String
Finally set the result field's text to that String.

To convert between Strings and numbers there are useful methods in the appropriate numeric classes, eg
Integer.parseInt(aString); // converts String to Integer
Integer.toString(anInt): // converts int to String
You should look these up yourself to learn about them properly.

Member Avatar
rithish
Posting Whiz in Training
281 posts since Apr 2011
Reputation Points: 13 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

so can i use integer.parse method will it convert string to integer??

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Yes. Have a look at its documentation for details (eg what happens if the String cannot be converted to integer)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article