Problems with transfering data between frames

Hi, im pretty new to developing java applications and programming in general.

Im currently working on a java front end for a database (access backend). Ive created several panels to add "employees", "projects" etc. which ive mounted on a jFrame when needed.

The problem that i am facing is that on my "Projects" panel i have a button that creates an new jFrame which contains a jTable with data that i have queried the database. That all works fine but when i select a row of the jTable , get the relevant data, then click on a button "OK", how do i transfer that data, say in an array, back to the class that has invoked the second frame ?

I've been working on this prolem for some time and my brain is fried so hopefully i have made some sense.

Prits

[786 byte] By [Pritsa] at [2007-11-26 21:27:14]
# 1
Post the code you have? Generally, you return something from a method. In some cases you pass the class reference containing the member data. Depends on a number of things.
abillconsla at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 2

you have the data in, say, an array

you want a new JFrame to appear, and render this data on a table

you need to pass this array into an instance of your UI component (the jframe)

simple: write a constructor for your UI component, which takes the array in question as a parameter. you could also write a setter method to do this

georgemca at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 3
Kind of sounds like he wants the data to flow in the opposite direction -> back to the first "frame". In either case, though, same idea ... passing data to a method as an argument of a setter.
abillconsla at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 4

> Kind of sounds like he wants the data to flow in the

> opposite direction -> back to the first "frame". In

> either case, though, same idea ... passing data to a

> method as an argument of a setter.

yeh, it does sound like that, doesn't it. @OP: do a bit of reading of tutorials, get the hang of passing objects backwards and forwards between other objects, via method calls. forget about specifically this problem for now, the aspects such as databases and Swing are probably making it seem a lot more complex than it actually is. us explaining how to solve this exact problem won't help you understand what is, after all, vitally important for you to know. once you've done that, solving this problem will be a complete no-brainer :-)

georgemca at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 5
Realize that "GUI problems" are just "problems". Suppose you have two objects,the first referred to projects and the other to employees. How could you pass an employeeback from the second object back to the first?
DrLaszloJamfa at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 6
use web services to do this.
mkoryaka at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 7
> use web services to do this.Don't make me get out the stick again :-)stick | V===================================== ^ |wrong end
DrLaszloJamfa at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 8
> use web services to do this.ah, you want to hang that picture on your bedroom wall? right, first we need my JCB to move the wall.....
georgemca at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 9

Thats what i cant figure out. If i create an instance of say, class 2 from class 1. how do i reference class 1 from 2 without creating another instance of it ?

I realise that within the first class i can have a getMethod but how would i know when i user has selected certain data, in this case selected an OK button?

im sure theres a really simple answer but my mind keeps going over the same thing!

some code snippits:

from my addProjetPanel, which gets mounted on my main application frame, this is the methos which invokes the second frame

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

//create a connection to the database, contains methods with queries

db dbase = new db();

//create the frame which allows you to select employess

selectDataFrame sdf = new selectDataFrame();

sdf.setFrameTitle("Select Member");

//fill the table in the frame with employess from the getMembers query

sdf.fillTable(dbase.getMembers());

sdf.setVisible(true);

from my select employess frame, Note. ive used netbeans hence all the other ****. Im a bit stuck as what to do in the OKbutton listener method.

/*

* selectMemberFrame.java

*

* Created on March 8, 2007, 2:17 PM

*/

import javax.swing.*;

import javax.swing.table.*;

import java.sql.*;

import java.util.Vector;

import java.awt.*;

/**

*

* @author Admin

*/

public class selectDataFrame extends javax.swing.JFrame {

/** Creates new form selectMemberFrame */

public selectDataFrame() {

try {

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

} catch (InstantiationException ex) {

ex.printStackTrace();

} catch (UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

} catch (IllegalAccessException ex) {

ex.printStackTrace();

}

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

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

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();

jTable = new javax.swing.JTable();

jLabel1 = new javax.swing.JLabel();

okButton = new javax.swing.JButton();

cancelButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setAlwaysOnTop(true);

jTable.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

{}

},

new String [] {

}

));

jScrollPane1.setViewportView(jTable);

