Class n.1:
void visualizza(){
System.out.println("Testing")
.....
}
Class n2 (JFrame):
label1.setText(obj1.visualizza());
Netbeans tell me error: "void type not allowed here"
Your void visualizza() method doesn't return any String.
System.out.println("Testing") just prints the String Testing on console.
But for setText() method you need to pass a String as parameter.
Most probably this is what you are trying to achieve:
String visualizza() {
return "Testing";
}
Then-
label1.setText(obj1.visualizza());
PS: Please do your own research at least on the basics before referring to stackoverflow. Good luck!
Related
Netbeans autogenerates the get() function, but my application doen't run if the return doesn't have the this.
Incorrect:
public String getStringVariable(){
return stringVariable;
}
Correct:
public String getStringVariable(){
return this.stringVariable;
}
As I work with many (many) variables its difficult write this in each get. I found I can change App.java but I don't know its location (http://www.fordfrog.com/2010/07/19/modifying-netbeans-generated-getters-and-setters/)
Thank you!
So I have a class with the following constructors:
public TaxiModule(TaxiData taxiData)
{
this(taxiData, VehicleUtils.getDefaultVehicleType());
}
public TaxiModule(TaxiData taxiData, VehicleType vehicleType)
{
this.taxiData = taxiData;
this.vehicleType = vehicleType;
}
When I try to call:
new TaxiModule(taxiData)
Eclipse gives me an error, saying: "The constructor is undefined." When I click on the little light-bulb thingy for the quick fix, it suggests:
Add argument to match constructor TaxiModule(TaxiData, TaxiConfigGroup).
Add argument to match constructor TaxiModule(TaxiData, TaxiConfigGroup, VehicleType).
Clearly from the constructor details my call is valid and the second suggestion does not fit.
Any help would be great.
Thanks!
How can I call from external JS with JSNI?
For example:
//Some external JS code
...
this.onFeatureClick = function(event) {
...
var name = "Batman";
passToJava(name); //Invoke java method and pass String name
};
I tried this here:
public void onModuleLoad() {
...
nativeVariableName(); //Call native method
}
public static void passToJava(String name) {
System.out.println(name);
}
public native String nativeVariableName() /*-{
$wnd.passToJava = function(name) {
#com.google.myproject.webinterface.client.MyWebInterface::passToJava(Ljava/lang/String;)(name);
}; }-*/;
I don't even know if the call from JavaScript works.
Thanks.
This code works just fine. I don't know where do you expect to see result of invocation of System.out.println, but looks like you are looking into wrong place. Replace System.out.println with Window.alert and see for yourself. If it doesn't work, it means that error is in some other place:
Check if the function is correctly exposed (open console in browser,
and type window.passToJava, if it displays null, function wasn't
exposed)
Check if onFeatureClick is called correctly.
I am building a user interface in netBeans (coding by hand, more flexible) with multiple toolbars.
What I am trying to do is create an actionListener for each button. I am retrieving names of the functions from XML and parse them to string. I will write implementations for those functions in a separate class, but my problem is the following:
How do I make the link between the function name and the string containing it's name?
Example: String is Open(), function will be Open(someParameter) and in the definitions class there will be static void Open(param).
First of all, consider my comment about your idea of dynamic button behavior resolved from strings being a wrong approach. However if you still need exactly what you asked, what you need is Reflection API.
Here's an example:
Class c = SomeClassWithMethods.class;
Method m = c.getMethod("someMethodName", String.class, Integer.class, Integer.TYPE);
m.invoke(baseObjectFromWhichToCallTheMethod, "stringParam", 10, 5);
Added:
Another option, which is a little bit prettier than reflection, but still a messy design, would be to use a map to link those Strings to methods. The code is a bit longer, but from the Java perspective it is much better than using reflection for your task (unless you have some specific requirement of which I'm not aware). This is how it would work:
//Interface whose instances will bind strings to methods
interface ButtonClickHandler {
void onClick();
}
class SomeClassYouNeed {
//One of the methods that will be bound to "onButtonOneClick()"
public void onButtonOneClick() {
log.info("ButtonOneClick method is called");
}
public void onButtonTwoClick() {
log.info("ButtonTwoClick method is called");
}
//Map that will hold your links
private static Map<String, ButtonClickHandler> buttonActionMap;
//Static constructor to initialize the map
static {
buttonActionMap = new Map<String, ButtonClickHandler>();
buttonActionMap.put("onButtonOneClick()",new ButtonClickHandler() {
#Override
public void onClick() {
onButtonOneClick();
}
});
buttonActionMap.put("onButtonTwoClick()",new ButtonClickHandler() {
#Override
public void onClick() {
onButtonTwoClick();
}
});
}
public void callByName(String methodName) {
final ButtonClickHandler handler = buttonActionMap.get(methodName);
if (handler == null) {
throw new IllegalArgumentException("No handler found by name: "+methodName);
}
handler.onClick();
}
}
After you call callByName("onButtonTwoClick()") it will fetch the respective instance of ButtonClickHandler which will use the static method onButtonTwoClick() to process the click of the button.
It seems to me that you are looking for the equivalent of JS "eval" function in Java. This might help. Nevertheless it is generally not a good idea as #Max stated, you might want to rethink your design.
If i have understood your question correctly you are trying to generate your code files based on some strings taken from a XML file. I can suggest you this library to generate your codes.
For tutorials you can visit this link.
You may even use the Java Reflection API. Here is a link for the tutorial.
Its upto you, that which of the above two you use.
I need to pull a JavaScript var off a site so I can use it in my code. Following this tutorial, I was able to display the string in an alert message. But what do I have to do to use the string outside of the alert message? Thanks.
EDIT: My code is basically the same as in the tutorial.
Instead of calling AlertDialog, just do something in Java with the value of the "html" parameter, unless I'm completely misunderstanding what you are asking.
String savedHtml = null;
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
#SuppressWarnings("unused")
public void showHTML(String html)
{
savedHtml = html; // this ought to work.
}
}