aspose file tools*
The moose likes Beginning Java and the fly likes How can I force the compiler to utilize a method from a specific library ... ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I force the compiler to utilize a method from a specific library ... ?" Watch "How can I force the compiler to utilize a method from a specific library ... ?" New topic
Author

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

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 31, 2001
Posts: 280
Try this:
public void actionPerformed(ActionEvent evt)
{
String buttontext = evt.getActionCommand();
}

This will return the text on the button.
Best Regards,
-Jason

Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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:


"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Aug 06, 2001
Posts: 88
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

Joined: Nov 22, 2008
Posts: 18944
So far... thanks a lot , folks !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can I force the compiler to utilize a method from a specific library ... ?