jLabel1.setText("Select the required row and click OK");

okButton.setText("OK");

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

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

okButtonActionPerformed(evt);

}

});

cancelButton.setText("Cancel");

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

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

cancelButtonActionPerformed(evt);

}

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 390, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel1)))

.addGroup(layout.createSequentialGroup()

.addGap(125, 125, 125)

.addComponent(okButton)

.addGap(22, 22, 22)

.addComponent(cancelButton)))

.addContainerGap(15, Short.MAX_VALUE))

);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addGap(24, 24, 24)

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(16, 16, 16)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(cancelButton)

.addComponent(okButton))

.addContainerGap())

);

pack();

}// </editor-fold>

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

int row;

//find the value of the selected row

row = jTable.getSelectedRow();

//if a row has been selected row will be > 0, else display msg to select row

if(row > 0)

{

}

}

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

//close the window when cancel is pressed

this.dispose();

}

//method to set the title of the JFrame

public void setFrameTitle(String title)

{

setTitle(title);

}

public void fillTable(ResultSet rs)

{

try

{

ResultSetMetaData meta = rs.getMetaData();

//retrieve the number of columns in the resultset

int cols = meta.getColumnCount();

//create an array to hold the column names

String colNames[] = new String[cols];

//go through the metadata results and put column names in array

for(int i = 0; i < cols; i++)

{

colNames[i] = meta.getColumnName(i+1);

}

rs.last();

int numOfRows = rs.getRow();

rs.first();

//create a 2d array to hold the data that is to populate the table

String data[][] = new String[numOfRows][cols];

//scan through the result set and place values in the array

int j = 0; //used for loop counter

do

{

for(int i = 0; i < cols; i++)

{

data[j][i] = rs.getString(i+1);

}

j++;

} while(rs.next());

//declare the default TableModel, the data and the column names

TableModel model = new DefaultTableModel(data,colNames)

//set the cells to be non-editable

{

public boolean isCellEditable(int rowIndex, int columnIndex) {

return false;

}

};

//apply the table model to the JTable itself

jTable.setModel(model);

//set selection mode so that only one row can be selected

jTable.setSelectionMode(0);

//add facilties to sort the table

RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);

jTable.setRowSorter(sorter);

}//end of try

catch(SQLException ex){ex.printStackTrace();}

}//end of procedure

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

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

public void run() {

new selectDataFrame().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton cancelButton;

private javax.swing.JLabel jLabel1;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTable jTable;

private javax.swing.JButton okButton;

// End of variables declaration

}

Pritsa at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 10

Ok so ive worked out that i need to send a reference of the class that is invoking the frame to the class frame,

selectDataFrame sdf = new selectDataFrame(this);

But in the selectDataFrame itself, i have to delcare the class that is invoking the frame as a paramter, in this case addProjectPanel:

public class selectDataFrame extends javax.swing.JFrame {

addProjectPanel parent;

/** Creates new form selectMemberFrame */

public selectDataFrame(addProjectPanel app)

{

parent = app;

The way i have designed the selectDataFrame is so that any results of a query can be put in the jTable and i was planning on using the frame from multiple classes. So how can i do this without having to specifically state a class in the parameters as adding addProjectPanel to the parameter restricts it to being called from the addProjectPanel class only ?

Pritsa at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 11

Depends on if you are using Generics or not. Without:

public selectDataFrame(Object o) {

If ( o instanceof addProjectPanel )

addProjectPanel app = (addProjectPanel)o;

/* do this */

else if ( o instanceof SomeOtherPanelOrClass )

addProjectPanel app = (SomeOtherPanelOrClass)o;

/* do that */

else

/* do something else yet */

}

Perhaps someone else will handle the generics version of the code.

If all your classes are extentions (sub-classes) of JPanel, then you can define your constructor as only accepting JPanel Objects instead of Object.

You should not start the name of a class in lower case - as is the case with addProjectPanel.

abillconsla at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 12
thanks abillconsl!exactly what i was after.
Pritsa at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...
# 13
Sure thing.
abillconsla at 2007-7-10 3:07:45 > top of Java-index,Java Essentials,New To Java...