This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Access JFrame Components from JDialog  RSS feed

 
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.
 
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!