Improve company productivity with a Business Account.Sign Up

x
?
Solved

Creating a Custom Java Swing Dialog

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

585 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