Hello All, I'm in the process of making a GUI for a search engine, I have never coded a GUI before so just experimenting with user input in Textfields and also the use of ActionListeners and i'm having a slight problem.


My code can pick up what the user has entered if they press Enter, (I have got an ActionListener on the jtftext1)

However when I press button "a", it sets the text to the button name "Search!".

I want to be able to show "Searching: <userinput>" for both when i press enter and click the button.

here is my code:

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

import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class Interface extends JFrame {

	//Class Declarations
	JTextField jtfText1, jtfUneditableText;
	String disp = "";
	TextHandler handler = null;
	JButton a;
	
	//Constructor
	public Interface() {

		super("TextField Test Demo");
		Container container = getContentPane();
		container.setLayout(new FlowLayout());
		a = new JButton("Search!");
		jtfText1 = new JTextField(10);
		jtfUneditableText = new JTextField("", 20);
		jtfUneditableText.setEditable(false);
		
	//adding pic
	ImageIcon image = new ImageIcon("C:\\java\\pics\\1.jpg");
	JLabel imageLabel = new JLabel(image);
	container.add(imageLabel);
	
	//adding contents
		container.add(new JLabel("Please enter your query"));
		container.add(jtfText1);
		container.add(jtfUneditableText);
		
	//buttons
		container.add(a);
		
		handler = new TextHandler();
		jtfText1.addActionListener(handler);
		a.addActionListener(handler);
		setSize(500, 500);
		setVisible(true);		
	}
			
	//Inner Class TextHandler
	private class TextHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {
				System.out.println(e.getActionCommand());
				disp = "Searching:  " + e.getActionCommand() + "\t...";
				jtfUneditableText.setText(disp);
		}
	}
	
	//Main Program that starts Execution
	public static void main(String args[]) {
		Interface test = new Interface();
		test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}//end

Recommended Answers

All 5 Replies

I'm thinking that i'm going to need 2 methods that use ActionListener, one for pressing enter and one for clicking the button, still can't get it to work though.

my two methods:

handler = new TextHandler();
button = new ButtonHandler();
jtfText1.addActionListener(handler);
a.addActionListener(button);

	//Inner Class TextHandler
	private class TextHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {
				//System.out.println(e.getActionCommand());
				disp = "Searching:  " + e.getActionCommand() + "\t...";
				jtfUneditableText.setText(disp);
		}
	}
	
	//button handler
	private class ButtonHandler implements ActionListener {
	
		public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());
		disp = "Searching: " + e.getActionCommand() + "\t...";
		jtfUneditableText.setText(disp);
			}
	}

Still comes up with Search! when i click the button

The way to get the user input from some text field tf is tf.getText()
You have been using getActionCommand, which is something completely different. Actions are a way to have multiple UI events share the same behaviour. As it happens, the action name for a text field it defaults to the current text, but for a button defaults to the button name.
For your immediate problem, use getText(), then have a look at how to use Actions here
http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

you are going correct directions, you can listening for ENTER key from JTextField,

I think that you have look at JFileChooser

Ok, thank you for replies. using getText() has worked. Will read up on Actions

Sorry to bring this back but I'm having some problems when trying to set a panel within my JFrame to visible when someone either presses enter or clicks the JButton within my two methods shown in previous post.

I am getting the following error:

Cannot make a static reference to the non-static field Interface.panel.

The updated code for the TextHandler is as follows:

private class TextHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {
				Interface.panel.setVisible(true);
				disp = "Searching:  " + e.getActionCommand() + "\t...";
				jtfUneditableText.setText(disp);
		}
	}

When i just use

panel.setVisible(true);

it does compile, but comes up with a bunch of errors when i press enter and try to show the panel, probably due to it not recognising panel in the TextHandler method as it is in Interface().

the code for my JPanel panel is within my class Interface() along with all the other code for creating the GUI textfields and such.

JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

		b = new JLabel("Blah blah");
		c = new JLabel("blah blah");
		
		panel.add(b);
		panel.add(c);
		panel.setVisible(false);

what am i doing wrong?

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.