Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Action Listener  RSS feed

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I go about making my action listener run my entire program?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can have an action listener do anything you like. Just put the appropriate code in it.

But to me that sounds like a strange thing to ask. Would you like to post a more detailed question than that? It's possible that your question is more complex than it seems, or perhaps you have a misunderstanding about what you are supposed to be doing.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

I have a lot of code going on and I didnt want to be annoying by posting an entire page of code, but what I am trying to achieve is to create a simple GUI that will run the program I have created for my class over previous weeks. It is a simple program that prints a list of information that I added to it; it is supposed to be a kind of database I guess. I have created the major portion of the GUI, but I cant seem to figure out how to make it run my whole program when I press the "Run" button I created. Here is the code for my GUI:



And then here is the rest of my program:



 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I entirely understand your first program, but wouldn't you just put this code into your action listener?



Although I really don't see the point of doing that, since you normally can't see the output of System.out.println when you're running a Swing application.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

A lot of what I have been doing doesnt make much sense from what people tell me, but its what they want for my assignments, so I dont have much choice. Thanks for your help!
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Would you happen to know how to get my output to display in a text area in the GUI?
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute,

Im using the Netbeans GUI builder and I cant find much information on how to print my output with it, which is why I was asking. So far I havent really tried anything because Im not even sure where to start, because the guides I do find are based on hand coding.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to see debugging messages and this is a "real" program, I would use Logging, either the built-in java/util/Logging or Apache's version. But if this is just homework, System.out.println will probably work in Netbeans (It does in Eclipse).

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Essentially what I want to do is have the output that prints after you run the program to print in a text area in the GUI. So from what youre saying, Im assuming that I would have to come up with something like:

 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, if you actually need to post text to a text box, I would use a JTextArea; create one and pass it into your runGame method.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:No, if you actually need to post text to a text box, I would use a JTextArea; create one and pass it into your runGame method.


You lost me there Knute, Im not sure what you mean
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Gutierrez wrote:Would you happen to know how to get my output to display in a text area in the GUI?


Earlier you said you wanted to "print" that data. Now, to me "printing" involves paper but maybe that's not what your assignment wants. Perhaps you should clarify the requirements?
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking back over the code that you posted, I'm wondering if this program runs at all. We (all) may have been talking at different levels. You want a GUI program that prints ten Games to a text area, right? Your first file is on the right track, but the action listener of the Run button does nothing:



Others may disagree, but I think the best starting point is to just put the contents of runGame() here the comment is. This will give you a program that prints to the console when you press the Run button. The console in Netbeans (or any IDE) will be a window in the IDE itself, not your program. Get this working first.

The second thing to do is get the text to print to a text area.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute, thats actually exactly what I did, I already corrected my previous code between my posts. My code in the gui builder works as it should, the gui pops up, when I click "Run" the program runs, but it prints the output in the normal area at the bottom of Netbeans. My exit button works as it should, my only issue is that I have no idea how to edit the code in the gui builder to make it so my output also prints in the textarea.

 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know anything about Netbeans Gui Builder, but from what I see on the web, it just creates a shell that you have to fill in. In some ways this is bad for beginners because they won't know what's happening "under the hood".

But let's look at the code. At the bottom all the GUI objects are declared. What does this mean regarding their scope? Another way to say this is, Will you be able to "see" jTextArea1 in inventory.runGame()? If not, we need to pass a reference to that method. See if you can do that first.

And I probably steared you the wrong way with JTextArea. That's way more than you need. Probably just a JLabel is all you need.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I don't know anything about Netbeans Gui Builder, but from what I see on the web, it just creates a shell that you have to fill in. In some ways this is bad for beginners because they won't know what's happening "under the hood".

But let's look at the code. At the bottom all the GUI objects are declared. What does this mean regarding their scope? Another way to say this is, Will you be able to "see" jTextArea1 in inventory.runGame()? If not, we need to pass a reference to that method. See if you can do that first.

And I probably steared you the wrong way with JTextArea. That's way more than you need. Probably just a JLabel is all you need.


I was under the impression from my class text that you could only pass by value in Java.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I did some research and apparently with the GUI builder and using a jLabel like you stated, I am supposed to create an event handler. So, I came up with this:



