user5928703 user5928703 - 2 years ago 103
Java Question

Need help calling a class in a JButton in java

This is a program that is part of a larger overall program. Just running a few test lines of codes just to make sure something works before I get 100 lines down. Kinda of new to programming. What I am trying to do is when the

in my swing program is pressed it does a variety of things, one is calling a class that takes the text from a
and outputs it to a
. The problem I am getting an error saying symbol cannot be found. Here the part of the code that is relevant to my question. I am trying to get (
Name test1 = new Name();
) to run.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame frame1 = new JFrame("Test");

JLabel label1 = new JLabel("Test run, just checking");

JPanel panel1 = new JPanel();

Name test1 = new Name();


public String Name()

String g = jTextField3.getText();

return g;


Answer Source

The line , public String Name() will define a method in Java and not a class. You are using new for a method name , Name test1 = new Name(); which is invalid, new is used to create an instance object of class.

You need to directly call method without new and type of test1 would be a String not Name, String test1 = Name();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download