Solved

Creating a Custom Java Swing Dialog

Posted on 2004-09-22
5
583 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 33
web project error add remove 1 77
Java array sort 10 59
How to fix  socket closed error 11 60
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

737 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