Hi,

How can I code it so I can make a new window appear on my application? Let's say, user hits a "new user" button, a new window is supposed to pop with TextFields and labels and so on.

In Dephi is something like, Form1.show;

How do I do this in Java?

Dani AI

Generated

Nice start from — instantiating a secondary window is the basic idea. For a "new user" form, pick the right window type first: use a modal JDialog when the main window should wait for the user to finish (simpler for collecting and returning data), and use a separate JFrame when the window is an independent tool that can stay open on its own.

Create and show GUI code on the Event Dispatch Thread, and prefer packing and centering the window before showing it. A common pattern is to create a dialog class (e.g., NewUserDialog extends JDialog) that exposes getters for the entered data; then show it modally and read those getters after it closes.

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        NewUserDialog d = new NewUserDialog(parentFrame); // build UI inside that class
        d.pack();
        d.setLocationRelativeTo(parentFrame);
        d.setVisible(true); // returns when dialog closed
        // read values from d.get...() here
    }
});

Troubleshooting tips: add all components before calling pack()/setVisible(), use DISPOSE_ON_CLOSE for secondary windows so they free resources, and never perform long-running tasks on the EDT (use SwingWorker). If you need modal behavior but also want owner-relative positioning, JDialog is usually better than JFrame. See the Swing tutorials for dialogs and concurrency for details and examples: How to Use Dialogs and Concurrency in Swing.

Recommended Answers

All 2 Replies

Great stuff! I knew it was going to be simple. Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.