But, as far as I can tell it doesnt do anything. At this point Im oretty confused
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Gutierrez wrote:
I was under the impression from my class text that you could only pass by value in Java.


You are right, but a reference is just a value. So when you pass in a parameter by value, and that value is a reference to an object, you can use the referenced object -- call its methods, set its values. The thing you *can't* do is create an object and return it through a parameter; you have to use the return keyword to do that.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Gutierrez wrote:So I did some research and apparently with the GUI builder and using a jLabel like you stated, I am supposed to create an event handler. So, I came up with this:



But, as far as I can tell it doesnt do anything. At this point Im oretty confused


First, let's get a label to work with. Using GUI Builder or by hand, change jTextArea1 to jLabel1 and all the JTextArea references to JLabels.

Now you have some code that looks like this:



Is jLabel1 in scope there? Is it visible? Yes, because it was declared in the same file outside of any method or block. But can runGame() see jLabel1? No, because it's in another file. So we need to pass it to the method through a parameter.



But now there's a problem. runGame() isn't defined to take another parameters, so we need to change the definition of runGame().



Oops, you have another problem: runGame() is private. Let's make it public.

Now you should have a reference to jLabel1 in runGame(). Try to get the text into it by looking at the documentation for JLabel (← click).
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute,

I decided to not use the GUI builder and I went back in and hand coded everything. Reading through the link you shared, I think I have to use .getText or .setText somehow. Am I on the right track? Unfortunately I only have 30 minutes before this assignment its due so I may have to turn it in like this but hand coding it was definitely easier to understand than the whole GUI builder thing.

 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, setText is the way to get text into a label.

Most of the people here think it's better to hand code things when you're new -- some even think you shouldn't use an IDE.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im only using Netbeans because the instructor said we had to; Im used to writing code in notepad++. I figured it was setText but I dont know enough about Java to decipher what those "standard" text mean. They make it too cryptic and daunting for a beginner. To be honest, from what we have done so far in my course, it just makes Java look like something that was created by Satan and I cant imagine doing this all day for a living. You guys that do this all the time have my genuine respect, I just cant grasp the concepts fast enough.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO using an IDE as a beginner is not a terrible choice. And the standard documentation isn't very beginner friendly. And programming is hard! But keep at it; things may start falling into place.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . . the standard documentation isn't very beginner friendly.
Agree. This how to write guide for documentation suggests documentation is not written for beginners.
And programming is hard! . . .
The Java® language is really easy (or was until the reintroduced generics). It is programming itself which is difficult. Very difficult.

I think this latter part of the thread might fit well into the “beginning Java” forum, so I shall duplicate it there.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want you guys to think I'm not trying; I spend hours reading the text we are provided with, I look at online tutorials, but they explain everything in a way that only someone who already has experience coding would understand. For the most part, my assignments are: Do this and this and this, read this chapter if you have questions. Ive been able to complete most of the assignments and I think that I understand Java at a very basic level, but sometimes just the small examples I get on this site help me put the pieces together. I have never even seen setText in my course yet, so I honestly have no idea how to implement it. I understand the basic structure is going to look like:



And I understand that I have to do something in the () after setText to link it to my runGame method, but Im not sure how exactly to go about this. Also, when I do this:



I get: "method runGame in class IT215Inventory cannot be applied to given types;
required: no arguments
found: JLabel
reason: actual and formal argument lists differ in length"
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Gutierrez wrote:I don't want you guys to think I'm not trying; I spend hours reading the text we are provided with, I look at online tutorials, but they explain everything in a way that only someone who already has experience coding would understand.


I believe you're trying. I'll try to help you

For the most part, my assignments are: Do this and this and this, read this chapter if you have questions. Ive been able to complete most of the assignments and I think that I understand Java at a very basic level, but sometimes just the small examples I get on this site help me put the pieces together.


Doesn't sound like the greatest class.

I have never even seen setText in my course yet, so I honestly have no idea how to implement it. I understand the basic structure is going to look like:



And I understand that I have to do something in the () after setText to link it to my runGame method, but Im not sure how exactly to go about this. Also, when I do this:



