1

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. :)

  • 1
    The `"view detail" frame` shouldn't be a JFrame, a **main** application window, but rather should be a JDialog, or an application's dependent sub-window. – Hovercraft Full Of Eels Oct 22 '16 at 17:36
  • 1
    Please see: [The Use of Multiple JFrames: Good or Bad Practice?](http://stackoverflow.com/q/9554636/522444) for more on this. Also, you would pass information from one class to another same as for any non-Swing application: through constructor and method parameters. – Hovercraft Full Of Eels Oct 22 '16 at 17:38
  • Where do you call this `other()` method?? – Hovercraft Full Of Eels Oct 22 '16 at 17:40
  • Try to deal with one problem at a time. The code if full of problem and will not even compile as posted. First step: get the main-frame with all its content run as expected, and post an [MCVE] if you encounter difficulties.. – c0der Oct 22 '16 at 18:00
  • I guess I'll redo my codes cause I'm doing it wrong. Thank you for pointing out. :) – Siomai Killer Oct 23 '16 at 01:12

1 Answers1

2

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);
    }

}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373