Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 80

Thread: Multiple instances of a class

  1. #26
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Yeah sorry, it's a little bit difficult to explain so I understand if you're confused. I'll try to explain it better though.

    So I have two JFrames, the first one is in the MainFrame.java and has the button "addmemberbutton", which is supposed to open up a second JFrame, wait until the user has entered all the information in the fields and when the user presses the "completebutton" the information will be stored in the new Members instance we just created.

    So here I create the new instance of Membes:

            if(e.getSource() == AddMemberButton)
            {
                while(cancel == false)
                {
                            MA++;
                            int loginValue = 0;
                            Members muffin;
                            muffin = memberFrame.createAndShowMemberFrameGUI();
    Which runs the createAndShowMemberFrameGUI:

    	public static Members createAndShowMemberFrameGUI() 
    	{
    		//JFrame.setDefaultLookAndFeelDecorated(true);
    		//JFrame frame = new JFrame(WindowName);
    		memberFrame MF = new memberFrame();
    		MF.frame.setContentPane(MF.createContentPane());
    		MF.frame.setSize(WindowWidth,WindowHeight);
    		MF.frame.setLocationRelativeTo(null);
    		MF.frame.setResizable(false);
    		MF.frame.setVisible(true);
     
    		while(true)
    		{
    			if(done == true)
    			{
    				return newMember;
    			}
     
    			if(cancel == true)
    			{
    				return null;
    			}
     
    			/*
    			try 
    			{
    				Thread.sleep(1000);
     
    			} catch (InterruptedException e) 
    			{
    				e.printStackTrace();
    			}
    			*/
    		}
     
     
     
    	}

    And that creates the second JFrame within:

    	public JPanel createContentPane()
    	{
    //lots of code setting up the JFrame which isn't relevant to the issue so I won't bore you with it =)

    The issue however is that createAndShowMemberFrameGUI() needs to return a value to muffin = memberFrame.createAndShowMemberFrameGUI(); when called upon, and those values are meant to be taken from the second JFrame which is just being created. So I need to hold off on the return of the value until the user has actually submitted these values in the second JFrame and pressed the complete button, but currently createAndShowMemberFrameGUI(); doesn't want to do that but rather return the values before the second JFrame is even opened.

    I hope this explained the issue I'm having better. =/

  2. #27
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,491
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Multiple instances of a class

    Better explanation. I stand by my previous comments. Use a modal JDialog instead of a JFrame to collect and return data from the user before continuing.

  3. The Following 2 Users Say Thank You to GregBrannon For This Useful Post:

    aussiemcgr (October 20th, 2013), Lora_91 (October 25th, 2013)

  4. #28
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Edit: I figured out a couple of the issues but ran into a new problem, it claims not to be able to reach the code where I return the values, even though it does allow me to enter the values and press the completebutton:

        public static Members createAndShowMemberFrameGUI()
        {
            //JFrame.setDefaultLookAndFeelDecorated(true);
            //JFrame frame = new JFrame(WindowName);
     
     
        	frame = new JFrame(WindowName);
     
            memberFrame MF = new memberFrame();
     
     
            dialog1 = new JDialog(frame);
            dialog2 = new JDialog(dialog1, "", Dialog.ModalityType.APPLICATION_MODAL);
     
            memberFrame.dialog2.setContentPane(MF.createContentPane());
            memberFrame.dialog2.setSize(WindowWidth,WindowHeight);
            memberFrame.dialog2.setLocationRelativeTo(null);
            memberFrame.dialog2.setResizable(false);
            memberFrame.dialog2.setVisible(true);
     
            while(true)
            {
                if(done == true)
                {
                    return newMember;
                    JOptionPane.showMessageDialog(null,"Returning"); //Error, unreachable code
                }
     
                if(cancel == true)
                {
                    return null;
                }
     
     
                try
                {
                    Thread.sleep(1000);
     
     
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
     
            }
     
     
     
     
        }


        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == CompleteButton)
            {
                done = true;

    Also, with this method the JDialog needs to be declared within the static method which means it has to be a static variable? I've heard from quite a few people that static variables should be avoided if possible, and I'm trying not to use them, but I consistently get errors along the lines of "cannot make a static reference to the non-static ---" or something like that. It all links back to the
    public static void main(String [] args){
    which starts up the program, and I'm pretty sure that one has to be static? =/

  5. #29
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    The main method is static. All methods and instance variables used in a static method must also be static. Now, there is a simple way to get around this: do your work in the class constructor instead of the main.
    Have a look at the two code examples below. They do the same thing, but the first one requires the methods to be static, while the second one does not:
    With Statics:
    public class SomeClass {
    	/**
    	 * This variable must be static because it is directly referenced in the
    	 * static getBool() method, and in the main
    	 */
    	public static boolean bool;
     
    	/**
    	 * This method must be static because it is directly referenced in the main
    	 */
    	public static boolean getBool() {
    		return bool;
    	}
     
    	public static void main(String[] ags) {
    		bool = true;
    		System.out.println(getBool());
    	}
    }
    Without Statics:
    public class SomeClass {
    	/**
    	 * This variable does not need to be static because it is not directly 
    	 * referenced in any static methods
    	 */
    	public boolean bool;
     
    	/**
    	 * Default Constructor for this class
    	 */
    	public SomeClass() {
    		bool = true;
    		System.out.println(getBool());
    	}
     
    	/**
    	 * This method does not need to be static because it is not directly 
    	 * referenced in the main
    	 */
    	public boolean getBool() {
    		return bool;
    	}
     
    	public static void main(String[] ags) {
    		/* We simply create a new instance of this class. Since the new instance
    		 * exists only inside of the static method, the instance and all of its
    		 * methods and variables do not need to be static. The constructor is
    		 * referencing the bool variable and the getBool() method, not the main
    		 * method.
    		 */
    		new SomeClass(); 
    	}
    }

    Tell me if that makes sense. Once we get this resolved, we can see if your other problems still exist.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  7. #30
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Thanks, that does make sense and I was able to get rid of all of the static variables and methods! =)

    Although without the static my method doesn't seem to know how to find the other class' method, even though it's public:

    (MainFrame.java)
            if(e.getSource() == AddMemberButton)
            {
                while(cancel == false)
                {
                            Members muffin;
                            muffin = createGUI(); //error: the method createGUI() is undefined for the type MainFrame

    Trying to refer to the memberFrame class' method createGUI():

    (memberFrame.java)
        public Members createGUI()
        {	
        	frame = new JFrame(WindowName);
     
            memberFrame MF = new memberFrame();
     
     
            dialog1 = new JDialog(frame);
            dialog2 = new JDialog(dialog1, "", Dialog.ModalityType.APPLICATION_MODAL);
     
            dialog2.setContentPane(MF.createContentPane());
            dialog2.setSize(WindowWidth,WindowHeight);
            dialog2.setLocationRelativeTo(null);
            dialog2.setResizable(false);
            dialog2.setVisible(true);
     
            while(true)
            {
                if(done == true)
                {
                    return newMember;
                    //JOptionPane.showMessageDialog(null,"Returning");
                   // frame.dispose();
                }
     
                if(cancel == true)
                {
                    return null;
                }   
            }
        }

    It seems to only be looking within the MainFrame class, and memberFrame.createGUI can only be used if it's static, which I would prefer to avoid.

  8. #31
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    createGUI() does not need to be static. You need to invoke the createGUI() method on a MemberFrame object.

    For example, I assume somewhere in your MainFrame class, you created a MemberFrame object (for this example, we'll name it: memberFrame). To invoke the createGUI() method, all you have to say is:
    MemberFrame memberFrame = ...; // At some point in your MainFrame class, you have already done this
    ...
    /* With the exception of class instance variables, 
     * all variables should explicitly be given a default 
     * value. For objects, this value is usually null 
     */
    Member muffin = null; 
     
    /* Invoke the method on your memberFrame
     * object you created earlier in the class.
     */
    muffin = memberFrame.createGUI();
    ...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  10. #32
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Hm, I had missed doing that, and it would appear I'm not entirely sure of how to do it. I added:

    	private memberFrame memberFrame;
        memberFrame MemberFrame = memberFrame;
    Into the variables of MainFrame.java, and it did cause muffin = memberFrame. to find the createGUI(); without any errors.

    However, when running the program, and pressing the AddMemberButton I got a bunch of errors all starting with:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at LoginSystem.MainFrame.actionPerformed(MainFrame.ja va:253)
    Which is the muffin = memberFrame.createGUI(); line

    So I'm guessing I didn't set up the memberFrame object variable correctly?

  11. #33
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    I don't exactly understand what you did. Can you post your MainFrame class? If it is too large, just post the sections were you mention MemberFrame.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  13. #34
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    I added it at the top together with all of the other variables:

    public class MainFrame implements ActionListener
    {
     
    	public memberFrame memberFrame;
        memberFrame MemberFrame = memberFrame;


    And this is when I use it in the actionlistener:
            if(e.getSource() == AddMemberButton)
            {
                while(cancel == false)
                {
                    Members muffin;
                    muffin = memberFrame.createGUI(); //nullpointer exception

  14. #35
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    Why do you have the second MemberFrame variable? And do you ever actually initialize your memberFrame object (call it's constructor)?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  15. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  16. #36
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    I was confused with what to initialize it with and got an error that asked me to change it into that. =/

    public class MainFrame implements ActionListener
    {
     
        memberFrame MemberFrame = null;

    I must be initializing it incorrectly here as well since I get a nullpointer exception, which wouldn't be strange considering it's equal to null. =/

  17. #37
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    Ok, so in the constructor of your MainFrame class, initialize it with the constructor of the memberFrame class. If you have not created a constructor in the memberFrame class, you can just use the default constructor:
    MemberFrame = new memberFrame();
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  18. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  19. #38
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Ah thanks that worked! =)

    Only issue that seems to be appearing now is that the memberFrame.java class is having issues closing down the second created frame, which is odd considering it worked while it was static. It claims nullpointerexception:
    else if(e.getSource() == CancelButton)
            {
     
                int check = 0;
                String[] options = new String[]{"Yes", "No"};
                check = JOptionPane.showOptionDialog(null,
                        "Are you sure you wish to cancel?",
                        "Attention!",
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.PLAIN_MESSAGE, null, options,
                        options[0]);
     
     
                if(check == 0)
                {
                	dialog2.dispose();
                    //frame.dispose();
                }
     
            }

    Not sure if it should be dialog2.dispose or frame.dispose, but both give the same error, and the JFrame remains open. =/

  20. #39
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    That's not odd. Static methods do not require an initialized instance of an object in order to work.
    In case you do not know what "static" does, making a class variable static makes the variable "instance-less". If ANY instance of a class changes that static variable, ALL instances of that class reflect the change. Here is a simple example you can execute:
    public class Prog {
        /**
         * Static variable
         */
        private static int var;
        /**
         * Constructor
         */
        public Prog() {
            var = 0;
        }
        /**
         * Getter
         */
        public int getVar() {
            return var;
        }
        /**
         * Setter
         */
        public void setVar(int v) {
            var = v;
        }
     
        /**
         * Main
         */
        public static void main(String[] args) {
            Prog prog1 = new Prog();
            Prog prog2 = new Prog();
            Prog prog3 = new Prog();
            System.out.println("Prog1: " + prog1.getVar());
            System.out.println("Prog2: " + prog2.getVar());
            System.out.println("Prog3: " + prog3.getVar());
            prog2.setVar(5);
            System.out.println("Prog1: " + prog1.getVar());
            System.out.println("Prog2: " + prog2.getVar());
            System.out.println("Prog3: " + prog3.getVar());
        }
    }
    The output of this program is:
    Prog1: 0
    Prog2: 0
    Prog3: 0
    Prog1: 5
    Prog2: 5
    Prog3: 5
    Notice how we only called the getter on prog2, but all instances of Prog changed? This was because var is static. If var was not static, the output would be:
    Prog1: 0
    Prog2: 0
    Prog3: 0
    Prog1: 0
    Prog2: 5
    Prog3: 0
    That's a simple little intro on what static means. I know it confused the hell out of me when I first started. Maybe that will make it a little easier to understand.

    If you are getting a null pointer, it means that dialog2 must be null. Have you initialized it with a constructor?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  21. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  22. #40
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Static is indeed confusing, which is one of the reasons I try to avoid it the best I can. =/

    I thought I was initializing them with:
            frame = new JFrame(WindowName);
     
            memberFrame MF = new memberFrame();
     
     
            dialog1 = new JDialog(frame);
            dialog2 = new JDialog(dialog1, "", Dialog.ModalityType.APPLICATION_MODAL);

    But perhaps not, because after doing it when they are created:
        JFrame frame = new JFrame();
        JDialog dialog1 = new JDialog();
        JDialog dialog2 = new JDialog();
    I no longer receive any errors, but the frame isn't closed down either. It's like it's closing down the wrong instance of it?

  23. #41
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    If you are receiving null pointer exceptions after initializing them, you'll need to post the full stack-trace of the error message.
    Perhaps check does not equal 0.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  24. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  25. #42
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    You misunderstand I didn't receive any null pointer exception with this, but I can't seem to close the frame either.
                	dialog2.dispose();
                    frame.dispose();
    One of those two lines is supposed to close down the frame, no? However nothing happens when I try to do it. =/

  26. #43
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    I thought you said you were getting a null pointer exception (back at post #38).
    Put a print statement inside the if statement for those calls. Perhaps the if statement is never being entered in the first place.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  27. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  28. #44
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    I did say that at post 38 yes, but it was resolved in post 40, I apologize if I was unclear. =/

    I added a JOptionPane.showMessageDialog(null,"Dispose");

    And it's being called as it should, but the frame remains open. No errors are given, the message is shown, but nothing else happens. =/

  29. #45
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    If the null pointer was resolved by using the default constructors instead of the other constructors, this is probably not a good sign.
    Are you able to post the entire code for that class?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  30. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  31. #46
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Seems to work, this is the entire memberFrame.java class:

    package LoginSystem;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.reflect.*;
    import java.util.List;
    import java.util.ArrayList;
     
    import javax.swing.*;
    import javax.swing.event.DocumentListener;
     
    public class memberFrame implements ActionListener
    {
     
        JPanel titlePanel;
        JPanel buttonPanel;
        JPanel textPanel;
     
        JLabel StringWelcome;
     
        private JButton CompleteButton;
        private JButton CancelButton;
     
        JTextField firstName;
        JTextField lastName;
     
        JLabel titleLabel = new JLabel("Please fill in your name and desired password below:");
        JLabel firstNameLabel = new JLabel("First name:");
        JLabel lastNameLabel = new JLabel("Last name:");
        JLabel passwordLabel = new JLabel("Password:");
        JLabel repeatPasswordLabel = new JLabel("Repeat password:");
     
        JPasswordField password;
        JPasswordField repeatPassword;
     
        //**********************************************
        //*!CONSTANTS!!CONSTANTS!!CONSTANTS!!CONSTANTS!*
        //*!CONSTANTS!!CONSTANTS!!CONSTANTS!!CONSTANTS!*
        //**********************************************
     
        private int WindowWidth = 370;
        private int WindowHeight = 210;
     
        private String WindowName = "User configuration";
     
        private Members newMember;
        private boolean done = false;
        private boolean cancel = false;
     
     
        JFrame frame = new JFrame();
        JDialog dialog1 = new JDialog();
        JDialog dialog2 = new JDialog();
        public JPanel createContentPane()
        {
            JPanel totalGUI = new JPanel();
            totalGUI.setLayout(null);
     
            int titleSize = 320;
     
            titlePanel = new JPanel();
            titlePanel.setLayout(null);
            titlePanel.setLocation((WindowWidth/2)-titleSize/2,0);
            titlePanel.setSize(titleSize,30);
     
     
            textPanel = new JPanel();
            textPanel.setLayout(null);
            textPanel.setLocation(20,30);
            textPanel.setSize(WindowWidth-40,80);
     
            buttonPanel = new JPanel();
            buttonPanel.setLayout(null);
            buttonPanel.setLocation(20,WindowHeight-80);
            buttonPanel.setSize(WindowWidth-40,40);
     
            titleLabel.setLayout(null);
            titleLabel.setLocation(0,0);
            titleLabel.setSize(titleSize,20);
            titlePanel.add(titleLabel);
     
     
            firstNameLabel.setLayout(null);
            firstNameLabel.setLocation(0,0);
            firstNameLabel.setSize(150,20);
            textPanel.add(firstNameLabel);
     
            firstName = new JTextField();
            firstName.setLayout(null);
            firstName.setLocation(0,20);
            firstName.setSize(150,20);
            textPanel.add(firstName);
     
     
     
            lastNameLabel.setLayout(null);
            lastNameLabel.setLocation(180,0);
            lastNameLabel.setSize(150,20);
            textPanel.add(lastNameLabel);
     
     
            lastName = new JTextField();
            lastName.setLayout(null);
            lastName.setLocation(180,20);
            lastName.setSize(150,20);
            textPanel.add(lastName);
     
     
     
            passwordLabel.setLayout(null);
            passwordLabel.setLocation(0,40);
            passwordLabel.setSize(150,20);
            textPanel.add(passwordLabel);
     
     
            password = new JPasswordField();
            password.setLayout(null);
            password.setLocation(0,60);
            password.setSize(150,20);
            textPanel.add(password);
     
     
            repeatPasswordLabel.setLayout(null);
            repeatPasswordLabel.setLocation(180,40);
            repeatPasswordLabel.setSize(150,20);
            textPanel.add(repeatPasswordLabel);
     
            repeatPassword = new JPasswordField();
            repeatPassword.setLayout(null);
            repeatPassword.setLocation(180,60);
            repeatPassword.setSize(150,20);
            textPanel.add(repeatPassword);
     
            CompleteButton = new JButton("Complete");
            CompleteButton.setLayout(null);
            CompleteButton.setLocation(50,0);
            CompleteButton.setSize(100,40);
            buttonPanel.add(CompleteButton);
            CompleteButton.addActionListener(this);
     
            CancelButton = new JButton("Cancel");
            CancelButton.setLayout(null);
            CancelButton.setLocation(180,0);
            CancelButton.setSize(100,40);
            buttonPanel.add(CancelButton);
            CancelButton.addActionListener(this);
     
     
            totalGUI.add(titlePanel);
            totalGUI.add(textPanel);
            totalGUI.add(buttonPanel);
     
            totalGUI.setOpaque(true);
            return totalGUI;
        }
     
     
        public Members createGUI()
        {
            //JFrame.setDefaultLookAndFeelDecorated(true);
            //JFrame frame = new JFrame(WindowName);
     
     
            frame = new JFrame(WindowName);
     
            memberFrame MF = new memberFrame();
     
     
            dialog1 = new JDialog(frame);
            dialog2 = new JDialog(dialog1, "", Dialog.ModalityType.APPLICATION_MODAL);
     
            dialog2.setContentPane(MF.createContentPane());
            dialog2.setSize(WindowWidth,WindowHeight);
            dialog2.setLocationRelativeTo(null);
            dialog2.setResizable(false);
            dialog2.setVisible(true);
     
            while(true)
            {
                if(done == true)
                {
                    return newMember;
                    //JOptionPane.showMessageDialog(null,"Returning");
                   // frame.dispose();
                }
     
                if(cancel == true)
                {
                    return null;
                }  
            }
        }
     
     
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == CompleteButton)
            {
                done = true;
                String Completefirstname = firstName.getText();
                String Completelastname = lastName.getText();
                String Completepassword = "Yey";
                newMember = new Members(Completefirstname,Completelastname,Completepassword);
                frame.dispose();
            }else if(e.getSource() == CancelButton)
            {
     
                int check = 0;
                String[] options = new String[]{"Yes", "No"};
                check = JOptionPane.showOptionDialog(null,
                        "Are you sure you wish to cancel?",
                        "Attention!",
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.PLAIN_MESSAGE, null, options,
                        options[0]);
     
     
                if(check == 0)
                {
                	dialog2.dispose();
                    frame.dispose();
                    JOptionPane.showMessageDialog(null,"dispose");
                }
     
            }
        }
     
     
     
     
     
    }

  32. #47
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    Why are you doing this in a while loop?
    while(true)
            {
                if(this.done == true)
                {
                    return this.newMember;
                    //JOptionPane.showMessageDialog(null,"Returning");
                   // frame.dispose();
                }
     
                if(this.cancel == true)
                {
                    return null;
                }  
            }

    And you get no exceptions or anything? What happens if you use the CompleteButton? Does frame get disposed?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  33. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  34. #48
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Currently, nothing happens when I press the complete button, I added a JOptionPane.showMessageDialog(null,"Complete"); just to check and it is being run as it should, but the frame isn't closed down same as with cancel. Since it isn't closed down the code never goes back to the loop and a value is never returned.

    The while loop was an idea from my friend, the idea being that the loop would wait for the user until he/she was done putting in all of the information needed and then return the value. Although with JDialog it might not be needed.

  35. #49
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Multiple instances of a class

    The while loop was an idea from my friend, the idea being that the loop would wait for the user until he/she was done putting in all of the information needed and then return the value. Although with JDialog it might not be needed.
    That is not how the while loop would operate. In reality, it locks up the thread it is running on, since it runs forever and never moves on. An infinite loop is really only arguably needed if you are listening to a server socket. In almost every other situation, it can be devastating to your program.
    I'm not sure if it will make a difference, but try removing the while loop and see if the frame closes. I don't think it will make a difference for you, but if that while loop is operating on the same thread as the frames, and the while loop is going forever, that may be why the frame is not closing: the thread is waiting for the while loop to finish before it can process the frame's disposal.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  36. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Lora_91 (October 25th, 2013)

  37. #50
    Member
    Join Date
    Oct 2013
    Posts
    77
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Removed the while loop but it didn't make any difference, since the code never returns back to that part until the JDialog/frame is closed down, and it refuses to be shut down with the dispose call. =/

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. referencing variables from multiple instances of a class?
    By nickdesigner in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 23rd, 2013, 08:24 PM
  2. Log location of multiple instances of jboss...
    By rathi in forum Java Servlet
    Replies: 2
    Last Post: January 23rd, 2012, 09:46 PM
  3. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 07:42 AM
  4. Replies: 0
    Last Post: December 1st, 2010, 05:10 AM
  5. Multiple instances of linked list
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 07:48 PM