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. 
-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.
0 Developer John · November 28, 2014
Why even use a JPanel?
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?
0 Developer John · December 1, 2014
Okay, I'll give it a try and see how this works for me.
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 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)
0 Developer John · December 3, 2014
It persists.
  • 1
  • 2

Java / Android Development

107,292 followers
About

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

Links
Moderators
Bucky Roberts Administrator