-
Is there a way to make things happen if a certain text is entered in a TextField?
Hey,
First of all please excuse my lack of technical terms.
I want to have a JTextField be able to accept commands, and have the commands change pictures, display text etc.
Is their a simple way to do this?
Thanks in advance,
-Duster
(If you need the code, just ask)
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Quote:
Originally Posted by
DusteroftheCentury
Hey,
First of all please excuse my lack of technical terms.
I want to have a JTextField be able to accept commands, and have the commands change pictures, display text etc.
Is their a simple way to do this?
Thanks in advance,
-Duster
(If you need the code, just ask)
I would suggest reading java docs GUI tutorials and methods that fall into that concept. Try something that comes to your mind, if you fail, try again and then post errors you receive here.
-
1 Attachment(s)
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Attachment 980This image might help better explain what I want:
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Quote:
Originally Posted by
elisha.java
I would suggest reading java docs GUI tutorials and methods that fall into that concept. Try something that comes to your mind, if you fail, try again and then post errors you receive here.
Didnt see your post there. I shall try.
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Here is my code so far:
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainClass extends JFrame implements ActionListener
{
public static void main(String[] args)
{
new MainClass();
}
private JButton button1;
private JLabel label1;
private JTextField textbox1;
public MainClass()
{
JPanel panel1 = new JPanel();
{
button1 = new JButton("OK");
ButtonListener b1 = new ButtonListener();
JLabel label1 = new JLabel();
JTextField textbox1 = new JTextField(15);
textbox1.getText();
textbox1.addActionListener(this);
panel1.add(textbox1);
panel1.add(label1);
panel1.add(button1);
this.add(panel1);
this.setSize(300,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
private class ButtonListener implements ActionListener
{
public void ActionPerformed1(ActionEvent e)
{
if (e.getSource() == button1);
{
if (e.getSource() == button1);
//Code to check if the command the commands goes here?
}
}
}
public void ActionPerformed2(ActionEvent f)
{
if (f.getSource() == textbox1);
{
if (f.getSource() == textbox1);
//Or here?
}
}
}
Am I heading in the right direction? I checked the Java docs GUI tuts and from what I could gather this is where I should be going.
In the places where i specified where the code might go what code would I put in there? The Gui docs tut just shows how to add the ActionListener, not how to use it.
Thanks for the help,
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Quote:
Entering "Banana" and hitting "ok" would display a picture of "DANCING" banana
so far your codes seems right, but your not even half way to what you want
1.) By looking at your codes, Entering words and listening to the actions can be done
2.) Displaying a picture
- Start with how to paint images in a Light weight component
3.) Dancing Banana
- How do you want the banana to dance?
- Rolling, Blinking, (im not sure what you want)
- start with simple java animations
- start with no.2, youre on the right track from painting images to animating simple things
hope this helps
-
1 Attachment(s)
Re: Is there a way to make things happen if a certain text is entered in a TextField?
I just want it to add a pic to the panel by using ImageIcon. This is the image: Attachment 981
How would I listen to the actions? I know I can do "textbox1.getText();" to retrieve the text, but what code do I add to make it listen to that text and check if it is a command?
Also where would it go?
Thanks for your help,
-Duster
-
2 Attachment(s)
Re: Is there a way to make things happen if a certain text is entered in a TextField?
I have made a solution . May it work for you :P
First you need to enter "banana" as a command to the text field shown below
Attachment 982
Now Click on "Click" Button You will get below image .. which make u happy :P
Attachment 983
Code:-
package pack;
import java.awt.Image;
import java.awt.event.ActionListener;
import javax.swing.*;
public class letsDance extends JFrame {
private static final ActionListener ActionListener = null;
public static void main(String[] args) {
new letsDance();
}
private JButton clickButton;
private JTextField commandField;
public letsDance() {
JPanel panel1 = new JPanel();
{
clickButton = new JButton("Click");
clickButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clickButtonActionPerformed(evt);
}
});
commandField = new JTextField(15);
commandField.setToolTipText("Enter the command");
panel1.add(commandField);
panel1.add(clickButton);
this.add(panel1);
this.setSize(300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.setVisible(true);
}
}
private void clickButtonActionPerformed(java.awt.event.ActionEv ent evt) {
if(commandField.getText().toString().equals("banan a"))
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
//provide the right image location in your local machine as i provided "C:/manojworkspace/BananaDancing/src/image/Dancing_Banana.gif"
ImageIcon icon = new ImageIcon("C:/manojworkspace/BananaDancing/src/image/Dancing_Banana.gif");
Image normalImage = icon.getImage();
Icon ic = new ImageIcon(normalImage);
JLabel label3 = new JLabel("Banana Dancing", icon, JLabel.CENTER);
frame.add(label3,"North");
frame.setSize(600, 300);
frame.setVisible(true);
}
else{
// add as your need
}
}
}
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Manoj Kumar Chhetri, please read the forum rules. I would also highly recommend that you read following:
The Problem with Spoon-feeding
I am stopping short of removing your code, but that does not mean another moderator won't come along and do so.
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
I appreciate this but I understand that spoon feeding is bad. I only needed a few lines of that and that is what I will use.
Thank you.
@copeg I understand, I accidentally spoon fed someone this morning, before I knew it was wrong.
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
-_-, i never thought of a GIF image -_-, i was thinking you want to create the sprites and animation manually, well there you go.
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
I really dont want to just use the code given, I want to figure it out with help.
Here is my new code:
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class MainClass extends JFrame
{
public static void main(String[] args)
{
new MainClass();
}
private JLabel label1;
private JTextField textbox1;
public MainClass()
{
JPanel panel1 = new JPanel();
{
JLabel label1 = new JLabel();
JTextField textbox1 = new JTextField(15);
textbox1.getText();
textbox1.setToolTipText("Enter commands");
panel1.add(textbox1);
panel1.add(label1);
this.add(panel1);
this.setSize(300,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
public void ActionPerformed2(ActionEvent f)
{
if (textbox1.getText().toString().equals("banana"))
{
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon pic = new ImageIcon(MainClass.class.getResource("PBn'J.gif"));
frame1.add(new JLabel(pic));
}
}
}
When I type "banana" and hit enter, nothing happens. No errors messages either. Do I need to add an action listener? if so, how would I do that in this instance?
Thanks for helping
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Yes you need a listener of some sort - an ActionListener your best bet (which you can add to a JTextField). See How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
@moderator
I sorry for what i did . Actually i am new to this forum and some of the post i have seen others providing solution using code snippets ...So that's why i did ..
But if i will not provide snippets, how would i able to give my solution ? If you recommend suggestion in terms of providing links and some comments, So its just become a long thread of a clarification/queries chain and just the wastage of time.
Personally i do believe in open source ethics "share and learn".
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Quote:
Originally Posted by
Manoj Kumar Chhetri
@moderator
I sorry for what i did . Actually i am new to this forum and some of the post i have seen others providing solution using code snippets ...So that's why i did ..
But if i will not provide snippets, how would i able to give my solution ? If you recommend suggestion in terms of providing links and some comments, So its just become a long thread of a clarification/queries chain and just the wastage of time.
Personally i do believe in open source ethics "share and learn".
Please check your private messages. Lets leave this discussion outside this thread for fear of getting off the main topic at hand.
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Thanks, I checked the link and touched up on ActionListeners. I wrote this code,
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class MainClass extends JFrame implements ActionListener
{
public static void main(String[] args)
{
new MainClass();
}
private JLabel label1;
private JTextField textbox1;
private JButton button1;
public MainClass()
{
JPanel panel1 = new JPanel();
{
JLabel label1 = new JLabel();
JTextField textbox1 = new JTextField(15);
textbox1.getText();
textbox1.addActionListener(this);
textbox1.setToolTipText("Whats the password?");
button1 = new JButton("OK");
button1.addActionListener(this);
panel1.add(textbox1);
panel1.add(button1);
panel1.add(label1);
this.add(panel1);
this.setSize(300,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
if (textbox1.getText().equals("password"))
System.out.println("You sir are a good guesser.");
}
}
}
And it produces this error message:
Code :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MainClass.actionPerformed(MainClass.java:48)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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$000(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)
Please help, not sure what is wrong with my code.
Thanks,
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
What is null on that line of code? Add some println's in there to see (hint: where do you initialize the instance variables?)
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Could you explain what null means? And what kind of printlns? I know they can be used to catch exceptions but how?
Thanks for the help,
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Quote:
Originally Posted by
DusteroftheCentury
Could you explain what null means?
null means an object has not been instantiated. In the following code...
...myValue will be null.
Quote:
Originally Posted by
DusteroftheCentury
And what kind of printlns? I know they can be used to catch exceptions but how?
Thanks for the help,
-Duster
Simply print out to the terminal (eg System.out.println). It allows you to inspect the value of a variable, and is one of the many invaluable debugging techniques (see http://www.javaprogrammingforums.com...t-println.html ). The exception stack trace tells you the method that throws the exception (and even the line it occurs on), so you can print the values of some variables you access prior to where the exception is thrown to see which is null, the backtrack to investigate why (and keep in mind my hint above). I am not trying to torture you by not telling you exactly what is wrong, because learning how to determine what is wrong is an important skill to learn.
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
What lines of code would you reccomend checking? And how? I understand your blog, but I do not see how that would apply here, because I dont really use numbers and integers and such, which if I did I would know how to check. However I dont know how to check text variables.
Thanks for helping a beginner,
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Quote:
Originally Posted by
DusteroftheCentury
What lines of code would you reccomend checking? And how? I understand your blog, but I do not see how that would apply here, because I dont really use numbers and integers and such, which if I did I would know how to check. However I dont know how to check text variables.
Thanks for helping a beginner,
-Duster
Read the exception:
Quote:
at MainClass.actionPerformed(MainClass.java:48)
Looks like line 48 of your MainClass.
PrintStream (System.out) contains print statements for all sorts of data types, not just primitives. To reiterate, go to the line of code above and inspect the value of the variables used in that line. For instance, you could write
Code :
System.out.println(textbox1);
What does it print?
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
It prints this:
Code :
javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@25b80053,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=15,columnWidth=0,command=,horizontalAlignment=LEADING]
Thanks for helping,
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Quote:
Originally Posted by
DusteroftheCentury
It prints this:
Code :
javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@25b80053,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=15,columnWidth=0,command=,horizontalAlignment=LEADING]
Thanks for helping,
-Duster
NO. In your previous last given code, you have actually declared textbox1 as instance variable and then in constructor, you re-declare and initialize it as a local variable, so the instance variable is actually shadowed but still null. Just remove the declaration inside the constructor and the code will work fine.
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
What line do I remove? Sorry Im just not very familiar with the many technical terms.
Thank you so much,
-Duster
-
Re: Is there a way to make things happen if a certain text is entered in a TextField?
Code :
JTextField textbox1 = new JTextField(15);
Instead of this, just write
Code :
textbox1 = new JTextField(15);