Results 1 to 4 of 4
- 04-17-2009, 01:12 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
calling JPanel's JTextField from another JPanel class
hi, i have a JPanel class which has a form liked constructor. I would like to get data from the JTextFieldss in the constructor by another class which needs the JTextField info. please see code below and point out reason pan.idField not working. thanks very much.
Java Code:// this is the class having the constructor i need to call from another class import java.sql.*; import java.text.MessageFormat; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class RecordCP extends JPanel implements ActionListener { private DataHolder dh = new DataHolder(); private JFrame frame; private JTextField idField, fnField, lnField, phoneField, addField, cityField, stateField, zipField, sexField, ageField, hourlyField, hoursField; private JButton saveButton, cancelButton, updateButton, searchButton, searchPreButton, clearButton; private int id, age; private String fn=null, ln=null, phone=null, add=null, city=null, state=null, zip=null, sex=null; private double hourly, hours; private LinkedList<Employee> employeeList = new LinkedList<Employee>(); private int count=-1, countTotal=0; public RecordCP(JFrame frame) { this.frame = frame; GridLayout gl = new GridLayout(15,2); // for the main panel //panel = new JPanel(); this.setBackground(java.awt.Color.white); this.setLayout(gl); //////the labels and fields /////// this.add(new JLabel("Employee Serial #:")); idField = new JTextField(); this.add(idField); this.add(new JLabel("First Name:")); fnField = new JTextField(); this.add(fnField); this.add(new JLabel("Last Name:")); lnField = new JTextField(); this.add(lnField); this.add(new JLabel("Telephone:")); phoneField = new JTextField(); this.add(phoneField); this.add(new JLabel("Address:")); addField = new JTextField(); this.add(addField); this.add(new JLabel("City:")); cityField = new JTextField(); this.add(cityField); this.add(new JLabel("State:")); stateField = new JTextField(); this.add(stateField); this.add(new JLabel("Zip Code:")); zipField = new JTextField(); this.add(zipField); this.add(new JLabel("Sex:")); sexField = new JTextField(); this.add(sexField); this.add(new JLabel("Age:")); ageField = new JTextField(); this.add(ageField); this.add(new JLabel("Hourly Rate:")); hourlyField = new JTextField(); this.add(hourlyField); this.add(new JLabel("Hours per week:")); hoursField = new JTextField(); this.add(hoursField); saveButton = new JButton("Add Record"); //saveButton.addActionListener(this); saveButton.addActionListener(new AddRecord(this)); --> parsing this.add(saveButton); // ... more code but i didn't put it hereJava Code:// this is the class i need the JTextField from the above class's constructor import java.sql.*; import java.text.MessageFormat; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class AddRecord implements ActionListener { private JPanel pan; AddRecord(JPanel p) { this.pan = p; /// the above class's JPanel becomes the JP here } public void actionPerformed(ActionEvent e) { if(pan.g.idField.getText().equals("")) /// why this doesn't work??? { JOptionPane.showMessageDialog(null, "Employee ID must be entered!"); status=false; }
-
Your main problem that I see (besides someone posting spam after your post) is that your pan variable in the GetRecord class is a JPanel variable, not a RecordCP variable. So pan has no knowledge of RecordCP's fields, methods, etc.
Regardless, my recommendation is to keep RecordCP's fields private and only allow them to be accessed by public getters. One way to do this (simplified by use of "magic numbers", sorry) is like so:
MyGui.java
MyAddRecord.javaJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyGui { // mainpanel holds all of gui components private JPanel mainPanel = new JPanel(); private JTextField idField = new JTextField(10); private JTextField fnField = new JTextField(10); private JTextField lnField = new JTextField(10); private JTextField[] fields = {idField, fnField, lnField}; private String[] labelStrs = {"Employee Serial #:", "First Name:", "Last Name"}; private JButton saveBtn = new JButton("Add Record"); public MyGui() { // each a one column grid JPanel labelPanel = new JPanel(new GridLayout(0, 1, 5, 5)); JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 5, 5)); for (int i = 0; i < fields.length; i++) { labelPanel.add(new JLabel(labelStrs[i])); fieldPanel.add(fields[i]); } JButton quitBtn = new JButton("Quit"); quitBtn.addActionListener(new QuitListener()); JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 15, 0)); buttonPanel.add(saveBtn); buttonPanel.add(quitBtn); int ebgap = 5; mainPanel.setBorder(BorderFactory.createEmptyBorder(ebgap, ebgap, ebgap, ebgap)); // make borderlayout to allow easy placement of grid columns mainPanel.setLayout(new BorderLayout(10, 10)); mainPanel.add(labelPanel, BorderLayout.WEST); mainPanel.add(fieldPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); // buttons on bottom } private class QuitListener implements ActionListener { public void actionPerformed(ActionEvent e) { // get the root window that holds the mainPanel Window win = SwingUtilities.getWindowAncestor(mainPanel); // and dispose of it win.dispose(); } } // public method to allow control class to add action listener // to save button public void addSaveBtnListener(ActionListener al) { saveBtn.addActionListener(al); } // public getters for all the text fields. Better would be to // have a HashMap of JTextFields and use the label strings as a // "property" that allows us to extract the text from a chosen // field public String getId() { return idField.getText(); } public String getFirstName() { return fnField.getText(); } public String getLastName() { return lnField.getText(); } // get the mainPanel so can be placed in JFrame public JComponent getMainJPanel() { return mainPanel; } }
MyMainJava Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MyAddRecord implements ActionListener { private MyGui gui; public MyAddRecord(MyGui gui) { this.gui = gui; } public void actionPerformed(ActionEvent arg0) { System.out.println("ID is: " + gui.getId()); System.out.println("First Name: " + gui.getFirstName()); System.out.println("Last Name: " + gui.getLastName()); } }
Java Code:import javax.swing.JFrame; public class MyMain { private static void createAndShowUI() { MyGui gui = new MyGui(); gui.addSaveBtnListener(new MyAddRecord(gui)); JFrame frame = new JFrame("GUI"); frame.getContentPane().add(gui.getMainJPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
- 04-20-2009, 04:50 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
thanks
very good suggestion, thanks
-
Similar Threads
-
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Making a Table Row with JPanel Composed of JTextField and JComboBox Selectable
By datechie in forum AWT / SwingReplies: 2Last Post: 07-30-2008, 12:33 PM -
Referencing JPanel Class
By uncopywritable in forum New To JavaReplies: 2Last Post: 08-12-2007, 01:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks