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. :)
Without a valid Minimal, Complete, and Verifiable example we can only guess what you might be doing wrong, but I'm guessing that the other()
method, the one where you display the main's text, is only called once, on creation of the second JFrame, and before the text is changed. The solution here is as mentioned in comments -- pass information from one class to another through method or constructor parameters, and pass this information when it is needed, not only on program start up.
Also as noted in comments, the 2nd dialog window should be a JDialog, not a JFrame (application window).
For example:
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class TwoWindows {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
MainGui mainPanel = new MainGui();
mainPanel.setPreferredSize(new Dimension(400, 250));
JFrame frame = new JFrame("Main GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class MainGui extends JPanel {
private SubWindow subWindow = new SubWindow();
private JDialog dialog;
private JTextField textField = new JTextField("Text in field", 10);
public MainGui() {
add(textField);
add(new JButton(new ShowDetailAction("Show Detail")));
}
private class ShowDetailAction extends AbstractAction {
public ShowDetailAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
// if dialog not yet created -- create it
if (dialog == null) {
Window win = SwingUtilities.getWindowAncestor(MainGui.this);
dialog = new JDialog(win, "Details Window", ModalityType.MODELESS);
dialog.add(subWindow);
dialog.pack();
dialog.setLocationRelativeTo(win);
}
String text = textField.getText();
subWindow.passText(text);
dialog.setVisible(true);
}
}
}
class SubWindow extends JPanel {
private JLabel textLabel = new JLabel(" ");
public SubWindow() {
setPreferredSize(new Dimension(300, 60));
add(new JLabel("Details:"));
add(textLabel);
}
public void passText(String text) {
textLabel.setText(text);
}
}