Variable from one class to another

I have a Program that contains a JDesktop Pane, and when the program starts, it creates the "main" window as in JInternal Frame.

When a certain check box is checked, I call an external class that creates and returns a JInternalFrame. What I need to do is have the external class get a Vector from the main class (I pass it in when i create the object), edit the Vector, then return it to the main class. If i try to create a method in the main class like "returnVector()" or whatever to get the vector back to the main class, I get the error of "Non-static variable cannot be referenced from a non-static context". How can I get around that? The NEXT issue is this: I would like to cause the JInternalFrame that is created from the external class (on the JDesktopPane in the main class) to close itself when a button in it is clicked. I'm guess I'd need a Listener somewhere. Would that listener be in the Main class or in the external one. Does this make sense? Any help would be greatly appreciated.

[1016 byte] By [Jensenryaa] at [2007-11-27 4:11:38]
# 1
please show the smallest bit of code that:1) compiles on its own and2) demonstrates your problem
petes1234a at 2007-7-12 9:17:29 > top of Java-index,Desktop,Core GUI APIs...
# 2

I solved the 2nd question on my own. But back to the first issue: Getting the variable from the Main class to the external and back again.

From the main class (TaxiLogUI):

//This vector holds Group1 objects. Needs to be sent between the main class and the "RoadCrewFrame" class.

Vector< Group1 > roadCrewHolder=new Vector < Group1 >(0);

//I made this in an attempt to give the "RoadCrewFrame" class a way to get the Vector back to the main program.

public void returnVector(Vector<Group1> groups)

{

roadCrewHolder=groups;

}

From the RoadCrewFrame class:

private void selectButtonListener(java.awt.event.ActionEvent evt) {

if (jList1.getSelectedIndex()==-1) {

JOptionPane.showMessageDialog(this, "You must select a trip", "Error!", JOptionPane.ERROR_MESSAGE);

} else{

locations.setSize(locations.getPreferredSize());

locations.setVisible(true);

TaxiLogUI.returnVector(groups);

//This last line is what's giving me problems:"non-static method returnVector(java.util.Vector<Others.Group1>) cannot be referenced from a static context"

}

The RoadCrewFrame class returns a JInternalFrame to TaxiLogUI when its called to be displayed on the JDesktopPane. This part works fine. When I instantiate RoadCrewFrame, I include the Vector in the constructor and just pass it that way. Now The RoadCrewFrame displays the contents of the Vector in a JList. The User has the option to add to the JList or edit options on the JList. I have all that working fine. But I need to get the modified Vector back to the TaxiLogUI class. Any Ideas how I can do this? I'm not really looking for a band-aid solution, but the correct way to do it. If it means rewriting a bunch of code, so be it. Any Help would be appreciated :-)

Jensenryaa at 2007-7-12 9:17:29 > top of Java-index,Desktop,Core GUI APIs...
# 3
please show the smallest bit of code that:1) compiles on its own and2) demonstrates your problemwithout the small but compilable code, I doubt anyone will have a real clue as to what you are doing or what help you need.
petes1234a at 2007-7-12 9:17:29 > top of Java-index,Desktop,Core GUI APIs...
# 4

Sorry, This might be a bit lengthy, but it works and illustrates my problem. I created it in NetBeans. There are the two classes I'm jusing. The issue is illustrated in the button listener on the 2nd class:

The First:

package javaapplication3;

import java.beans.PropertyVetoException;

import java.util.Vector;

import javax.swing.JInternalFrame;

public class NewJFrame extends javax.swing.JFrame {

Vector <String> TestVector = new Vector <String> (0); //Vector to move around.

public NewJFrame() {

//add some test strings to the Vector

TestVector.add("String1");

TestVector.add("String2");

TestVector.add("String3");

initComponents();

}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jDesktopPane1 = new javax.swing.JDesktopPane();

jInternalFrame1 = new javax.swing.JInternalFrame();

jLabel1 = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jInternalFrame1.getContentPane().setLayout(new java.awt.FlowLayout());

jInternalFrame1.setVisible(true);

jLabel1.setText("This is a VERY simple example");

jInternalFrame1.getContentPane().add(jLabel1);

jButton1.setText("Press this");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

ButtonListen(evt);

}

});

jInternalFrame1.getContentPane().add(jButton1);

jInternalFrame1.setBounds(10, 10, 350, 120);

jDesktopPane1.add(jInternalFrame1, javax.swing.JLayeredPane.DEFAULT_LAYER);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jDesktopPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jDesktopPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)

);

pack();

}// </editor-fold>

private void ButtonListen(java.awt.event.ActionEvent evt) {

JInternalFrame newFrame;

NewJFrame2 roadframe = new NewJFrame2(TestVector);

newFrame= roadframe.getFrameBack();

newFrame.setVisible(true);

jDesktopPane1.add(newFrame);

try {

newFrame.setSelected(true);

} catch (PropertyVetoException ex) {

ex.printStackTrace();

}

}

//created this in hopes of using it to return the vector from NewJFrame2

public void returnVector(Vector<String> vectorAgain){

this.TestVector=vectorAgain;

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NewJFrame().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JDesktopPane jDesktopPane1;

private javax.swing.JInternalFrame jInternalFrame1;

private javax.swing.JLabel jLabel1;

// End of variables declaration

}

The 2nd:

package javaapplication3;

import java.util.Vector;

import javax.swing.DefaultListModel;

import javax.swing.JInternalFrame;

public class NewJFrame2 extends javax.swing.JFrame {

Vector <String> testVector=new Vector<String>(0);

DefaultListModel model;

public NewJFrame2(Vector <String> TestVector) {

this.testVector=TestVector;

initComponents();

}

public JInternalFrame getFrameBack(){

return jInternalFrame1;

}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jInternalFrame1 = new javax.swing.JInternalFrame();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jScrollPane1 = new javax.swing.JScrollPane();

model=new DefaultListModel();

for (int a=0; a<testVector.size();a++){

model.addElement(testVector.get(a));

}

jList1 = jList1=new javax.swing.JList(model);

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jInternalFrame1.setVisible(true);

jLabel1.setText("This is the 2nd class I was referencing I passed TestVector from NewFrame. It Contains:");

jInternalFrame1.getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);

jLabel2.setText("Now I want to Edit this list here, and then send it back to NewFrame1. The button will attemp.");

jInternalFrame1.getContentPane().add(jLabel2, java.awt.BorderLayout.SOUTH);

jScrollPane1.setViewportView(jList1);

jInternalFrame1.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

jButton1.setText("Return Vector");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

ReturnVectorButtonListener(evt);

}

});

jInternalFrame1.getContentPane().add(jButton1, java.awt.BorderLayout.EAST);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jInternalFrame1)

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jInternalFrame1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

);

pack();

}// ></editor-fold>

private void ReturnVectorButtonListener(java.awt.event.ActionEvent evt) {

NewJFrame.returnVector(testVector);

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JInternalFrame jInternalFrame1;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JList jList1;

private javax.swing.JScrollPane jScrollPane1;

// End of variables declaration

}

Jensenryaa at 2007-7-12 9:17:29 > top of Java-index,Desktop,Core GUI APIs...