Re: passing value from jcombobox to other classes?



towers.789@xxxxxxxxx schrieb:

I am not quit understand you, by the way ,cann't you just create a
object of queryBuild and call its method?

I don't even have the experience to know how to do that at this stage.
I'm okay working with variables within the one class but I'm having
trouble passing them back and forth between classes and other .java
files, I think.

It doesn't matter in which file a class is defined. So, don't think about files but classes and objects.

Open a file C.java in some good old text editor of your choice. Declare a class A by typing the following into the editor:

class A {
private String x = "Initial Value";

public void setX( String x ) {
this.x = x;
}

public String getX() {
return x;
}
}

The public methods of this class (which form the interface of the class) indicate, what you need to set x in an object of A. All you need is

a) a reference to an object of A
b) and to call setX

Sure, you want to prove it. Add the following to your C.java text file:

class B {
private A a; // the reference to an object of A

public B(A a) { // constructor that takes a reference to an object
this.a = a; // of A
}

public void doit() {
a.setX("New Value");
}
}

Last, we need a third class, one that only defines the main-method. Put your hands on your keyboard and type the following:

public class C {
public static final void main( String args[] ) {
A a = new A(); // create a new instance of A
B b = new B(a); // create a new instance of B, passing the
// newly created instance of A to it.

System.out.println( a.getX() );
b.doit();
System.out.println( a.getX() );
}
}

Save C.java, compile it by executing "javac C.java". Run it by "java C" and see what happens.

After that works, let's separate the classes into different files. So, create two more files A.java and B.java, cut&paste the two classes A and B into these files and declare them to be public (public class A, public class B).

Delete the old *.class files. Then, execute "javac C.java" followed by "java C". Also, have a look at your directory - there'll be three .class files.

You should have some idea now, how to integrate this into your project.

Bye
Michael
.



Relevant Pages

  • LONG: Java student getting String Exception/String Index errors
    ... Hello, all, I'm back with more questions from my Java I class. ... and length' to edit a single long string of text. ... public void setTextFind{ ... errors indicate I'm using a number that's too large for substring ...
    (comp.lang.java.help)
  • Re: programming concepts > specific languages
    ... >> public static void manipulateObject(Object objectToManipulate) { ... >> the reference to get the address of an particular field instance (named x ... was showing how Java would behave if it were pass by reference. ... But I can't manipulate the string! ...
    (comp.programming)
  • Re: Tired of 100s of stupid Getter/Setter methods
    ... >> It is a common academic exercise, ... > String, I get a String back out. ... The reference is not typecast to ... It's like programming in Java with only Object references, ...
    (comp.lang.java.programmer)
  • Xml parser and character encoding
    ... I am new to java and I run a short program processing xml files. ... private String CData; ... public void startElement(String namespaceURI, String localName, String ...
    (comp.lang.java.programmer)
  • Re: A class that uses instances of itself? Is this right?
    ... > I was recently sent some Java code that I find VERY odd. ... > public String getText() { ... 'TypeSafe enum' pattern. ... public void aMethodDoingSomethingOnEnum{ ...
    (comp.lang.java.programmer)