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
+1 Homer Simpson · November 27, 2014
Aight bro, I don't really understand what you're trying to do, but there's a getText() method you can call on your textfield object. 
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 1, 2014
Okay, I'll give it a try and see how this works for me.
0 Developer John · December 5, 2014
Thanks!:sideways:
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
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 13, 2014
Thanks for all the help, I appreciate it!:sideways:
0 Developer John · December 2, 2014
Alright, so I tried it, and still it doesn't work. I don't understand how I can code this though. I want it to get the input from the JTextField, and then print that out using a print statement. For this, yes I used an ActionListener, because without it, the program really wouldn't have any functionality. Remember what I stated, I want the user input, but the JTextField is supposed to contain no String inside it until the user types something in. Using the ActionListener is the part I don't know how to do. How would I be able to use an if statement to check and see if the user input is equal to a String? I tried it and it threw me an error saying it was getting it from an unknown source:


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 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 2, 2014
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at me.wizard7611.Challenges.Image2$HandlerClass.actionPerformed(Image2.java:49)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
  • 1
  • 2

Java / Android Development

107,153 followers
About

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

Links
Moderators
Bucky Roberts Administrator