Take the 2-minute tour ×

How do I go about creating what I describe below?

First, here is the basic look of my GUI:

enter image description here

When I click on Add New Account I want to have the GUI pop up a small window where the user can enter log-in credentials. I would need this information to be passed back into the main GUI, so I am lost as how to approach this.

The same goes for Preferences or Remove Account. How do I go about creating a "GUI Overlay" of sorts. Sorry, I can't figure out the correct terminology for the effect I am looking for.

I wanted to attempt to use JOptionPane's, but after some research this seemed like it was not the route to be taking.

I was also toying with the idea of creating a new JFrame when the action was preformed. How should this be approached?

share|improve this question
Don't do that. Have a look at this SO question: stackoverflow.com/questions/9554636/… You can use modal dialogs instead. –  brano Dec 3 '12 at 23:49
Ok. I am now using an internal frame. But the problem is that it is trying to use the main pane's layout manager to position itself, even if I set location and size. –  Brendan Dec 4 '12 at 0:27
add comment

1 Answer

up vote 3 down vote accepted

Start by using dialogs over frames. Dialogs are designed to gather small pieces of information from the user.

I would create a separate component for each operation you want to perform. Within these components I would provide setters and getters to allow you to gain access to the information managed by the component.

From there I would either use a JOptionPane or JDialog to display the component to the user. The reason for using one over the other for me comes down to begin able to control the action buttons (Okay and Cancel for example). For something like the login dialog, I want to restrict the user from begin able to hit the Login button until they've provided enough information to make the attempt.

The basic follow would be something like this...

LoginDialog dialog = new LoginDialog(SwingUtilities.getWindowAncestor(this)); // this is a reference any valid Component
dialog.setModal(true); // I would have already done this internally to the LoginDialog class...
dialog.setVisible(true); // A modal dialog will block at this point until the window is closed
if (dialog.isSuccessfulLogin()) {
    login = dialog.getLogin(); // Login is a simple class containing the login information...

The LoginDialog might look something like this...

public class LoginDialog extends JDialog {
    private LoginPanel loginPane;
    public LoginDialog(Window wnd) {
        loginPane = new LoginPanel();
        setLayout(new BorderLayout());
        // Typically, I create another panel and add the buttons I want to use to it.
        // These buttons would call dispose once they've completed there work

    public Login getLogin() {
        return loginPane.getLogin();

    public boolean isSuccessfulLogin() {
        return loginPane.isSuccessfulLogin();

The dialog is simply acting as proxy/container for the login pane.

This is, of course an overview, you will need to fill in the blanks ;)

Now, if you don't want to go to the trouble of creating your own dialog, you can take advantage of the JOptionPane instead.

LoginPanel loginPane = new LoginPanel();
int option = JOptionPane.showOptionDialog(
     this, // A reference to the parent component
     "Login", // Title
     null, // You can supply your own icon it if you want
     new Object[]{"Login", "Cancel"}, // The available options to the user
     "Login" // The "initial" option
if (option == 0) {
    // Attempt login...
share|improve this answer
This should solve the overall issues I am seeing with internal frames; yet I am still confused as how to create the Dialog box. You show a private LoginPanel loginPane; without an reference to what a LoginPanel is. Is it just a class that extends JPanel? Contains the various components that I am going to add? –  Brendan Dec 4 '12 at 1:25
Yes LoginPane would extend from something like JPanel and contained the required UI components. It would then have getters and setters as required –  MadProgrammer Dec 4 '12 at 2:12
add comment

Your Answer


