• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access instance of another class(frames)  RSS feed

 
Filip Vero
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

---Hi all, I have the this issue I am having this frame and while clicking on it's button

---the top part:

public class Component extends JFrame {

private JPanel contentPane;
private static String title;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Component frame = new Component(title);
frame.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}
}
});
}

---<<the Action Listener>>

public Component(String title) {
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

System.out.println("good");
setVisible(false);
//Here I want to interact with the other frame for ex to setRam(see below)

}
});
}

___________________________________________________________________________
---I want to update the other frame which has been instanciated by another object and this is it:

public class Pc extends JFrame {

private JPanel contentPane;
private JLabel lblMotherboard;
private JLabel lblRam;
private String Motherboard="none";
private String Ram="none";

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pc frame2 = new Pc();
frame2.setVisible(true);


} catch (Exception e) {
e.printStackTrace();
}
}
});
}
...
public void setRam(String ram) {
Ram = ram;
}
____________________________________________________________________
the problem is on the top class I can access the other class setter only if it is static otherwise the instance name(frame2) isn't recognised.How I am supposed to overcome it?
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Filip,
Welcome to the Ranch!

Forgetting GUI for a moment - think about this: You have 2 classes - how do you refer or call methods of other classes. You simply need to create an instance of the class and through the instance, access the methods - nothing is different here.

So, you have to create an instance of Pc within that of Component and then call setRam() through it.

Now, coming to other points:
1) You have main() method defined in both your classes. I think that is not your intention. You should decide on what your startup class is going to be - and have the main method in that class only.
2) Suppose you have a requirement where you want to show an initial window and then display another window on click of a button - for such scenarios, a dialog is better suited. The initial window should be a frame and all other sub-windows should be dialog(s) in an ideal application. You can create dialogs using the JDialog class - you can declare this to be a modal one too - and you can declare the main JFrame as the parent to this dialog. These are all the advantages of using a dialog.
You can read more about dialogs in the tutorial

PS: 3) You can UseCodeTags when inserting code into your questions. Click on the UseCodeTags link to know how to do this.
 
Filip Vero
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, I see my mistake I thought you could reference freely from without creating the reference from the particular class.Also I am thinking of creating jDaliogs it's a definitely a better option .Thanks very much, much admired.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!