I'm a little bit new here. I'm stuck figuring out what's wrong with my code. I have a two JFrames, one is my main frame and the other is like a "view detail" frame whenever you click a selected data in the main frame. This is my main class looks like:
public class MainClass{
private JFrame frame;
private JButton btnNewButton;
private String testData;
private DetailClass detail;
public JFrame getFrame() {
return frame;
}
public String getTest() {
return testData;
}
public void setTest(String test) {
this.testData = test;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
main window = new main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainClass() {
initialize();
}
private void initialize() {
//some codes label, button, etc.
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setTest("Data retrieved from main");
detail.getFrame().setVisible(true);
}
});
detail = new DetailClass();
detail.setMainClass(this);
}
}
And here is my DetailClass where I want to show data that I get from main. Public class DetailClass(){ private MainClass main; private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DetailClass window = new DetailClass ();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DetailClass() {
initialize();
}
public void initialize(){
//some codes for frame
//this is where I test if the detail class receives the data
System.out.println(main.getTest());
}
public void setMainClass(MainClass m){
this.main = m;
}
}
That's it, the main.getTest() doesn't seem to work in intialize() but I tried to put it in a mouse event whenever I click a button in frame 2 and it works fine. It seems that it only works when the frame is already visible/activated/finished rendering but it won't work in initialize and I need it to preload the data in frame 2. I hope you guys can help me with this. :)