5

I know that it is very simple question, but I can't find a solution.

I have a main swing dialog and other swing dialog. Main dialog has a button. How can I make that after clicking a button the other dialog opens?

EDIT:

When I try this:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
       NewJDialog okno = new NewJDialog();
       okno.setVisible(true);
    }

I get an error:

Cannot find symbol NewJDialog

The second window is named NewJDialog...

Emmanuel Angelo.R
  • 1,545
  • 2
  • 16
  • 24
Devel
  • 950
  • 9
  • 17
  • 29
  • 1
    Add an ActionListener to the button (see e.g. http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html) in which you open the second dialog. – Searles Apr 18 '10 at 17:48
  • 1
    Concerning your edit, you should learn the difference between Class names and member names, and you should also take a look at the scope of variables. NewJDialog in your case is a class name and since this class does not exist you get the error. – Searles Apr 18 '10 at 23:38
  • @Searles: Good point. The name is reminiscent of those generated by the NetBeans GUI editor. A related example is discussed here: http://stackoverflow.com/questions/2561480 – trashgod Apr 19 '10 at 03:36

1 Answers1

6

You'll surely want to look at How to Make Dialogs and review the JDialog API. Here's a short example to get started. You might compare it with what you're doing now.

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class DialogTest extends JDialog implements ActionListener {

    private static final String TITLE = "Season Test";

    private enum Season {
        WINTER("Winter"), SPRING("Spring"), SUMMER("Summer"), FALL("Fall");
        private JRadioButton button;
        private Season(String title) {
            this.button = new JRadioButton(title);
        }
    }

    private DialogTest(JFrame frame, String title) {
        super(frame, title);
        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new GridLayout(0, 1, 8, 8));
        ButtonGroup group = new ButtonGroup();
        for (Season s : Season.values()) {
            group.add(s.button);
            radioPanel.add(s.button);
            s.button.addActionListener(this);
        }
        Season.SPRING.button.setSelected(true);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.add(radioPanel);
        this.pack();
        this.setLocationRelativeTo(frame);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JRadioButton b = (JRadioButton) e.getSource();
        JOptionPane.showMessageDialog(null, "You chose: " + b.getText());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogTest(null, TITLE);
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045