The code has been simplified considerably as to only highlight the issue. I’m not certain this is the best way to approach this problem either.
I’m attempting to make an object that can have any single swing component and a set of generic methods to edit the component. In this case having a method which sets the components text if the component is a JLabel and returning whether it was successful.
package table;
import java.awt.Component;
public class CompTest
{
private Component comp;
public CompTest(Component C)
{
comp=C;
}
public boolean setText(String S)
{
if(comp instanceof javax.swing.JLabel)
{
comp.setText(S); //error
return true;
}
return false;
}
}
The object would be created similarly to;
...
CompTest comp1=new CompTest(new javax.swing.JLabel());
...
I’m using the Netbeans IDE 7.2, and gives me an error for the line containing “//error” (in the first codeblock);
cannot find symbol
symbol: method setText(String)
location: variable comp of type Component
How can I fix the issue, and if there is none (which I doubt) how do I get Netbeans to play nice?
The class
Componentdoes not have a method calledsetText.You have to cast
compfirst to a JLabel and then can call the method, e.g.: