Hi,

I'm learning java and I have a problem. The getText() doesn't seem to work. What I am trying to do is to write a simple code, where I assign the text of a button to a string variable and print that string variable on the command line. I get the error

cannot resolve symbol
Symbol: getText()
Class : java.awt.Button

import java.io.*;
import java.awt.*;
import java.event.*;
import java.*;
import java.util.*

class tests
{
     public static void main(String[] args)
     {
        Frame f = new Frame();
        Button b1 = new Button("Text");
        f.add(b1);
        f.setSize(500,500);
        f.setVisible(true);
        String x = b1.getText();
        System.out.println(x);
     }
}

Thanks,

Member Avatar

Line #3 should be import java.awt.event.*;
You don't neet line #4.
Missing semicolon on line #5.
You also require import javax.swing.*;
Change your Button to JButton.

There is no get text method for a JButton. assign the text as a variable, and then just use that to print out, and set the text on the JButton

Thanks guys, the getText() method worked with the JButton. Thanks..