[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vip /vp / vr / w / wg / wsg / wsr / x / y ] [Search | Home]
4Archive logo
JAVA Help.
If images are not shown try to refresh the page. If you like this website, please disable any AdBlock software!

You are currently reading a thread in /wsr/ - Worksafe Requests

Thread replies: 11
Thread images: 1
How do I put an int variable inside a TextArea.

http://pastebin.com/mfbpud2i
>>
>>28983
ta.settext(" %d ", Integer.toString(tmp));
>>
>>28988
It gives me the exact same error.
>>
>>28996
What, java.lang.dontYouDarePostThisAnywhereException?
>>
>>28996
ta.settext( Integer.toString(tmp));

Read the API spec: https://docs.oracle.com/javase/7/docs/api/java/awt/TextComponent.html#setText(java.lang.String)
>>
>>29001
>ta.settext( Integer.toString(tmp));

Many thanks! This works.

>Read tge API spec

I'm far too much green to understand what I'm looking. Is this the sintax:
>public void setText(String t)

I don't understand why they write "public void" behind the set.Text(String t). Also the site you gave me doesn't put a "." behind the setText. Is there any guideline to know when should I put a "." ?


>>28998
It gave me:
java:74: error: method setText in class TextComponent cannot be applied to given types; ta.setText(" %d ",Integer.toString(tmp));
>>
>>29020
>I'm far too much green to understand what I'm looking.
You're looking at the declaration from the API, the way it would look if you were looking at the source code to the Java public API.

It's "public" because anyone can call it (like you're doing in your program), and "void" because that's what it returns (i.e. nothing).

Because it's declares as "public void setText(String txt)", you know that it's callable by anyone, doesn't return anything, and if you want to call it, you need to do so with exactly one String, nothing more, nothing less.

Some methods will be polymorphic: there'll be several declarations like this, one after the other, that each have different parameters.

For example, Integer.toString is polymorphic. It's declared as:

public static void toString(int i);
and
public static void toString(int i, int r);

"static" means you don't need an object to call this method on; you can call it in any context just by using "Integer.toString".

Depending on if you give it one int or two, it'll behave differently; that's polymorphism. The API documentation will tell you exactly what the various forms do.

If you learn how to read the API specification, then whenever you're wandering "what does this method do?" or "how do I call this?", you'll be able to just look it up.

It's also worth reading all the way through it, at least for the java.lang hierarchy. There's a lot of stuff in java.lang that will do a lot of the boring stuff for you, but you need to know it's there before you can use it.
>>
>>29020
As for where you put the dots, that's again the difference between static and non-static ("object") methods.

A static method doesn't belong to an object, so it doesn't get an object or a dot in front of it. For example, to call System.out.println, you just use its full name and put the parameters in the brackets.

But say you have

Integer i = new Integer(5);
System.out.println(i.toString());
System.out.println(Integer.toString(5));

toString is polymorphic.

The first time we call it, it's a non-static method, so we take the object we're calling it on (i), and then add a dot and the method name, then the parameters in brackets. In this case there aren't any parameters, because the object i already knows what object it is, and that's all it needs to know to give you its value as a string.

The second time we call it, it's a static method, so no object and no dot on the front, just like System.out.println() . Because it's static and there's no object, we have to give it the thing we want the value of, in this case "5", which is an int literal.
>>
>>29161
>>29171

Have you wrote a book? That is a damn good explanation. Many thanks.

I have stumbled with a new problem. I was sure that I could handle this exercise by my self if I only learned how to write and bring in variables inside a TextArea. I'm doing a multiplication table when I write the number on the TextField and then press Calc. I used a for cycle and it only shows up the latest count, meaning the times ten multiplication. Any help?
>>
>>29185
So you have one textarea and you want it to show several lines of text, but it only shows one?

It's doing this because setText overwrites whatever is in the textbox.

So you'd want to

TextArea t;
String line;
t.setText(""); // clear textbox
for (...) {
line=calculateline();
t.append(line); // append doesn't clear the textarea
t.append("/n"); // "/n" is an escape sequence that (in a string literal) gets replaced with a newline
}

A more advanced technique that saves allocating objects in a loop and updating the textarea loads of times would be to construct the contents in a StringBuffer and then send it to the textarea once.
>>
>>29202

Sorry I'm having a hard time installing this piece of code. Anyway, thanks. I'm going to bed.
Thread replies: 11
Thread images: 1
Thread DB ID: 387829



[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vip /vp / vr / w / wg / wsg / wsr / x / y] [Search | Home]

[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vip /vp / vr / w / wg / wsg / wsr / x / y] [Search | Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the shown content originated from that site. This means that 4Archive shows their content, archived. If you need information for a Poster - contact them.
If a post contains personal/copyrighted/illegal content, then use the post's [Report] link! If a post is not removed within 24h contact me at [email protected] with the post's information.