Java send data to another class

on this Java road I am hitting some big bumps, this one, which seems so simple, is just not possible for me to work out on my own.

I have searched the wild west internet and as usual there is just too much information and I have no idea which bit is correct, and none of the options I tried have worked for me.

so to the issue.

in xojo I have a GUI, in that GUI I have a textarea.
in a method in the WINDOW I do this:-

textarea1.Text = “HOLA”//inside the method which is part of the WINDOW

there is a BUTTON that calls this method and prints the text.

perfect…

I then make a MODULE or CONTAINER, whatever, something not within the WINDOW immediate scope.

so I make another method in this container called ‘Print_Hello()’

I then make the BUTTON call ‘PrintHello()’ and this code runs:-

WINDOW.textarea1.Text = “Hello”

so two issues to solve, one is get the button to call a function in a different ‘class’ (I think its a class) and then that function to reference the textarea and set its TEXT parameter.

my OOP is not up to speed and the terminology in Java is above my current understanding, a nice example would be super fine so I can move on up the java food chain.

and before any messages tell me I should not code like the above, ITS JUST AN EXAMPLE TO SHOW THE POINT.

thank you in advance.

Can you post the sources please?

In Xojo this is possible but I would try to dissuade you from doing it

What you might do instead is something like

Sub PrintHello(txtArea as textArea)
     txtArea.Text = “Hello”
End Sub

and now THIS code can be used with ANY text area on any window

AND, if you do the same in Java, the same style works

so I would have thought Norm, but it seems less simple.

I have got (by accident after trying all sorts) something that does what I am trying to do right now (as an example of the process, not actually wanting to do in a project).

I have what feels like a hack which I would think is NOT OOP friendly.

Java seems to be totally OOP in the examples I have seen, none of which I understand.

this is a Netbeans project file and requires nothing special to run, except Netbeans…and Java.

ok, the upload will not accept zip files so I have no idea how to add a normal file to this message, so pointless post really.

You can add a property to the container to hold a reference to the window and initialization sub to send the instance :thinking:

Use getters and setters, google for it you will find is or send me the project to code@stueker.org and I’ll write them for you as tutorial

that was what I was trying to do but can not work out the syntax.

ok Thorsten, doing that right now

you wrote a setter already but the other way around. Place the setters ALWAYS and under ALL CIRCUMSTANCES and under HIGH IMPORTANCY in the Class where the GUI Form resides. IMPORTANT!!! When not doing it it is complex.

For the setter place it so that it has an argument String so that you can call the setter from everywhere with it’s argument String.

The Getter returns. This is for example a simple method where you would get the Text for example with get text() and return this to the caller.

further to you Norm, I have the process you outlined up and running with this syntax:-

public static void txt_fill(String s)
    {
        TA_Debug.append(s + "\n");
    }

the call:-

myClass.txt_fill("printing from a different class")

does work, BUT only if I have the textarea declared thus:-

private static javax.swing.JTextArea TA_Debug;

now that may be ok for making the code work, but my concern (due to not seeing a single sample of code anywhere that does this) is that its not the ‘correct’ way to do it in Java.

but I just don’t know!

the STATIC keyword is obviously the reason it works, what I am unable to figure out how to do this as OOP in Java.
that is, creating the instance of the target class in the class I am writing the code in, its just not obvious to me from all the examples I have seen how its supposed to work…

public static String getit(){

    String wahaa = TA_Debug.getText();
    return wahaa;
    
    
}

this would be your getter

A few hints:

Namings are important to make Code readable. So TA_debug is not really readable. TA_textarea would be. All you can place in the class where the object is what belongs as functionality to the class place also in that class while it makes it accessible and readable.

Setting the Variables of the Form static makes them accessible also from other classes. If you do not do that it is impossible to reach them from outside. That exactly is the sense behind. Private is private and not accessible from outside, public is accessible but only when accessing the right object (what you can identify over the this keyword. So why you need under this circumstances an instance of a static variable??? Otherwise you have to write different code.

thanks for the info, I have seen the getter and setter methods in some examples but they never worked because the textarea.setText() line was always in error.
if I make the textarea STATIC it works.

is this ok to do in Java, make a GUI object STATIC or should it have an instance created and that used to address the target GUI element?

having written code for 40 or more years I know naming is important, VERY important, that is WHY TA_xxx is named this, for me TA_ is a TEXTAREA and debug is what its for, to me TA_debug describes exactly what is targeted and what its function is, to me at least.
that is part of my personal format and it works for me.

I am going to put all this into the code now and see if its all working, but I just do not like the idea that the textarea is STATIC.

the different code is what I want to find out how to write, I have seen code that uses an instance of the class and sets the element writable for that instance so you can access any private part of any class, that is what I was trying to do.

this was what I saw that almost made sense to me but still not completely understood.

I think @doj is using hungarian notation for the variables where TA_ stands for TextArea.
TA_Debug would refer to a TextArea (called) Debug. TA_textarea would be confusing.

But what he tries to do is to modify private Swing variables. Instancing Swing is another thing than instancing variable classes. That is not that complex. Building a class with private variables and instancing them builds a new object with the accessibility of the instance. that means it is an entire empty instances which inherits the parent class parameters

yes, although I did not know it was any specific named notation format.

I always do this:-
Btn_xxx for button
TA_xxx for textarea
TF_xxx for textfield
flag_stop_this
flag_start_that

if a MODULE is called Messages then every function is:-
msg_read
msg_write

and you can guess the rest.

thats my format and I expect 50% to hate it 50% to not care.

okay. more important is object instancing. you could build a new instance of the button but that would not affect the shown instance.

Hi Thorsten,

My aim is just to learn how to do all the Java things correctly from the very start.

I am only learning it once and right now I simply do not know what would be accepted as the ‘correct’ way to do anything in Java.

it is easy to make code work, but I want to do it correctly, following a convention that I am not familiar with.

this is very difficult when you first try to lean a new language!

I am now going to try the ideas from the messages above to see how it looks.

Thanks for all input.