Stuck on Object scope in Java

Posted by ivor on Stack Overflow See other posts from Stack Overflow or by ivor
Published on 2010-03-31T11:09:36Z Indexed on 2010/03/31 11:13 UTC
Read the original article Hit count: 311

Filed under:
|

Hello, I'm working my way through an exercise to understand Java, and basically I need to merge the functionality of two classes into one app.

I'm stuck on one area though - the referencing of objects across classes.

What I have done is set up a gui in one class (test1), and this has a textfield in ie.

chatLine = new JTextField();

in another class(test2), I was planning on leaving all the functionality in there and referencing the various gui elements set up in test1 - like this test1.chatLine

I understand this level of referencing, I tested this by setting up a test method in the test2 class

public static void testpass() {

        test1.testfield.setText("hello");
    }

I'm trying to understand how to implement the more complex functionality in test2 class though, specifically this existing code;

test1.chatLine.addActionListener(new ActionAdapter() {
            public void actionPerformed(ActionEvent e) {
               String s = Game.chatLine.getText();
               if (!s.equals("")) {
                  appendToChatBox("OUTGOING: " + s + "\n");
                  Game.chatLine.selectAll();

                  // Send the string
                  sendString(s);
               }
            }
         });

This is the bit I'm stuck on, if I should be able to do this - as it's failing on the compile, can I add the actionadapter stuff to the gui element thats sat in test1, but do this from test2 - I'm wondering if I'm trying to do something that's not possible.

Hope this makes sense, I'm pretty confused over this - I'm trying to understand how the scope and referencing works.

Ideally what i'm trying to achieve is one class that has all the main stuff in, the gui etc, then all the related functionality in the other class, and target the first class's gui elements with the results etc.

Any thoughts greatly appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about oop