hey there. Im using Netbeans.
i have a form where there is a button edit.
when i press it, other form opens, and the selected item on the previous page has to be edited in the databse.
So bottom line, i have to get a String / int that i used on the first form, to another form.

Is there a right way to do this?

I tried something but it didnt work... i dont know why though... maybe because i created a new one, which kinde defines the point.. coz then i dint use the one i created the first time.
This was my class, but given, i dont know if this would be the right way to do this... And i would like to use the right way.

my class

String hel = "nope didnt work";

    public void settext(String it)
    {
        hel = it;
    }
    public String gettext()
    {
        return hel;
    }

then on the first form

hspersonnelinvolvedclass set = new hspersonnelinvolvedclass();
        set.settext("this should be the new text");

the second form

hspersonnelinvolvedclass set = new hspersonnelinvolvedclass();
        JOptionPane.showMessageDialog(null, set.gettext());

So there i would get the String.. but i get the "it doesnt work" message..

Would apreciate any help.
Thanx Ruan

Recommended Answers

All 16 Replies

that are two different instances of the hspersonnelinvolvedclass-class, which means, for the instance in your second form, you never executed that setText method.

if you want them to work with one instance, you'll have to pass a reference to that instance to the second form.

that are two different instances of the hspersonnelinvolvedclass-class, which means, for the instance in your second form, you never executed that setText method.

if you want them to work with one instance, you'll have to pass a reference to that instance to the second form.

so... How do i do that? since i then totaly did it like my ass

is one of these classes used by the other?

you could refer to other similar posts aswell for help.

heres a post where i posted my opinion on cross form communication.

here

is one of these classes used by the other?

no, so far it was a simple "test" class just to see if it would work.

in which case, they don't have direct access to each others variables.
if you still want to 'interchange information', you can store that information in an xml file, and read-write it from both classes, but I doubt that's what you're looking for :)

in which case, they don't have direct access to each others variables.
if you still want to 'interchange information', you can store that information in an xml file, and read-write it from both classes, but I doubt that's what you're looking for :)

haha thanx. Coz what i have is a form that saves a person to a database. then theres a listbox that when you click on that person you can edit his details or add new details to it (wich requires the creation of a new sql table for data)
so when i click on that person in the listbox, and when you press the add button, a new form opens, and on it you can add the new details for that person. but to be able to add that i have to have that persons name i just clicked on in the listbox. =)
And later on i want to make this server side and what have you so dont know if the xml or notepads will work =)

ah, now I'm getting a bit of the bigger picture.
what you are describing is one application that consists out of a number of forms, but the code you showed us, were two independent applications.

sure, it's possible the way you describe it, but not the way you coded those examples :)

ah, now I'm getting a bit of the bigger picture.
what you are describing is one application that consists out of a number of forms, but the code you showed us, were two independent applications.

sure, it's possible the way you describe it, but not the way you coded those examples :)

haha ah yea sorry, guess i could have stated my question a lil better.
Gah, so im not on the right track... Do you think i should invest my time in figuring out how global variables work? or is there a better way?

i think i found a way to do it. its semi working now.
Because i read that its bad practice to work with global variables, so i found it on Rose Idia how to do it. I just modified their code a bit.
Thank you for your help.

ah, now I'm getting a bit of the bigger picture.
what you are describing is one application that consists out of a number of forms, but the code you showed us, were two independent applications.

sure, it's possible the way you describe it, but not the way you coded those examples :)

ahh.. i got it to Kind of work...

On my one form i have this code

JFrame mainFrame = PrelogApp.getApplication().getMainFrame();
        HSIncidentRegisterclientinvloved pass = new HSIncidentRegisterclientinvloved("Hello");
        pass.setLocationRelativeTo(mainFrame);
        PrelogApp.getApplication().show(pass);

and on my other one i have a class to get it.

HSIncidentRegisterclientinvloved(String st)

    {   
        JOptionPane.showMessageDialog(null, st);
    }

Note that the second form, has a whole gui interface. but when i press the button, it only shows me the jOptionPane with the hello message.(so it works) and then a blank screen(and then it doesnt work).. how would i modify my code to work?

ahh.. im an idiot.... Fixed it... this is how your second class should look... I forgot the initComponents();

public class mytester extends javax.swing.JFrame {

    /** Creates new form mytester */
    public mytester (String helo) {
        initComponents();
        hel=helo;
    }
     String hel;

Thank you!

as for Global variables:
it sure won't solve everything because global variables limit you to one value at the time, you'll also want to use (I assume) several ID's and instances of variables, so I think you'll be better of finding a good way to pass parameters between the forms.

as for Global variables:
it sure won't solve everything because global variables limit you to one value at the time, you'll also want to use (I assume) several ID's and instances of variables, so I think you'll be better of finding a good way to pass parameters between the forms.

Ausm, thank you. Ill remember that from now on.
Have a great day!

This question is solved

commented: no sh*t sherlock holmes -1
commented: Wow... Mr obvious is on fire today -1

This question is solved

Yes, I thought as much when I saw the "This thread is solved" comment.
What tipped you off?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.