Java Swing TextField Problem

+2 Developer John · November 26, 2014
Ok, so I've been experimenting with TextFields lately and cannot find out how I can get my scanner variable(scan) and my string variable(UserInput). I am trying to make a TextField get userinput and allow the TextField's ActionListener to do something. I have not defined what will go inside the ActionPerformed method yet since I don't know how I can get those two variables:


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Image2 extends JFrame{

private ImageIcon image1;
private ImageIcon image2;
private JTextField text;
private JLabel label;
private JLabel label2;

public Image2(){
super("Images");
setLayout(new FlowLayout());

Icon image1 = new ImageIcon(getClass().getResource("Pic1.jpg"));
Icon image2 = new ImageIcon(getClass().getResource("Pic2.jpg"));

Scanner scan = new Scanner(System.in);

String UserInput = scan.nextLine();

JLabel label = new JLabel(image1);
JLabel label2 = new JLabel(image2);
add(label);
add(label2);

JTextField text = new JTextField(UserInput);
add(text);

HandlerClass handler = new HandlerClass();
text.addActionListener(handler);
}
}
class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){

}
}


-Thanks!

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Developer John · December 7, 2014
I'm confused now. How exactly does Java know to read from something? Right now, for instance, I have a class named Image2, and constructors named Image2, yet I still get an error when using text.addActionListener(imageObject2);:

Image2 imageObject2 = new Image2(text);

text.addActionListener(imageObject2);

The error directly states:
The method addActionListener(ActionListener) in the type JTextField is not applicable for the arguments (Image2).
0 Developer John · December 3, 2014
It persists.
0 Developer John · December 3, 2014
Right when I press enter in the JTextField.
0 Developer John · November 28, 2014
Why even use a JPanel?
0 Developer John · December 3, 2014
Why not just copy and paste the code?
Image1:

package me.wizard7611.Challenges;

import javax.swing.JFrame;

public class Image{

public static void main(String args[]){

Image2 imageObject = new Image2();
imageObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
imageObject.setSize(600, 600);
imageObject.setVisible(true);

}
}

Image2:

package me.wizard7611.Challenges;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Image2 extends JFrame{

ImageIcon image1;
ImageIcon image2;
JLabel label;
JLabel label2;
JTextField text;
JPanel panel;

public Image2(){
super("Images");
setLayout(new FlowLayout());

JPanel panel = new JPanel();
panel.setSize(600, 600);

Icon image1 = new ImageIcon(getClass().getResource("trolldad.jpg"));
Icon image2 = new ImageIcon(getClass().getResource("mexican troll.jpg"));

JLabel label = new JLabel(image1);
JLabel label2 = new JLabel(image2);
panel.add(label);
panel.add(label2);

JTextField text = new JTextField(10);
panel.add(text);

add(panel);

HandlerClass handler = new HandlerClass();
text.addActionListener(handler);
}

private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent e){
String Input = text.getText();

if(Input.equalsIgnoreCase("hello")){
System.out.println(Input);
}
}
}
}
0 Developer John · November 28, 2014
Thanks for all the help!
0 Developer John · November 28, 2014


And in addition to what Kayler said about it not needing a listener, this is for the most part true. Often times, what you'll have is a button that when clicked, reads the text in the textfield, like a save button. Or, you could also get the text when you close the window. It's really a case by case type of thing. You can get the text of the text field from pretty much anywhere you need to at any time during runtime, it doesn't have to occur when the textbox's actionPerformed is fired.



Is this because...
.getText()

is a built in method for a JTextField, so it basically does all the work for you?
-1 Developer John · November 27, 2014
Thanks!
-1 Developer John · November 27, 2014
Actually no, I'm not planning on just using the 
text.getText();

method. I'm going to add some more functionality to this program using if statements. Then, I can put it inside the ActionListener.
  • 1
  • 2

Java / Android Development

124,426 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator