38

I am currently developing a java application.

I want to show a new Window which contains a text area and a button.

Do you have any ideas?

Community
  • 1
  • 1
Carlo
  • 1,147
  • 3
  • 15
  • 23

6 Answers6

40

The same answer : JOptionpane with an example :)

package experiments;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CreateDialogFromOptionPane {

    public static void main(final String[] args) {
        final JFrame parent = new JFrame();
        JButton button = new JButton();

        button.setText("Click me to show dialog!");
        parent.add(button);
        parent.pack();
        parent.setVisible(true);

        button.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                String name = JOptionPane.showInputDialog(parent,
                        "What is your name?", null);
            }
        });
    }
}

enter image description here

Evorlor
  • 7,263
  • 17
  • 70
  • 141
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
14

Hmm it has been a little while but from what I remember...
If you want a custom window you can just make a new frame and make it show up just like you would with the main window. Java also has a great dialog library that you can check out here:

How to Make Dialogs

That may be able to give you the functionality you are looking for with a whole lot less effort.

Object[] possibilities = {"ham", "spam", "yam"};
String s = (String)JOptionPane.showInputDialog(
                    frame,
                    "Complete the sentence:\n"
                    + "\"Green eggs and...\"",
                    "Customized Dialog",
                    JOptionPane.PLAIN_MESSAGE,
                    icon,
                    possibilities,
                    "ham");

//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
    setLabel("Green eggs and... " + s + "!");
    return;
}

//If you're here, the return value was null/empty.
setLabel("Come on, finish the sentence!");

If you do not care to limit the user's choices, you can either use a form of the showInputDialog method that takes fewer arguments or specify null for the array of objects. In the Java look and feel, substituting null for possibilities results in a dialog that has a text field and looks like this:

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
kreynolds
  • 426
  • 4
  • 15
  • I think I wanna do the new Frame can u Show me how to do it thx btw ? – Carlo Jan 13 '12 at 21:12
  • The example I put above is about as clear and concise as I can get for showing you how to do it. If you want to customize it beyond the example take a look at the Java Docs. – kreynolds Jan 23 '12 at 14:17
4

JOptionPane is your friend : http://www.javalobby.org/java/forums/t19012.html

mcfinnigan
  • 11,442
  • 35
  • 28
  • 1
    Around 11 years later, and the website linked here is down. To save time for others who come here, there's a [Wayback Machine URL](https://web.archive.org/web/20170710135759/http://www.javalobby.org/java/forums/t19012.html) to the page or you can check maeric's post which links the [Oracle URL](http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html). I'm only commenting this considering that at the time of writing this comment, this entire question is the top Google result for `java open popup`. – Severnarch Feb 11 '23 at 18:34
2

Check out Swing Dialogs (mainly focused on JOptionPane, as mentioned by @mcfinnigan).

maerics
  • 151,642
  • 46
  • 269
  • 291
0
public class JSONPage {
    Logger log = Logger.getLogger("com.prodapt.autotest.gui.design.EditTestData");


    public static final JFrame JSONFrame = new JFrame();
    public final JPanel jPanel = new JPanel();

    JLabel IdLabel = new JLabel("JSON ID*");
    JLabel DataLabel = new JLabel("JSON Data*");
    JFormattedTextField JId = new JFormattedTextField("Auto Generated");
    JTextArea JData = new JTextArea();
    JButton Cancel = new JButton("Cancel");
    JButton Add = new JButton("Add");

    public void JsonPage() {

        JSONFrame.getContentPane().add(jPanel);
        JSONFrame.add(jPanel);
        JSONFrame.setSize(400, 250);
        JSONFrame.setResizable(false);
        JSONFrame.setVisible(false);
        JSONFrame.setTitle("Add JSON Data");
        JSONFrame.setLocationRelativeTo(null);
        jPanel.setLayout(null);

        JData.setWrapStyleWord(true);
        JId.setEditable(false);

        IdLabel.setBounds(20, 30, 120, 25);
        JId.setBounds(100, 30, 120, 25);
        DataLabel.setBounds(20, 60, 120, 25);
        JData.setBounds(100, 60, 250, 75);
        Cancel.setBounds(80, 170, 80, 30);
        Add.setBounds(280, 170, 50, 30);

        jPanel.add(IdLabel);
        jPanel.add(JId);
        jPanel.add(DataLabel);
        jPanel.add(JData);
        jPanel.add(Cancel);
        jPanel.add(Add);

        SwingUtilities.updateComponentTreeUI(JSONFrame);

        Cancel.addActionListener(new ActionListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void actionPerformed(ActionEvent e) {
                JData.setText("");
                JSONFrame.hide();
                TestCasePage.testCaseFrame.show();
            }
        });

        Add.addActionListener(new ActionListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    PreparedStatement pStatement = DAOHelper.getInstance()
                            .createJSON(
                                    ConnectionClass.getInstance()
                                            .getConnection());
                    pStatement.setString(1, null);
                    if (JData.getText().toString().isEmpty()) {
                        JOptionPane.showMessageDialog(JSONFrame,
                                "Must Enter JSON Path");
                    } else {
                        // System.out.println(eleSelectBy);
                        pStatement.setString(2, JData.getText());
                        pStatement.executeUpdate();
                        JOptionPane.showMessageDialog(JSONFrame, "!! Added !!");
                        log.info("JSON Path Added"+JData);
                        JData.setText("");
                        JSONFrame.hide();
                    }

                } catch (SQLException e1) {
                    JData.setText("");
                    log.info("Error in Adding JSON Path");
                    e1.printStackTrace();
                }
            }
        });
    }

}
laalto
  • 150,114
  • 66
  • 286
  • 303
  • 9
    hmm ... a bit on the borderline of answering the question, isn't it ;-) Anyway, a) please learn java naming conventions and stick to them b) never do any manual sizing/locating of components, that's the exclusive task of a suitable LayoutManager c) updateComponentTree isn't needed – kleopatra Oct 04 '13 at 08:00
0

Try Using JOptionPane or Swt Shell .

iAmLearning
  • 1,153
  • 3
  • 15
  • 28