I get: "method runGame in class IT215Inventory cannot be applied to given types;
required: no arguments
found: JLabel
reason: actual and formal argument lists differ in length"


That sounds like you changed caller

but didn't change the definition in the IT215Inventory class


It may be time for you to post your file or files again. From what you've described I would expect to see "Results" in your JLabel.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed that piece of code that was giving me the error so that it would run. I posted a screenshot of my tiny GUI and how it outputs the results to the console when I click "Run". This is my code for the GUI:

java example.png
[Thumbnail for java example.png]
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see a lot of things but let's concentrate on getting the GUI working. You have a run button b1 and on that button you set an ActionListener with some code in it:



The ActionListener "listens" for an action event (pressing the button) and calls the method actionPerformed(). In that method, you create an object from the IT215Inventory class. Then you call runGame() on that class. This is important. Notice that you're calling runGame without any parameters.

Now I know you have a class IT215Inventory because I can see it in the screenshot of NetBeans. It's this class where you need to pass in the JLabel and update its text property. So the runGame method in the file you posted is not doing anything; it can be deleted. Also, the jButtonb1ActionPerformed method is never called; it can be deleted. And at the top, you are extending the class:



You can remove extends IT215Inventory; I don't think it's doing anything.

Now, update the runGame method in IT215Inventory (pass in jLabel1 and use setText) and post that code too.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh man I dont know how I didnt put that together.

Here is my new code for both the GUI and my IT215Inventory class






The weird thing is that now, when I click "Run" on my GUI, I was expecting it to just output everything with the word Results but it actually doesnt output anything, it just stays blank. There was another wrench thrown in my whole process too; I have to do this for this week:

Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.
Add a company logo to the GUI using Java graphics classes.

This is added to the step I was never able to complete where I have to have 5% added to the value of my total inventory. I wrote the code for it but it never worked, so its all just piling up
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The weird thing is that now, when I click "Run" on my GUI, I was expecting it to just output everything with the word Results but it actually doesnt output anything, it just stays blank.


There is some confusion about what you're adding to the JFrame:



You're adding buttons and a label to the JPanel, adding the panel to the frame, wiping it out by setting a new layout. The buttons are showing because you add them again directly to the frame. Try adding the label directly to the frame and it should work. I don't think you need a JPanel right now, or at least you're not using it.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.
Add a company logo to the GUI using Java graphics classes.


It looks like a JLabel may not be the best choice with this new requirement. This list may help you if you haven't seen it. What would be the best component to use? Take a look at JList but check to see if another would be better. Get your component working with displaying the games and then look at adding Next and Previous buttons.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to correct those issues you mentioned, and I finally figured out how to add the 5% I needed to the final output. Now, I think Im going to have to use a JTextfield for the output; or maybe a Spinner? A list might work but Im supposed to be able to add new items to the list and apparently a JList cant be changed from what Im reading. Hopefully I can understand this one.

So, using a JTextField, I did the following:




Now, when I click "Run" in my gui, it just displays the word "Results". I tried several ways that I could think of to get my output to display in the text field. Ive tried doing things like:



But it always ends in some kind of error. I have read on textfields on several sites but I havent seen anything about pushing console output to it.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the errors? Pick some code you think should be correct, post the code and the errors.

This looks wrong:



Where is get defined? What is GameName?

You might also want to post Game.java.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is Game.java



My error now is that I dont know what to do with setText; I dont see anywhere a guide on how to output an array to setText
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should be helpful. The idea is to put all the text into a String and then set it as the text of the JTextField.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up not being able to figure out how to do it with a textField, so I did further research and I found out how to finally get everything to output using a textArea. Moving along now, I have to figure out how to make these buttons allow me to select the items in my output . I have been working on it for a while now but I havent been able to figure it out.


java example.png
[Thumbnail for java example.png]
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, Im back for what will probably be my last post on this course; Im on the final stretch here and Im having a little trouble with the final requirements.

Modify the Inventory Program to include an add button, a delete button, and a modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item.
Add a save button to the GUI that saves the inventory to a C:\data\inventory.dat file.
Use exception handling to create the directory and file if necessary.
Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that product’s information in the GUI.

I think that for the save function I have to use a file chooser, but Im not sure how to implement it.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look here
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!