Results 1 to 3 of 3
  1. #1
    Graham is offline Member
    Join Date
    Oct 2013
    Location
    United States
    Posts
    4
    Rep Power
    0

    Default Maintaining access to objects in other classes.

    I'm trying to use "getters" and "setters" to move object values between classes and I'm missing something.
    I can use them successfully to move a string value between a textBox and a textArea in a Swing GUI
    (same class "Main") but can't figure out how to grab the string value of an object that is calculated by
    another class into the textArea in the GUI. It's obviously not accessible for some reason and I'm just not
    sure how to either make the object value global or to directly port it from the calculating class to the
    GUI (Main) class?? I want the GUI to be the sole UI and use the textBoxes and textAreas as I/O for
    all the modules in the system. Any ideas??

    Thanks
    Graham
    Last edited by Graham; 10-12-2013 at 12:20 AM. Reason: Typos

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Maintaining access to objects in other classes.

    Post your code. If it is too long then create a SSCCE.

    One class needs to hold a reference to the second class.
    Java Code:
    class Foo {
        private String text;
    
        Foo(String s) {
            text = s;
        }
    
        public String getText() {
            return text;
        }
    }
    
    class Bar {
         Foo obj;
    
        Bar(Foo f) {
            obj = f;
        }
    
        public void doStuff() {
            System.out.println(obj.getText());
        }
    
        public static void main(String[] args) {
            Foo fooey = new Foo("Hello World");
            Bar barr = new Bar(fooey);
            barr.doStuff();
        }
    }

  3. #3
    Graham is offline Member
    Join Date
    Oct 2013
    Location
    United States
    Posts
    4
    Rep Power
    0

    Default Re: Maintaining access to objects in other classes.

    OK, here tis'

    This piece is from the GUI that I use to trigger events. I can start other classes with it. I'm trying to grab variables generated
    by other classes and dump them into a textArea I will use as a message field for the user. All actions will be triggered by and
    seen on the GUI.

    This code prints input in a textField into a textArea in the same (GUI) class when I press the button.
    This works fine.


    JButton btnLogin = new JButton("LOGIN");
    btnLogin.setHorizontalTextPosition(SwingConstants. CENTER);
    btnLogin.setFont(new Font("Tahoma", Font.BOLD, 12));
    btnLogin.setBounds(166, 118, 89, 23);
    contentPane.add(btnLogin);

    // Create Action Listener for Login Button

    btnLogin.addActionListener (new ActionListener()

    {
    public Object TextField;
    public Object TextArea;
    //public Object Counter;

    public void actionPerformed(ActionEvent e) {

    // Create Action Event Handler for Login button
    System.out.println(" You pressed Login button and entered the following text");


    //Grab text from TextBox "nameField"
    String TextField = nameField.getText();

    //Import text from "nameField" and port it to TextArea "outputField"
    //Left to right, go and get it and haul it back.
    outputField.setText(nameField.getText());
    System.out.println(TextField);

    System.out.println("Done");
    }});

    Here is the variable in the other class "WipTemplateGenerator"


    public class WipTemplateGenerator extends MainGui

    {
    public static void TemplateGenerator(Object outputField) throws IOException {


    Variable "counter" is generated with some code such that it can be printed locally to the console with

    System.out.println(" The template is "+ counter);

    Works fine.. I can launch WipTemplateGenartor with the GUI but can't recover the variable.

    Any idea what I'm missing?

Similar Threads

  1. Objects and Classes
    By dharav in forum New To Java
    Replies: 1
    Last Post: 10-24-2012, 11:47 PM
  2. OOP Objects and Classes help
    By richlan in forum New To Java
    Replies: 5
    Last Post: 03-05-2011, 05:50 PM
  3. Classes and Objects Help
    By collin389 in forum New To Java
    Replies: 1
    Last Post: 12-14-2009, 12:44 AM
  4. classes as objects
    By kroiz in forum New To Java
    Replies: 4
    Last Post: 07-25-2009, 05:22 AM
  5. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •