How do I popup a new Jframe?

Asked

Viewed 2,880 times

1

How can I do for when to press one JMenuItem it show a window where I would have to enter, for example, a date and select from a couple of checkboxes?

Briefly, as I do a "popup" a new JFrame?

Thanks for the help!

2 answers

2

See if it’s something like this you want:

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

img

Link with more examples

2

You can create this popup using JDialog. Apparently you already use JFrame and knows that it is possible to add components to it, the JDialog works in 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);
    }
}

Upshot:

resultado

And then you can add the components (JLabel, JTextField, etc) in accordance with the need for its application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.