• Post Reply Bookmark Topic Watch Topic
  • New Topic

Action Listeners  RSS feed

 
Marvin Hunkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. well no way to add atatchments. so will paste the code files below. can any one help me out. the previous example i did works. but this one does not. so will just and copy and paste. so maybe some one could run it in java, then how do i fix this. this was the code shown, i was reading in a java book. learn to program with java, john smiley. marvin.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Marvin, What error you are getting?
 
Marvin Hunkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. well. here's the error when I try to compile. how to fix this. what am I doing wrong. what is the correct code. can you have a look and help me out. tried googling, and looked, and tried rewriting the code, but still no luck. any ideas. marvin.

javac ButtonListener.java
ButtonListener.java:15: error: cannot find symbol
JOptionPane.showMessageDialog("My name is: " + txtName + " ");
^
symbol: variable txtName
location: class ButtonListener
1 error
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That field is declared in class Draw GUI, not in the one you're using it in.
 
Marvin Hunkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. so how do I fix that. what is the correct code. can you let me know. and where to put it. can you help. marvin.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ButtonListener has a reference to the DrawGUI object - you're already using that to get at another object in the DrawGUI class; why not do the same for txtName?
 
Marvin Hunkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. so what is the code to do that. do you have a example, and then how to fix this.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're already using that to get at another object in the DrawGUI class

The field I'm referring to is "btnName".
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marvin Hunkin wrote:hi. well no way to add atatchments. so will paste the code files below. . . .
Quite right. That's how we like it. It is much easier to read code pasted into code tags (I see you used them, but not correctly, so I corrected that). Unfortunately your code wasn't indented, but code tags make it look better.
We don't post answers; we believe people learn better if they work things out for themselves.

I do not think you are ever going to get that listener code to work. I have my own ideas about Listeners, and where you put them. It depends where you are going to use the listener, and you can read about it here, and read the following posts for other links.
I suggest you need a Show Name listener and an exit listener. I think your putting System.exit() calls is disastrous. Remember Swing runs in its own thread. What if something else is going on in another thread, e.g. writing to a database? You call System.exit and all threads terminate suddenly and the writing stops. If you didn't set up the database correctly, you get half a record written and the database is corrupted. (Actually most databases will reject half a record and revert to their old state). Look at this thread, where Rob Camick provides a link showing a nice way to close a frame.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to use Components in listener classes, you can make those Components fields of the listener class, and set their values via the constructor.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss this sort of thing on our GUIs forum, so I shall move this discussion.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how to fix this.

What did you understand from my last two posts? Do you know what I mean by that ButtonListener has a reference to DrawGUI? Do you know what such a reference can be used for? How do you think "btnName" demonstrates that?
 
Marvin Hunkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. well the name, has the button listener, and then draws from the draw gui. so. how do i fix this. e-mail me and will send you the complete project.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's break this down some more. You're using the "x" reference in ButtonListener to access the DrawGUI variable "btnName". Now you want to reference another DrawGUI variable in ButtonListener. You tried doing that by using the name it has in DrawGUI -"txtName"- which didn't work. Do you see any parallels between using btnName and txtName in class ButtonListener? If you don't, carefully compare how you're using each, keeping in mind that one of them causes an error, while the other works fine.

(And no, we don't need to see any further source code - nor would I personally would want to, certainly not as a zip via email.)
 
Marvin Hunkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. yes, txt Name is a text box, and btnName is a button. so, how to do this, and how to get it right. can you give me some sample code, how to fix this and to get it to compile. all i want to do is to enter a name, click show name, then have a show message dialog in java, to show me the name, with a space between first and last name. so. how to do this. any sample code you can show me.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will never get anywhere unless you understand what you're doing. Post the two lines of code from the ButtonListener class in which you're using the txtName and btnName fields. If you see those two lines next to one another, something should click.
 
Marvin Hunkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. the txt and the btn are in DrawGUI, not in the button listener. and took out the variable. so it compiles, but shows nothing when i click Show Name. so how do i add the variable. just point me to some sample code, or maybe show me the code, i need. sorry. trying my best as a neubie. do understand what is going on. marvin.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't comment on why code that we don't ses might not work. As this seems to be a different issue, please start a new thread with the details.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!