Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new to Java and I've hit a brick wall. I want to access GUI components (that have been created in one class) from another class. I am creating a new GUI class from one class, like so;

GUI gui = new GUI();

and I can access the components in that class, but when I go to a different class I cant. I really just need to access the JTextAreas to update their content. Could someone point me in the right direction please, any help is greatly appreciated.

GUI Class:

public class GUI {

    JFrame frame = new JFrame("Server");        
    ...
    JTextArea textAreaClients = new JTextArea(20, 1);  
    JTextArea textAreaEvents = new JTextArea(8, 1);

    public GUI()
    {
        frame.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 3));     
        ...
        frame.setVisible(true);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
107 views
Welcome To Ask or Share your Answers For Others

1 Answer

First respect encapsulation rules. Make your fields private. Next you want to have getters for the fields you need to access.

public class GUI {
    private JTextField field = new JTextField();

    public GUI() {
        // pass this instance of GUI to other class
        SomeListener listener = new SomeListener(GUI.this);
    }

    public JTextField getTextField() {
        return field;
    }
}

Then you'll want to pass your GUI to whatever class needs to access the text field. Say an ActionListener class. Use constructor injection (or "pass reference") for the passing of the GUI class. When you do this, the GUI being referenced in the SomeListener is the same one, and you don't ever create a new one (which will not reference the same instance you need).

public class SomeListener implements ActionListener {
    private GUI gui;
    private JTextField field;

    public SomeListener(GUI gui) {
        this.gui = gui;
        this.field = gui.getTextField();
    }
}

Though the above may work, it may be unnecesary. First think about what exactly it is you want to do with the text field. If some some action that can be performed in the GUI class, but you just need to access something in the class to perform it, you could just implement an interface with a method that needs to perform something. Something like this

public interface Performable {
    public void someMethod();
}

public class GUI implements Performable {
    private JTextField field = ..

    public GUI() {
        SomeListener listener = new SomeListener(GUI.this);
    }

    @Override
    public void someMethod() {
         field.setText("Hello");
    }
}

public class SomeListener implements ActionListener {
    private Performable perf;

    public SomeListener(Performable perf) {
        this.perf = perf;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        perf.someMethod();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...