How can I do when when I press a JMenuItem
it shows a window where I would have to insert, for example, a date and select from a pair of checkboxes?
In short, how do I popup a new JFrame
?
Thanks for the help!
How can I do when when I press a JMenuItem
it shows a window where I would have to insert, for example, a date and select from a pair of checkboxes?
In short, how do I popup a new JFrame
?
Thanks for the help!
Check if it's something like this:
import javax.swing.*;
/**
* JOptionPane showInputDialog example #1.
* A simple showInputDialog example.
* @author alvin alexander, http://alvinalexander.com
*/
public class JOptionPaneShowInputDialogExample1
{
public static void main(String[] args)
{
// a jframe here isn't strictly necessary, but it makes the example a little more real
JFrame frame = new JFrame("InputDialog Example #1");
// prompt the user to enter their name
String name = JOptionPane.showInputDialog(frame, "What's your name?");
// get the user's input. note that if they press Cancel, 'name' will be null
System.out.printf("The user's name is '%s'.\n", name);
System.exit(0);
}
}
Link to more examples
You can create this popup using JDialog
. Apparently you already use JFrame
and you know you can add components to it, JDialog
works the same way.
import java.awt.FlowLayout;
import javax.swing.*;
public class Main {
public static void main(String...args) {
// A Janela 'principal'
JFrame jFrame = new JFrame();
jFrame.setSize(300, 300);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// O Janela 'popup' que terá os botões de seleção, data, etc
JDialog jdialog = new JDialog(jFrame, true);
jdialog.setSize(200, 200);
jdialog.setLayout(new FlowLayout());
jdialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// Adicionando os botões de 'radio' no JDialog da mesma forma que é feita no JFrame
jdialog.add(new JRadioButton("Vermelho"));
jdialog.add(new JRadioButton("Verde"));
jdialog.add(new JRadioButton("Azul"));
jFrame.setVisible(true);
jdialog.setVisible(true);
}
}
And then you can add the components ( JLabel
, JTextField
, etc) according to the need of your application.