I want to create a modal JDialog which will be called from JMenuItem. I have read some post and found an example (look belove). In this example main frame is called from main method. How to create something like that, but in main method will only be called constructor of main class?
Exmaple: http://www.rsdn.ru/forum/java/3526582.1
Update:
My code:
import java.awt.event.*;
import javax.swing.*;
public class JD extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JMenuBar menubar = new JMenuBar();
private JMenu menu = new JMenu("Options");
private JMenuItem item = new JMenuItem("Try this");
JLabel label = new JLabel("label");
JTextField text = new JTextField(10);
JPanel panel = new JPanel();
JButton b = new JButton("get and close");
JD() {
super("Frame");
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
menu.add(item);
menubar.add(menu);
setJMenuBar(menubar);
item.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent evnt) {
Object source = evnt.getSource();
if (source == item) {
new Dialog(new JFrame());
}
}
private class Dialog extends JDialog {
private static final long serialVersionUID = 1L;
Dialog(JFrame frame) {
super(frame, true);
setSize(200, 200);
setLocationRelativeTo(null);
panel.setLayout(null);
label.setBounds(10, 10, 50, 15);
panel.add(label);
text.setBounds(60, 8, 50, 19);
panel.add(text);
add(panel);
b.setBounds(40, 40, 100, 25);
panel.add(b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evnt) {
System.out.println(text.getText());
dispose();
}
});
setVisible(true);
}
}
public static void main(String[] args) {
new JD();
}
}