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 1
Oldest  Newest  Rating
0 Developer John · December 13, 2014
Thanks for all the help, I appreciate it!:sideways:
0 Developer John · December 13, 2014
Oh, okay. Yeah, I thought you named your class HandlerClass, but wasn't actually the HandlerClass that handled the events. I still had the error, but eventually compared my code to yours and figured it out. Now, you obviously didn't have a layout since your program only consisted of a JTextField. That was one of the differences I noticed between my code and yours. The second different would be the problem. I typed in:

public HandlerClass(JTextField text){
textinput = text;
}

//For a better look at it:

textinput = text;

What is the difference between that and this:
text = textinput;

You mentioned before that using static was bad practice. Why exactly?

-Thanks!
0 Developer John · December 10, 2014
I have no idea on what I'm doing wrong here:


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);

Image2 imageObject2 = new Image2(text);
text.addActionListener(imageObject2);
}

public void actionPerformed(ActionEvent e){
String Input = text.getText();

if(Input.equalsIgnoreCase("hello")){
System.out.println(Input);
}
}
}

The error I'm getting is directly on both of the lines for the object and text.addActionListener.
Your code directly states that you create an object inside the the newly created constructor. I don't see how that's possible.
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 5, 2014
Secondly, what exactly do I assign the object to:
Image2 imageObject2 = new Image2():

or:
HandlerClass handler = new HandlerClass();

The problem is, HandlerClass is non-existent. Do I assign it to the constructor? Another problem I sought out is that I currently have two constructors:
public Image2(){
super("Gui Title");
}

And of course, the new constructor:
private Image2(){
this.text = text;
}
0 Developer John · December 5, 2014
Wait, actually, I should first ask, when you declare this:
this.textField = textField;

What does this exactly do?
0 Developer John · December 5, 2014
Thanks!:sideways:
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 · December 3, 2014
Right when I press enter in the JTextField.
0 Developer John · December 3, 2014
It persists.
  • 1
  • 2

Java / Android Development

107,309 followers
About

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

Links
Moderators
Bucky Roberts Administrator