Issue
The following is my menu class. I want to obtain values from the masterdetails class's textfields when i click the Save button. How do i do? I have tried creating an object of the second class and using the getText() to retrieve data but I am not able to get it somehow
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane.*;
import java.awt.event.*;
public class menu extends JFrame implements ActionListener
{
public JMenu jb1,jb7,jb8;
public JPanel panel;
public ImageIcon i,j;
public JFrame jf;
private JMenuBar menubar=new JMenuBar();
String str = null;
String str1=null;
public menu()
{
setTitle("Main_Screen");
setJMenuBar(menubar);
jb1=new JMenu("File");`enter code here`
jb7=new JMenu("Details");
jb8=new JMenu("Report");
JMenuItem e=new JMenuItem("Exit");
menubar.add(jb1);
menubar.add(jb7);
menubar.add(jb8);
JMenuItem jm1=new JMenuItem("Save");
jb1.add(jm1);
JMenuItem jm3=new JMenuItem("Save As");
jb1.add(jm3);
JMenuItem jm4=new JMenuItem("Update");
jb1.add(jm4);
JMenuItem jm5=new JMenuItem("Retrieve");
jb1.add(jm5);
JMenuItem jm6=new JMenuItem("Exit");
jb1.add(jm6);
JMenuItem jm8=new JMenuItem("Master Details");
jb7.add(jm8);
JMenuItem jm12=new JMenuItem("Performance Testing");
jb7.add(jm12);
JMenuItem jm13=new JMenuItem("Raw Materials");
jb7.add(jm13);
JMenuItem jm81=new JMenuItem("Generate");
jb8.add(jm81);
panel=(JPanel)getContentPane( );
panel.setLayout(new BorderLayout());
JLabel lb1=new JLabel("",i,JLabel.CENTER);
panel.add(lb1,BorderLayout.CENTER);
e.addActionListener(this);
jm1.addActionListener(this);
jm3.addActionListener(this);
jm4.addActionListener(this);
jm5.addActionListener(this);
jm6.addActionListener(this);
jm13.addActionListener(this);
jm12.addActionListener(this);
jm8.addActionListener(this);
jm81.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
String str=a.getActionCommand();
if(str.equals("Master Details"))
{
masterdetails f=new masterdetails();
f.setSize(800,600);
f.setVisible(true);
f.show();
}
if(str.equals("Save"))
{
masterdetails m=new masterdetails();
//m.setSize(800,600);
//m.setVisible(true);
//m.show();
}
}
public static void main(String args[])
{
menu m=new menu();
m.setSize(800,600);
m.setVisible(true);
m.show();
}
}
The Master details class looks as follows.
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.awt.*;
import javax.swing.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wb.swt.SWTResourceManager;
public class masterdetails extends JFrame implements ActionListener {
public JPanel panel;
public JLabel title,lblnumber,lblPSINo,lblmodel,lblcapacity,lblpressure,lblplungerdia;
public JLabel lblnostrk,lblclient,lblpono,lblsize,lblstrokelen;
public JLabel lblmotorhp,lblmotorrpm,lblmotorno,lblmake,lblpumptagno,lblitemno,lbldate;
public JButton b1;
public String number1="";
public JTextField number,PSINo,model,capacity,pressure,plungerdia;
public JTextField nostrk,client,pono,size,strokelen;
public JTextField motorhp,motorrpm,motorno,make,pumptagno,itemno;
DateTime date;
Font g;
String str=null;
public masterdetails()
{ g=new Font("",Font.BOLD,18);
panel=(JPanel)getContentPane();
panel.setLayout(null);
title=new JLabel("Master Details");
title.setFont(g);
title.setBounds(325, 10, 150, 40);
panel.add(title);
lblnumber=new JLabel("No.");
lblnumber.setBounds(10, 57, 87, 21);
number=new JTextField("");
number.setBounds(121, 57, 116, 21);
panel.add(lblnumber);
panel.add(number);
lbldate=new JLabel("Date");
lbldate.setBounds(10, 97, 87, 21);
panel.add(lbldate);
lblPSINo=new JLabel("Pump SI No.");
PSINo=new JTextField("");
PSINo.setBounds(121, 137, 116, 21);
lblPSINo.setBounds(10, 137, 87, 21);
panel.add(lblPSINo);
panel.add(PSINo);
lblmodel=new JLabel("Model");
model=new JTextField("");
model.setBounds(121, 175, 116, 21);
lblmodel.setBounds(10, 177, 87, 21);
panel.add(lblmodel);
panel.add(model);
lblcapacity=new JLabel("Capacity");
capacity=new JTextField("");
lblcapacity.setBounds(10, 217, 87, 21);
capacity.setBounds(121, 213, 116, 21);
panel.add(lblcapacity);
panel.add(capacity);
lblpressure=new JLabel("Pressure");
pressure=new JTextField("");
lblpressure.setBounds(10, 257, 87, 21);
pressure.setBounds(121, 252, 116, 21);
panel.add(lblpressure);
panel.add(pressure);
lblplungerdia=new JLabel("Plunger Dia.");
plungerdia=new JTextField("");
lblplungerdia.setBounds(10, 297, 87, 21);
plungerdia.setBounds(121, 291, 116, 21);
panel.add(lblplungerdia);
panel.add(plungerdia);
lblnostrk=new JLabel("No.of Strk/Min");
nostrk=new JTextField("");
lblnostrk.setBounds(10, 337, 87, 21);
nostrk.setBounds(121, 330, 116, 21);
panel.add(nostrk);
panel.add(lblnostrk);
lblpumptagno=new JLabel("Pump Tag No.");
pumptagno=new JTextField("");
pumptagno.setBounds(121, 369, 116, 21);
panel.add(pumptagno);
lblpumptagno.setBounds(10, 369, 87, 21);
panel.add(lblpumptagno);
lblclient=new JLabel("Client.");
client=new JTextField("");
client.setBounds(430, 57, 214, 21);
panel.add(client);
lblclient.setBounds(335, 57, 87, 21);
panel.add(lblclient);
lblpono=new JLabel("P.O. No.");
pono=new JTextField("");
pono.setBounds(430, 96, 214, 21);
panel.add(pono);
lblpono.setBounds(335, 96, 87, 21);
panel.add(lblpono);
lblsize=new JLabel("Size");
size=new JTextField("");
size.setBounds(430, 137, 214, 21);
panel.add(size);
lblsize.setBounds(335, 137, 87, 21);
panel.add(lblsize);
lblstrokelen=new JLabel("Stroke Length");
strokelen=new JTextField("");
strokelen.setBounds(430, 175, 214, 21);
panel.add(strokelen);
lblstrokelen.setBounds(335, 175, 87, 21);
panel.add(lblstrokelen);
lblmotorhp=new JLabel("Motor H.P.");
motorhp=new JTextField("");
motorhp.setBounds(430, 213, 214, 21);
panel.add(motorhp);
lblmotorhp.setBounds(335, 213, 87, 21);
panel.add(lblmotorhp);
lblmotorrpm=new JLabel("Motor RPM");
motorrpm=new JTextField("");
motorrpm.setBounds(430, 252, 214, 21);
panel.add(motorrpm);
lblmotorrpm.setBounds(335, 252, 87, 21);
panel.add(lblmotorrpm);
lblmotorno=new JLabel("Motor No.");
motorno=new JTextField("");
motorno.setBounds(430, 291, 214, 21);
panel.add(motorno);
lblmotorno.setBounds(335, 291, 87, 21);
panel.add(lblmotorno);
lblmake=new JLabel("Make");
make=new JTextField("");
make.setBounds(430, 330, 214, 21);
panel.add(make);
lblmake.setBounds(335, 330, 87, 21);
panel.add(lblmake);
lblitemno=new JLabel("Item No.");
itemno=new JTextField("");
itemno.setBounds(430, 369, 214, 21);
panel.add(itemno);
lblitemno.setBounds(335, 369, 87, 21);
panel.add(lblitemno);
b1=new JButton("Next");
b1.setBounds(671, 507, 75, 25);
panel.add(b1);
b1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent a) {
// TODO Auto-generated method stub
String str=a.getActionCommand();
if(str.equals("Next"))
{
performancetest f=new performancetest();
f.setSize(800,600);
f.setVisible(true);
f.show();
}
}
public static void main(String args[])
{
masterdetails f=new masterdetails();
f.setVisible(true);
f.setSize(800,600);
f.show();
}
}
Solution
I would suggest the in the masterdetails
class you add a public method getDetails()
which will return the values of the text fields as an array or object (Whatever you wish).
Now in the actionPerformed
method of the menu
class extract the local variable f
outside the if statement and initialise as null
. now in if(str.equals("Save"))
section first check whether f
is not null
and call f.getDetails()
to get the values of the text fields to do whatever you want to do.
Answered By - Blip
Answer Checked By - Mary Flores (JavaFixing Volunteer)