Solved

Creating a Custom Java Swing Dialog

Posted on 2004-09-22
5
588 Views
Last Modified: 2013-11-23
I would like to create a simple Java Swing Dialog box that accepts, as user input, a user Id and password and retuns an Object.  I don't want to nor do I think that I can use JOption for this.  I believe this has to be a custom dialog class that extends JDialog.  Below, I've created a basic JPanel that expects a user id and password.  There are two JButtons, on the JPanel, 'Login' and 'Cancel'.  I would appreciated it if some one could help me complete the rest of the code so that this class can be created as a dialog box the pops up modally and passes back an Object if the 'Login' JButton is pressed or passes back a null if the 'Cancel' JButton is pressed.  Both buttons should close the dialog box.

Thanks, in advance, for the help.

package com.adminclient.form;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class LogonForm {
  JPanel mainPanel = new JPanel();
  JPanel loginPanel = new JPanel();
  Border border1;
  JPanel welcomePanell = new JPanel();
  Border border2;
  JLabel welcomeLabel = new JLabel();
  JLabel logonLabel = new JLabel();
  Border border3;
  JLabel userIdLabel = new JLabel();
  Border border4;
  JLabel passwordLlabel = new JLabel();
  Border border5;
  JTextField userIdTextField = new JTextField();
  JPasswordField passwordTextField = new JPasswordField();
  JButton loginButton = new JButton();
  JButton cancelButton = new JButton();
  GridBagLayout mainGridBagLayout = new GridBagLayout();
  GridBagLayout loginGridBagLayout = new GridBagLayout();
  GridBagLayout welcomeGridBagLayout = new GridBagLayout();

  public LogonForm() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    border1 = BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(99, 99, 99),new Color(142, 142, 142));
    border2 = BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(99, 99, 99),new Color(142, 142, 142));
    border3 = BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(99, 99, 99),new Color(142, 142, 142));
    border4 = BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(99, 99, 99),new Color(142, 142, 142));
    border5 = BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(99, 99, 99),new Color(142, 142, 142));
    mainPanel.setLayout(mainGridBagLayout);
    mainPanel.setVerifyInputWhenFocusTarget(true);
    loginPanel.setBorder(border1);
    loginPanel.setLayout(loginGridBagLayout);
    welcomePanell.setBorder(border2);
    welcomePanell.setLayout(welcomeGridBagLayout);
    welcomeLabel.setFont(new java.awt.Font("Batang", 3, 24));
    welcomeLabel.setText("Welcome To The Acme Corporation");
    logonLabel.setFont(new java.awt.Font("Dialog", 1, 12));
    logonLabel.setBorder(border3);
    logonLabel.setText("Please enter your login information");
    userIdLabel.setBorder(border4);
    userIdLabel.setText("User Id");
    passwordLlabel.setBorder(border5);
    passwordLlabel.setText("Password");
    userIdTextField.setText("");
    passwordTextField.setText("");
    loginButton.setText("Login");
    cancelButton.setText("Cancel");
    mainPanel.add(loginPanel,  new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(11, 85, 0, 100), 30, 27));
    loginPanel.add(logonLabel,  new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(11, 28, 0, 52), 0, 0));
    loginPanel.add(userIdLabel,  new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(24, 28, 0, 0), 22, 0));
    loginPanel.add(passwordLlabel,  new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 28, 27, 0), 5, 0));
    loginPanel.add(userIdTextField,  new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(23, 0, 0, 30), 150, 0));
    mainPanel.add(welcomePanell,  new GridBagConstraints(0, 0, 2, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(11, 12, 0, 12), 17, 14));
    welcomePanell.add(welcomeLabel,    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(11, 19, 7, 17), 5, 0));
    loginPanel.add(passwordTextField,  new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 27, 30), 149, 0));
    mainPanel.add(loginButton,  new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(13, 167, 15, 0), 0, 0));
    mainPanel.add(cancelButton,  new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(13, 0, 15, 162), 0, 0));
  }
}
0
Comment
Question by:HycelTaylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 1

Accepted Solution

by:
Dave_Toland earned 500 total points
ID: 12129887
i wrote a basic class showing what you need to do. Just pad it out with your own gui objects. ANy probs, shout.

import javax.swing.*;
import java.awt.event.*;


public class JDialogTest extends JDialog {
      
      public JDialogTest(){
                              
                  JLabel nameLbl = new JLabel("Name:");
                  JTextArea nameTxt = new JTextArea(10,1);
                  
                  JLabel passLbl = new JLabel("PassWord:");
                  JTextArea passTxt = new JTextArea(10,1);
                  
                  JButton loginBtn = new JButton("Login");
                  loginBtn.addActionListener(
                        new ActionListener(){
                              public void actionPerformed(ActionEvent e){
                                    
                                    System.out.println("Do something now...");
                                    dispose();
                              }
                        }
                  );
                  
                  JButton cancelBtn = new JButton("Cancel");
                  cancelBtn.addActionListener(
                        new ActionListener(){
                              public void actionPerformed(ActionEvent e){
                                    
                                    dispose();
                              }
                        }
                  );
                  
                  JPanel mainPanel = new JPanel();
                  mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
                  mainPanel.add(nameLbl);
                  mainPanel.add(nameTxt);
                  mainPanel.add(passLbl);
                  mainPanel.add(passTxt);
            
                  JPanel btnPanel = new JPanel();
                  btnPanel.add(loginBtn);
                  btnPanel.add(cancelBtn);
                  
                  mainPanel.add(btnPanel);
                  
                  getContentPane().add(mainPanel);
      }
      
      public static void main(String[] args){
            
            JDialogTest jdt = new JDialogTest();
            jdt.setSize(250,150);
            jdt.show();
      }
}


0
 
LVL 1

Expert Comment

by:Dave_Toland
ID: 12129897
you could also put a constructor into it that takes a String parametre, then as the first line within the constructor make a call to super with the string to set it as the title of the dialog box

public JDialogTest(String title){
 
      super(title);
}
0
 
LVL 1

Assisted Solution

by:Dave_Toland
Dave_Toland earned 500 total points
ID: 12129905
sorry that should be:

      public JDialogTest(String title){
 
            setTitle(title);
      }
0
 

Author Comment

by:HycelTaylor
ID: 12130060
Hey Dave Toland,

Your solution worked.

Thank you very much.
0
 
LVL 1

Expert Comment

by:Dave_Toland
ID: 12131670
anytime
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question