?
Solved

Creating a Custom Java Swing Dialog

Posted on 2004-09-22
5
Medium Priority
?
608 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 2000 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 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