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