i need to access the current text of a JTextField from another method but i get an error here is the code

public Calc(){
		JTextField textBox1, textBox2;
		setLayout(new FlowLayout());
		textBox1 = new JTextField(" Time ");
		textBox2 = new JTextField("  Exp  ");
		JLabel lRate = new JLabel("Rate");
        JButton calc = new JButton("Calculate");
        textBox1.setSize(100, 10);
        textBox2.setSize(100, 10);
        calc.setToolTipText("Click to Calculate");
        calc.setActionCommand("disable");
        calc.setMnemonic(KeyEvent.VK_D);
        calc.addActionListener(this);
        add(textBox1);
        add(textBox2);
        add(calc);
        add(lRate);
	}
	
	public void actionPerformed(ActionEvent e) {
		if ("disable".equals(e.getActionCommand())) {
			String rate = calcu(textBox1.getText(), textBox2.getText());
			lRate.setText(rate);
			try{
				write(rate);
			} catch(IOException e1){System.err.println(e1);}
		}
	}

Recommended Answers

All 9 Replies

no idea just

String rate = calcu(textBox1.getText() + " " +  textBox2.getText());

no i didnt include that part of the code but calcu is a method which takes 2 strings

no idea just

String rate = calcu(textBox1.getText() + " " +  textBox2.getText());

String rate is a new String, I can't info that we can pass string with two params....

public String calcu(String s, String s1){
		float si = toFloat(s);
		float si2 = toFloat(s1);
		float fin = si / si2;
		String fi = convert(fin);
		return fi;
	}

no again, check my post from link and change your JTextField to JFormattedtextField, is about number format

how long I don't see funcion toFloat and convert, but I hoping that is void Names, not methods because that not exist in Java programing language, no idea in this case

anyway if you want hepls from ..., then no in this form, you have to expain that's, not by using some words, by posting some runnable code, not as you with empty code snipped, my magic ball is today sooo tired

You wouldn't normally have both the declaration and initialization of TextFields etc in the same method. Most of the time, you would declare all your forms controls etc as global variables. Then call some initialise method from your class constructor. The way you did it, your JTextFields are local to that method.

public class Class1{
private JTextField textBox1, textBox2;

public Class1()
{
initComponents();
}

private void initComponents()
{
  textBox1 = new JTextField(" Time ");
  textBox2 = new JTextField("  Exp  ");
  textBox1.setSize(100, 10);
  textBox2.setSize(100, 10);
  add(textBox1);
  add(textBox2);

}

}

no again, check my post from link and change your JTextField to JFormattedtextField, is about number format

how long I don't see funcion toFloat and convert, but I hoping that is void Names, not methods because that not exist in Java programing language, no idea in this case

anyway if you want hepls from ..., then no in this form, you have to expain that's, not by using some words, by posting some runnable code, not as you with empty code snipped, my magic ball is today sooo tired

Look more closely here:

String rate = calcu(textBox1.getText(), textBox2.getText());

calcu() is a method which returns a String!

thanks so much thats that solved my problem

You wouldn't normally have both the declaration and initialization of TextFields etc in the same method. Most of the time, you would declare all your forms controls etc as global variables. Then call some initialise method from your class constructor. The way you did it, your JTextFields are local to that method.

public class Class1{
private JTextField textBox1, textBox2;

public Class1()
{
initComponents();
}

private void initComponents()
{
  textBox1 = new JTextField(" Time ");
  textBox2 = new JTextField("  Exp  ");
  textBox1.setSize(100, 10);
  textBox2.setSize(100, 10);
  add(textBox1);
  add(textBox2);

}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.