Home > front end >  Getting text from textfield does not work
Getting text from textfield does not work

Time:03-05

Hello I found a Projekt on yt where you can search for a keyword and it will show all the websites it found on google. And right now I am trying to revive the keyword the user put in the textfield but it isn't working. It does not find the textfield (tf1) I made, but I don't know what I did wrong. Thanks in advance!

here's my code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main implements ActionListener {

    public static void main(String[] args) {
        
        int frameWidth = 600;
        int frameHeight = 600;  
        
        JFrame f = new JFrame();
        JLabel l1 = new JLabel();
        JTextField tf1 = new JTextField();
        JButton b1 = new JButton();
        
        
        
        f.getContentPane().setBackground(Color.PINK);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setSize(frameWidth, frameHeight);
        f.setTitle("Search");
        f.setLocationRelativeTo(null);
        f.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
        
        f.add(l1);
        f.add(tf1);
        f.add(b1);

        l1.setText("Enter Keywords");
        
        tf1.setPreferredSize(new Dimension(200, 20));
        tf1.revalidate();
        
        b1.setText("Search");
        b1.addActionListener(new Main());
        
        
        f.setVisible(true);
        
//      ArrayList<WebCrawler> bots = new ArrayList<>();
        
//      bots.add(new WebCrawler("", 1));
//      bots.add(new WebCrawler("", 2));
//      bots.add(new WebCrawler("", 3));
        
//      for(WebCrawler w : bots) {
//          try {
//              w.getThread().join();
//              
//          }catch(InterruptedException e) {
//              e.printStackTrace();
//          }
//      }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
    
        String keyword = tf1.getText(); //Here it does not find the tf I made
        System.out.println(keyword);    //just for confirmation
    }
}

CodePudding user response:

You have a reference issue.

tf1 is declared as a local variable within main, which makes it inaccessible to any other method/context. Add into the fact that main is static and you run into another problem area.

The simple solution would be to make tf1 a instance field. This would be further simplified if you grouped your UI logic into a class, for example...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private JTextField textField;
        private JButton searchButton;

        public TestPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setBackground(Color.PINK);
            setLayout(new GridBagLayout());

            label = new JLabel("Enter Keywords: ");
            textField = new JTextField(20);
            searchButton = new JButton("Search"); 

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(8, 8, 8, 8);
            gbc.gridx = 0;
            gbc.gridy = 0;

            add(label, gbc);
            gbc.gridx  ;
            add(textField, gbc);

            gbc.gridy  ;
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(searchButton, gbc);

            ActionListener listener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = textField.getText();
                    JOptionPane.showMessageDialog(TestPane.this, "You want to search for: "   text);
                }
            };

            textField.addActionListener(listener);
            searchButton.addActionListener(listener);
        }

    }
}

This is basic Java 101. You might find something like What is the difference between a local variable, an instance field, an input parameter, and a class field? of help

  • Related