medzi
medzi

Reputation: 407

How can I pass data from another class to a GUI?

How do I pass data/values from one class to another using GUI? I'm trying to pass the message2 array to namesOut label in GUI. I'm stuck and getting an error.

Here's my code:

GUI

package testClassesGUI;

import java.awt.BorderLayout;

public class UI extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UI frame = new UI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public UI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDisplayOutNames = new JLabel("Display out names:");
        lblDisplayOutNames.setBounds(32, 25, 121, 16);
        contentPane.add(lblDisplayOutNames);

        JLabel namesOut = new JLabel(""); //here i need to bring the data
        namesOut.setBounds(32, 63, 228, 87);
        contentPane.add(namesOut);
    }
}

Logic:

Here I'm getting an error.

package testClassesGUI;

public class Logic {

    private String[] someArray = { "Great", "World" };



    // getter method
    public String[] message2(){
        return someArray;
    }


    // setter method
    public void setSomeArray(String[] someArray){
        this.someArray = someArray;
    }

    UI logicObject = new UI();
    logicObject.namesOut.setText(message2); //here my error misplaced construct(s), variable declaratoridexpected
}

Your help is much appreciated.

Upvotes: 0

Views: 4322

Answers (3)

Paul Samsotha
Paul Samsotha

Reputation: 208994

Put this in your UI constructor. You should create a Logic object in it

    public UI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDisplayOutNames = new JLabel("Display out names:");
        lblDisplayOutNames.setBounds(32, 25, 121, 16);
        contentPane.add(lblDisplayOutNames);

        JLabel namesOut = new JLabel(""); //here i need to bring the data
        namesOut.setBounds(32, 63, 228, 87);
        contentPane.add(namesOut);

        Logic logic = new Logic();           <<---
        String[] array = logic.message2();       |
                                                 |
        String s = "";                           |
        for (String str : array){                |
            s += str + " ";                      |
        }                                        |
                                                 |
        namesOut.setText(s);                <<----
    }

You can delete this from your Logic class

UI logicObject = new UI();
logicObject.namesOut.setText(message2);

Upvotes: 2

Brierson
Brierson

Reputation: 96

You have to define a function. Maybe a constructor:

Logic () {
    logicObject.namesOut.setText(message2);
}

Also you can execute inside a code block, but it's not usual:

{
    logicObject.namesOut.setText(message2);
}    

Upvotes: 0

stinepike
stinepike

Reputation: 54682

namesOut is a local variable declared in the constructor of UI. you cannot access it from Logic. declare it as a public member variable

public JLabel namesOut;

Upvotes: 0

Related Questions