Reputation: 13
I want to get a JFrame (createFrame) from another JFrame (mainFrame). I created a method in the mainFrame which returns the createFrame, so that I can dispose the createFrame when clicking a button in the createFrame. But I cant call this methode (getCreateFrame) from the second JFrame (createFrame).
MainFrame.java
import frames.SelectFrame;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame{
private JPanel mainPanel;
private JComboBox cbOption;
private JButton btConfirm;
private JLabel lbCredit;
private JFrame createFrame;
public static void main(String[] args) {
JFrame mainFrame = new JFrame("");
mainFrame.setContentPane(new MainFrame().mainPanel);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setBounds(500, 500, 400, 400);
}
public MainFrame() {
btConfirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int option = cbOption.getSelectedIndex();
System.out.println(option);
if(option == 0){
JFrame createFrame = new SelectFrame();
createFrame.setContentPane(new SelectFrame().mainPanel);
createFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
createFrame.pack();
createFrame.setVisible(true);
createFrame.setBounds(500, 500, 400, 400);
System.out.println("Frame created!");
} else if(option == 1){
} else{
}
}
});
}
public final JFrame getCreateFrame(){
return createFrame;
}
}
CreateFrame.java
package frames;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
public class SelectFrame extends JFrame{
public JPanel mainPanel;
private JLabel lbCredit;
private JComboBox cbxCurrency;
private JButton btConfirm;
private JButton btExit;
public SelectFrame() {
btConfirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
btExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
}
Upvotes: 1
Views: 579
Reputation: 1811
To answer your question I believe you should take a look at Programming scope.
If you want to access the getCreateFrame()
method in your SelectFrame
-class you will need to be able to reach the MainFrame
-class somehow since this holds the method.
createFrame
is a private variable of the MainFrame
-class which means it's only accessible within the class. Which is why (as you have done) you need a get-method to retrieve it. Although, when you call the get-method you want to know which object's createFrame
you are trying to access. The question is, how will SelectFrame
know which method to call?
If you edit your SelectFrame
-class alike this:
public class SelectFrame extends JFrame {
private MainFrame mFrame;
public SelectFrame(MainFrame frame) {
mFrame = frame;
}
}
You can now call the getCreateFrame()
from the MainFrame
you pass to SelectFrame
's constructor within the SelectFrame
class. (mFrame.getCreateFrame();
)
Note: In the MainFrame
-class's constructor you initialize your createFrame
in an if
-statement which means it will give you null
unless option == 0
.
if(option == 0) {
JFrame createFrame = new SelectFrame();
}
Upvotes: 2
Reputation: 816
In your constructor, you are creating a local variable instead of assigning to the instance variable you created.
JFrame createFrame = new SelectFrame();
Should instead have been:
createFrame = new SelectFrame();
Don't create a new variable, assign to the existin one.
Upvotes: 0
Reputation: 114
Your returned createFrame is null. This is due to the fact that you have introduced an local instance of JFrame createFrame = new SelectFrame(); in the MainFram constructor
public final JFrame getCreateFrame(){
return createFrame;
}
Upvotes: 0