How to access JFrame's method from JDialog?

695 views Asked by At

I have my main JFrame and one more JDialog. If user click on the button, i want JDialog to call method from this JFrame(which contains some operations on ComboBox in this JFrame). How can I do that? I don't want to use MyJFrame form = new MyJFrame(); because it will make a new JFrame which i don't want to do, i want to call method from JFrame which is running currently on my computer. Thanks.

2

There are 2 answers

1
Juan Sebastian Osorio On

Maybe JOptionPane.showInputDialog() , show a JDialog to take input from User.

10
Salem On

Assuming the JButton is on the JDialog.

If both are in the same class, why not just do this?

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //I just got clicked
        form.doSomething();
    }
});

which can be shortened to

button.addActionListener(e -> form.doSomething());

If they aren't, and you're extending JDialog (which I wouldn't recommend) just pass the JFrame in its constructor, then your dialog will have access to it.

It's not possible to add much more without seeing more of your code.