• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I force the compiler to utilize a method from a specific library ... ?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m trying to get the text from a button, which is done by the method getText() of the library javax.swing.AbstractButton, but, I think the compiler is getting the wrong �getText()�, I mean, this method from another library ,as long as there are 17 different libraries that implement this method... . At first, I changed the order for the �imports� and put javax.swing.* as the last one, so that its getText() method could override the others... But I think , somehow , I miss some important concept... So, can I force the compiler to utilize a method from a specific library ?
The code is :

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
o o o
public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
String buttontext = source.getText();
o o o
And I get this from the compiler...
" cannot resolve symbol"
symbol : method getText ()
location: class java.lang.Object
buttontext = source.getText();
^

So ... could anyone help me ?
Thx
F�bio
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
public void actionPerformed(ActionEvent evt)
{
String buttontext = evt.getActionCommand();
}

This will return the text on the button.
Best Regards,
-Jason
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of things -
If you just want to get that it is a Button then you can use
evt.toString().
However I think that you want to know the Label that is on the Button.
So first - source is an Object not a button. Object does not have a getText() method.
getText() only works for TextComponents for the most part. Button does not have that method.
However Button does have a getLabel() method. Try this:

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - so you are talking about JButtons which DO have a getText() method.
So do what Jason suggested.
[This message has been edited by Cindy Glass (edited December 07, 2001).]
 
Colin Kenworthy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fabio,
Yes you are missing an important concept, the order of the imports DOES NOT MATTER.
I am from a mainframe background where the order of the libraries DOES make a difference as they are searched in the order they are listed.
Java is object based so when you ask for the 'getText()' method for an object it does not go to your import libraries and get the first one it finds. By a process called 'late binding' the JVM determines the actual class of the object and uses the appropriate 'getText()' method.
The compiler is another matter and you can use something called a 'cast' to allow you to call getText() on an object of type Object.
e.g. String buttontext = ((JButton)source).getText();
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far... thanks a lot , folks !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic