• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access JFrame Components from JDialog  RSS feed

 
James Ajewole
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone help me, I created a form (which extends JFrame) that collects details like name, address, etc and has a button such that when i click the button, it brings up a JDialog that contains a list of names retrieved from a database. What i want to do is that when i click/double click one of the names, in the JDialog, the info about the person gets loaded in the respective components in the JFrame. I have the JDialog working and retrieving the info in the database is working but how to load - for example the name of the person in the JTextfield which is a component in the Jframe. thanks.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I answered a very similar question here. Hope that helps.
 
James Ajewole
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Gregg, I tried your suggestion but i seem not to get it to work. below is a skeleton of the code i wrote maybe it could give you an idea of what i'm working with.
public class MyFrame extends JFrame implements ActionListener {
private JTextField emp_name;
private JTextField emp_address
.
.
private JButton choose
public MyFrame() {
emp_name = new JTextField(10);
// add name to layout manager
choose = new JButton("Select Employee");
// add choose to layout manager
choose.addactionlistener(this);
.
.
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == choose) {
JDialog dialog = new MyDialog(this,"Title",true);
}
}
now the dialog class...
public class MyDialog extends JDialog implements MouseListener{
// declare variables
private Jlist mylist;
.
.
public MyDialog(Frame frame, String title, boolean bo) {
// set up the JList and retrieve data from
// database into list
mylist.addMouseListener(this);
}
public void mousePressed(MouseEvent e) {
// get number of clicks
mylist.getSelectedIndex();
// get data from database about name selected
// **how do i load data from database into emp_name and emp_address??
}
}
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have several options.
1. Since your textfields are private you need to create public setter methods for them in your JFrame class. Then call those methods from your JDialog class. So you would have methods like the following in your JFrame

2. Make your textfields public static and just dot reference them through your JFrame object you passed to your JDialog
3. Add 2 more params to your JDialog constructor and pass your JTextField objects to your JDialog and access them that way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!