Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Making JFrame a parent of JDialog

630556Jun 14 2008 — edited Jun 14 2008
I decided to start this post again because i was going a bit of the rails before. For now, i am keeping to my original code so i can see where i am going wrong. My JFrame class is my Title class. I have other buttons in this class but the one to concentrate on at the moment is my btnLogin which brings up my Login class, which is a Jdialog. Because the action event on this button was in an inner class, i couldnt make Title the direct parent. So i have made a few changes and made the action event call another class, so that hopefully i can make Title its parent. Here is the code for Title.
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.*;
 
public class Title extends JFrame {
 	
	public Title() {

        JPanel cp = new JPanel();
		cp.setLayout(new BorderLayout());
 
        Icon image = new ImageIcon("london.png");
		JLabel centerPanel1 = new JLabel(image);

        cp.add(centerPanel1, BorderLayout.CENTER);
		cp.add(createTitlePanel(), BorderLayout.NORTH);
		cp.add(createButtonPanel(), BorderLayout.SOUTH);
 
		setContentPane(cp);
		setTitle("Ner);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
 
	}
    
	private JPanel createButtonPanel() {
    	
            JButton btnRegister = new JButton("REGISTER");
            JButton btnLogin = new JButton("LOGIN");
            JButton btnExit = new JButton("Exit");
       
            btnRegister.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    new Register();
                }
            });
           
            btnLogin.addActionListener(new LoginListener(this));
           
           
            btnExit.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) { 
                     cancel();
                }
            });
		JPanel panel = new JPanel();
		JPanel temp = new JPanel(new GridLayout(1,2, 5,5));
		temp.add(btnRegister, BorderLayout.LINE_START);
		temp.setPreferredSize(new Dimension(300, 40));
		temp.add(btnExit, BorderLayout.CENTER);
		temp.setPreferredSize(new Dimension(300, 40));
		temp.add(btnLogin, BorderLayout.LINE_END);
		temp.setPreferredSize(new Dimension(300, 40));
		panel.add(temp);
		return panel;
	}


	
	private JPanel createTitlePanel()  {
		
		JLabel titleLabel = new JLabel("OFFICIAL RANKINGS");
		titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 48));
		titleLabel.setForeground(Color.black);
		JPanel titlePanel = new JPanel();
		titlePanel.setOpaque(false);
		titlePanel.add(titleLabel);	
		return titlePanel;
	}
	
public void cancel(){
	
	this.dispose();
	}
	
    public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Title().setVisible(true);
			}
		});
	}
	
 class LoginListener implements ActionListener {
         JFrame jf;
         LoginListener(JFrame jf) {
              this.jf = jf;
         }
         public void actionPerformed(ActionEvent evt) {
            new Login(jf);    
        }    
    }

 
}
{code}

Now in my Login class, i have created a constructor which accepts a father as an arguement, so that Login can have a parent.  Here is the code to this class.
{code}
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.*;

public class Login extends JDialog implements ActionListener
{
ArrayList personsList;
PersonDAO pDAO;

JLabel userName, passWord;
JTextField userName1;
JPasswordField passWord1;
JButton jbnClear, jbnSubmit, jbnCancel;

String userName2, passWord2;
Container cPane;

public static void main(String args[]){
new Login(); 
}

public Login(JFrame father) {
super(father);
}

public Login()
{ 	
userName2  = "";
passWord2   = "";


createGUI();

personsList = new ArrayList();
	
}
public void createGUI(){


cPane = getContentPane();
setLayout(new GridBagLayout());

//Arrange components on contentPane and set Action Listeners to each JButton
arrangeComponents();

setSize(210,170);
setTitle("Login");
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

}

public void arrangeComponents(){

userName = new JLabel("Username");
passWord = new JLabel("Password");

userName1   = new JTextField(20);
passWord1   = new JPasswordField(20);

jbnClear  = new JButton("Clear");
jbnSubmit = new JButton("Submit");
jbnCancel = new JButton("Cancel");

GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
gridBagConstraintsx01.gridx = 0;
gridBagConstraintsx01.gridy = 0;
gridBagConstraintsx01.insets = new Insets(5,5,5,5); 
cPane.add(userName, gridBagConstraintsx01);

GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
gridBagConstraintsx02.gridx = 1;
gridBagConstraintsx02.insets = new Insets(5,5,5,5); 
gridBagConstraintsx02.gridy = 0;
gridBagConstraintsx02.gridwidth = 2;
gridBagConstraintsx02.fill = GridBagConstraints.BOTH;
cPane.add(userName1, gridBagConstraintsx02);

GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
gridBagConstraintsx03.gridx = 0;
gridBagConstraintsx03.insets = new Insets(5,5,5,5); 
gridBagConstraintsx03.gridy = 1;
cPane.add(passWord, gridBagConstraintsx03);

GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
gridBagConstraintsx04.gridx = 1;
gridBagConstraintsx04.insets = new Insets(5,5,5,5); 
gridBagConstraintsx04.gridy = 1;
gridBagConstraintsx04.gridwidth = 2;
gridBagConstraintsx04.fill = GridBagConstraints.BOTH;
cPane.add(passWord1, gridBagConstraintsx04);

GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
gridBagConstraintsx09.gridx = 0;
gridBagConstraintsx09.gridy = 4;
gridBagConstraintsx09.insets = new Insets(5,5,5,5); 
cPane.add(jbnClear, gridBagConstraintsx09);

GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
gridBagConstraintsx10.gridx = 1;
gridBagConstraintsx10.gridy = 4;
gridBagConstraintsx10.insets = new Insets(5,5,5,5); 
cPane.add(jbnSubmit, gridBagConstraintsx10);

GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
gridBagConstraintsx11.gridx = 1;
gridBagConstraintsx11.gridy = 5;
gridBagConstraintsx11.insets = new Insets(5,5,5,5); 
cPane.add(jbnCancel, gridBagConstraintsx11);

jbnClear.addActionListener(this);
jbnSubmit.addActionListener(this);
jbnCancel.addActionListener(this);
}

public void actionPerformed (ActionEvent e){


if (e.getSource() == jbnClear){
clear();
}

else if (e.getSource() == jbnSubmit){			
Submit();
}

else if (e.getSource() == jbnCancel){			
cancel();
}

}
public void clear(){

userName1.setText("");
passWord1.setText("");

personsList.clear();
} 

public void Submit(){

userName2=userName1.getText();
passWord2 = new String(passWord1.getPassword());

PersonInfo person = new PersonInfo(userName2, passWord2);

if(userName2.equals("") || passWord2.equals("")){

JOptionPane.showMessageDialog(null, "Please complete all fields.");

}
else
{
pDAO.loginPerson(person); 
}	 

}

public void cancel(){

this.dispose();

}

}
{code}

Everything now compiles fine, and i get no runtime errors, but when i click the Login button on my Title page, nothing comes up.  So this is where i am at now.  Can anyone see my mistake?
cheers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 12 2008
Added on Jun 14 2008
11 comments
866 views