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
}