passing value from jcombobox to other classes?

Discussion in 'Java' started by towers.789@gmail.com, Feb 3, 2007.

  1. Guest

    Hi

    I have an Jcombobox with an ActionListener set up in a class called
    "boxSelected", so that when the user clicks on the choice they're
    after the result is sent to a jtext area.


    public class boxlistener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==queryBox){
    int selectedIndex =
    queryBox.getSelectedIndex();
    String boxSelected =
    queryBox.getSelectedItem().toString();
    System.out.println("Selected:
    "+boxSelected);

    String newline = "\n";

    textArea.append(boxSelected + newline);

    }

    }

    };


    The class with the ActionListener, boxlistener, is actually nested
    within another class, one called Editor.

    My question is, can I pass this new string value, boxSelected, for use
    in another class? I'm trying to create a new .java file with a
    queryBuilder class and pass this string value for use with that class
    there, so I can parse it into some queries.

    I am still pretty new. Can anyone show me how to do this, or show me
    what code I need? Any help would be really appreciated.

    Regards,

    John
     
    , Feb 3, 2007
    #1
    1. Advertising

  2. Guest

    On 2ÔÂ3ÈÕ, ÏÂÎç5ʱ09·Ö, wrote:
    > Hi
    >
    > I have an Jcombobox with an ActionListener set up in a class called
    > "boxSelected", so that when the user clicks on the choice they're
    > after the result is sent to a jtext area.
    >
    > public class boxlistener implements ActionListener {
    >
    > public void actionPerformed(ActionEvent e) {
    > if(e.getSource()==queryBox){
    > int selectedIndex =
    > queryBox.getSelectedIndex();
    > String boxSelected =
    > queryBox.getSelectedItem().toString();
    > System.out.println("Selected:
    > "+boxSelected);
    >
    > String newline = "\n";
    >
    > textArea.append(boxSelected + newline);
    >
    > }
    >
    > }
    >
    > };
    >
    > The class with the ActionListener, boxlistener, is actually nested
    > within another class, one called Editor.
    >
    > My question is, can I pass this new string value, boxSelected, for use
    > in another class? I'm trying to create a new .java file with a
    > queryBuilder class and pass this string value for use with that class
    > there, so I can parse it into some queries.
    >
    > I am still pretty new. Can anyone show me how to do this, or show me
    > what code I need? Any help would be really appreciated.
    >
    > Regards,
    >
    > John


    I am not quit understand you, by the way ,cann't you just create a
    object of queryBuild and call its method?
     
    , Feb 3, 2007
    #2
    1. Advertising

  3. Guest


    > 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.

    Regards,

    John
     
    , Feb 3, 2007
    #3
  4. 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
     
    Michael Rauscher, Feb 3, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    5
    Views:
    1,226
    Roedy Green
    Feb 13, 2006
  2. Tony Johansson

    Nested classes within other classes

    Tony Johansson, Dec 14, 2004, in forum: C++
    Replies:
    2
    Views:
    495
    msalters
    Dec 14, 2004
  3. Replies:
    7
    Views:
    578
    Alex Hunsley
    Aug 12, 2006
  4. Hal Vaughan
    Replies:
    7
    Views:
    1,463
    Hal Vaughan
    Sep 16, 2006
  5. Mariano
    Replies:
    15
    Views:
    1,914
    Michael Rauscher
    Mar 27, 2007
Loading...

Share This Page