Skip to content
Advertisement

In Java how do you pass variables and lists between jframe components

I have created a simple program with a list object. I also have a simple UI and want to add the values from the different parts of the list to the text frames and labels but cannot find how to pass the values that are needed.

The list object has been created in the main class of the UI (which was created using the GUI builder in Netbeans).

I have tried accessing the value that is needed in the jButton action performed section, but am told that package exampleProgram does not exist.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{
    jTextField1.setText(exampleProgram.exampleClass1.get(0).number3);
}                                        

public static void main(String args[]) 
{
    ExampleProgram exampleProgram = new ExampleProgram();
    exampleProgram.exampleClass1.add(new ExampleClass(5,4,3,2,1));
    System.out.println("The example value (which should be 3) is : " + exampleProgram.exampleClass1.get(0).number3);

    java.awt.EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {
            new ExampleUI().setVisible(true);
        }
    });
}

The two other files in the package currently are ExampleProgram

package classtest;

import java.util.ArrayList;
import java.util.List;

public class ExampleProgram 
{
    List<ExampleClass> exampleClass1 = new ArrayList<>();
    List<ExampleClass> exampleClass2 = new ArrayList<>();
    List<ExampleClass> exampleClass3 = new ArrayList<>();
}

And ExampleClass

package classtest;

public class ExampleClass 
{
    int number1;
    int number2;
    int number3;
    int number4;
    int number5;

    public ExampleClass (int a, int b, int c, int d, int e) 
    {
        number1 = a;
        number2 = b;
        number3 = c;
        number4 = d;
        number5 = e;
    }
}

UPDATE:

Tried suggested answer – works when using the constructor, but still not able to access the passed object in the buttons action code.

public class ExampleUI extends javax.swing.JFrame 
{
    public ExampleUI()
    {
        initComponents();
    }

    public ExampleUI(ExampleProgram passedObject)
    {
        initComponents();
        jTextField1.setText(String.valueOf(passedObject.exampleClass1.get(0).number3));
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {                                         
        jTextField2.setText(String.valueOf(passedObject.exampleClass1.get(0).number3));
    }                                        

    public static void main(String args[])
    {
        ExampleProgram exampleProgram = new ExampleProgram();
        exampleProgram.exampleClass1.add(new ExampleClass(5,4,3,2,1));
        System.out.println("The example value (which should be 3) is : " + exampleProgram.exampleClass1.get(0).number3);

        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new ExampleUI(exampleProgram).setVisible(true);
            }
        });
    }
}

Advertisement

Answer

    public static void main(String args[]) 
{
    ExampleProgram exampleProgram = new ExampleProgram();
    exampleProgram.exampleClass1.add(new ExampleClass(5,4,3,2,1));
    System.out.println("The example value (which should be 3) is : " + exampleProgram.exampleClass1.get(0).number3);

    java.awt.EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {
            new ExampleUI(exampleProgram).setVisible(true);
        }
    });
}

Pass data in the constructor of ExampleUI class then access it in ExampleUI class. Hope this will help you.

Advertisement