1

So I have a GUI class which contains the JButtons, and then I have an actionPerformed class below which shows what will happen if I press the button. Now, when I do the if(e.getSource()==search) the search comes up red and says the variable cannot be found in the class GUI. Even though it's in that very class.

I need some help with this.

package crimedata;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;


class GUI extends JFrame implements ActionListener {

    Connection con = null;

    GUI() {
        JPanel p = new JPanel();
        JFrame f = new JFrame("Crime Data");

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600, 600);

        f.setResizable(true);
        f.setLocationRelativeTo(null);
        GridBagConstraints gbc = new GridBagConstraints();
        getContentPane().setLayout(new GridBagLayout());

        gbc.insets = new Insets(5, 5, 5, 5);

        JLabel LongLabel = new JLabel("Enter Longitude here");
        LongLabel.setForeground(Color.gray);
        gbc.gridx = 0;
        gbc.gridy = 1;
        getContentPane().add(LongLabel, gbc);
        JTextField LongText = new JTextField(20);
        getContentPane().add(LongText, gbc);

        JLabel LatLabel = new JLabel("Enter Latitude here");
        LatLabel.setForeground(Color.gray);
        gbc.gridx = 0;
        gbc.gridy = 2;
        getContentPane().add(LatLabel, gbc);
        JTextField LatText = new JTextField(20);
        getContentPane().add(LatText, gbc);

        JLabel LSOANameLabel = new JLabel("Enter LSOA Name here");
        LSOANameLabel.setForeground(Color.gray);
        gbc.gridx = 0;
        gbc.gridy = 3;
        getContentPane().add(LSOANameLabel, gbc);
        JTextField LSOANameText = new JTextField(20);
        getContentPane().add(LSOANameText, gbc);

        JButton search = new JButton("Search");
        gbc.gridx = 0;
        gbc.gridy = -1;
        getContentPane().add(search, gbc);
        search.addActionListener(this);

        JButton exportnoid = new JButton("Export No Crime ID");
        gbc.gridx = 0;
        gbc.gridy = -1;
        getContentPane().add(exportnoid, gbc);

        JButton exportdup = new JButton("Export Duplicate ID");
        gbc.gridx = 0;
        gbc.gridy = -1;
        getContentPane().add(exportdup, gbc);

        f.add(getContentPane());
        pack();
        f.setVisible(true);
    }

   @Override
   public void actionPerformed(ActionEvent e) {
      if(e.getSource()==search){
          System.out.println("");
      } else {

      }
   }

}

class GUIHandler {
    public static void main(String[] args) {
        GUI a = new GUI();
    }
}
1
  • 2
    Looks like search is local to the constructor, so is not a member of the class. Please format the code.
    – Andrew S
    May 7, 2018 at 14:10

3 Answers 3

1

search variable is not an instance variable, its scope is limited to the constructor, so you cannot use it outside of it.

Declare JButton search variable after Connection con = null;.

Also note that have empty 'else' statement in actionPerformed method.

5
  • Done this. > private JButton search; Now I don't get a compiler error, so I think this works.
    – AadilF1
    May 7, 2018 at 14:25
  • 1
    @AadilF1 cool, your code is a bit sloppy though and I understand that you are just starting with Java. I suggest you to read the Clean Code by Robert C. Martin and Refactoring by Martin Fowler that will improve your code structuring skills a lot!
    – xagaffar
    May 7, 2018 at 14:31
  • Okay I've just ran it with a simple sout ("Hello") command, and it doesn't seem to work :/
    – AadilF1
    May 7, 2018 at 14:47
  • @AadilF1 did you remove the declaration of search in constructor? You need to replace JButton search inside GUI() constructor by just search otherwise it overshadows the instance variable.
    – xagaffar
    May 7, 2018 at 15:02
  • Yep, this is what I needed, Thanks very much for your input!
    – AadilF1
    May 7, 2018 at 15:26
1

No, there is no variable search in the scope of your class GUI.

There is a local variable search in the constructor of your class GUI.

The only instance variable I see so far, is the variable con of type Connection.

You should declare search as an instance variable of GUI, just like con, if you want to use it in another method. My suggestion is, you make it private.

Alternatively you could use setActionCommand(String cmd) to set an action command name for your JButton. Then you can use getActionCommand() of your ActionEvent to retrieve the name of the action and react accordingly. But I do not like this approach, since in this case the compiler cannot help you much to detect name clashes.

5
  • Thank you very much. As a total noob to Java, can you sort of hold my hand with this, and show me what you mean by making it private? Thanks a bunch!
    – AadilF1
    May 7, 2018 at 14:12
  • Also I meant symbol, not variable :/
    – AadilF1
    May 7, 2018 at 14:15
  • 1
    @AadilF1 instead of writing Connection con or JButton search, you can write private Connection conn (or private JButton search, resp.) to limit the visibility of your variable to your class. Otherwise it would have so called "default visibility". Look here for more information: docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
    – UniversE
    May 7, 2018 at 14:17
  • Okay, should I remove the GUI() method?
    – AadilF1
    May 7, 2018 at 14:19
  • @AadilF1 GUI() is not a method. It's the constructor of your class and it is automatically invoked, when your object is initialized by new (in new GUI()). There are different opinions on whether you should initialize your Widgets in your constructor (like following the RAII Idiom which is more popular in C++), or lazily with some separate init() method. But these are things you can start to think about, when you are more experienced. First, you should read more online documentation about the Java language and object oriented programming.
    – UniversE
    May 7, 2018 at 14:33
0

class GUI extends JFrame implements ActionListener {

Connection con = null;
JButton search;

GUI() {
    JPanel p = new JPanel();
    JFrame f = new JFrame("Crime Data");

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(600, 600);

    f.setResizable(true);
    f.setLocationRelativeTo(null);
    GridBagConstraints gbc = new GridBagConstraints();
    getContentPane().setLayout(new GridBagLayout());

    gbc.insets = new Insets(5, 5, 5, 5);

    JLabel LongLabel = new JLabel("Enter Longitude here");
    LongLabel.setForeground(Color.gray);
    gbc.gridx = 0;
    gbc.gridy = 1;
    getContentPane().add(LongLabel, gbc);
    JTextField LongText = new JTextField(20);
    getContentPane().add(LongText, gbc);

    JLabel LatLabel = new JLabel("Enter Latitude here");
    LatLabel.setForeground(Color.gray);
    gbc.gridx = 0;
    gbc.gridy = 2;
    getContentPane().add(LatLabel, gbc);
    JTextField LatText = new JTextField(20);
    getContentPane().add(LatText, gbc);

    JLabel LSOANameLabel = new JLabel("Enter LSOA Name here");
    LSOANameLabel.setForeground(Color.gray);
    gbc.gridx = 0;
    gbc.gridy = 3;
    getContentPane().add(LSOANameLabel, gbc);
    JTextField LSOANameText = new JTextField(20);
    getContentPane().add(LSOANameText, gbc);
    search = new JButton("Search");
    gbc.gridx = 0;
    gbc.gridy = -1;
    getContentPane().add(search, gbc);
    search.addActionListener(this);

    JButton exportnoid = new JButton("Export No Crime ID");
    gbc.gridx = 0;
    gbc.gridy = -1;
    getContentPane().add(exportnoid, gbc);

    JButton exportdup = new JButton("Export Duplicate ID");
    gbc.gridx = 0;
    gbc.gridy = -1;
    getContentPane().add(exportdup, gbc);

    f.add(getContentPane());
    pack();
    f.setVisible(true);
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.