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 4 of 4 FirstFirst ... 234
Results 76 to 80 of 80

Thread: Multiple instances of a class

  1. #76
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Multiple instances of a class

    Like with the custom ActionListener. In the MembersActionListener (the last one I posted), we passed a Members object in the constructor, which we used to set a variable in the class instance.
    public class MembersActionListener implements ActionListener {
    	private Members member;
     
    	public MembersActionListener(Members memb) {
    		member = memb;
    	}
     
    	public Members getMember() {
    		return member;
    	}
    ...

    We would do the same thing for a MainFrame object. Just add another parameter in the constructor and another instance variable in the class for the MainFrame object. Then when you create your MembersActionListener objects, you would have to pass the MainFrame object as well.

    There is an alternative which we could use if you only intend on having one MainFrame object at any time. What we could do is create a static variable in the MembersActionListener class and set that variable statically when you create your MainFrame object. Every MembersActionListener object you create from then on will already have a reference to the specified MainFrame object without you having to do anything else. But, like I said, this would ONLY work if you only have ONE MainFrame object initialized in your program.
    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/

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

    Lora_91 (November 25th, 2013)

  3. #77
    Member
    Join Date
    Oct 2013
    Posts
    78
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    Thanks, got it to work! =)

    I've also gotten logging to work to some degree (storage of log in/out data), as well as all the data saved and loaded in .text files whenever the program is closed/loaded, each existing member has its own text file created, and all non-used text files are removed.

    My current issue however is in the Mainframe.java, where I display all of the members and their buttons. They are all displayed and positioned correctly, and update accordingly whenever someone is added/removed, the issue is that when I have enough users they go off "screen" (more like off panel) and the scroll bars refuse to show up.

    Up until recently the memberPanel (which contains the member buttons and labels that are displayed as users) didn't have any layout (set to null), it made it easy to position everything the way I wanted it to. However, if it doesn't have any layout the scrollbars don't seem to recognize that they are supposed to show up:
            memberPanel = new JPanel();
            memberPanel.setLayout(null);
            memberPanel.setLocation(0,0);
            //memberPanel.setSize(TextFieldWidth -20,TextFieldHeight);
     
            memberPanel.setBorder(BorderFactory.createLineBorder(Color.red));
            //memberPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
     
     
            memberScrollPanel = new JScrollPane(memberPanel);
            memberScrollPanel.setSize(TextFieldWidth,TextFieldHeight);
            memberScrollPanel.setLocation(0,0);
            memberScrollPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
            memberScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
     
            JPanel memberPanel2 = new JPanel(null);
            memberPanel2.setLayout(null);
            memberPanel2.setLocation(50,80);
            memberPanel2.setSize(TextFieldWidth,TextFieldHeight);
            memberPanel2.add(memberScrollPanel);
            memberPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

    With this I force a scrollbar to show, but it doesn't recognize that the frame expands even when it goes off the frame, so it's currently not usable for anything.

    My friend asked me to try using GridLayout instead, and with that the scrollbar does work, but gridlayout also unfortunately resizes everything to be the same size, so with just a couple of users the height is really awkwardly high, which then shrinks down as more users are added to the intended height, but the width remains incorrect, especially for the button and index number.

    Is there any way to get the scrollbar to work with null layout, or any way to fix gridlayout to work the way I want it to, or any other layout that would work? =/

  4. #78
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Multiple instances of a class

    Your scroll panel has a fixed size, as does your member panel. Your member panel is inside your scroll panel. The scroll bars will only show up if the member panel's size exceeds the scroll panel's size. When using GridLayout, I believe the member panel is automatically resized to accommodate the components. Since the panel gets larger, the scroll panel's scroll bar activates (the scroll panel's size does not change with the member panel). When you use a null layout, the size of the member panel does not change when you add components, so the scroll bars do not change. You are probably placing components beyond the bounds of the member panel, which does not throw an exception or anything (there is a legitimate argument for why java probably should add an exception when you do this, but one doesn't exist now).
    What you want to do is resize the member panel after you add components. For the most reliable success, you should set the size of the JPanel using BOTH the setPreferredSize() and setMinimumSize() methods. Some components look at the former, some look at the latter. If you set both of those to the same dimensions, you will have far better success than just setting one of them. Using the setSize() method on JPanel is not very reliable. You should use the two methods I mentioned above to set the size.
    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/

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

    Lora_91 (December 4th, 2013)

  6. #79
    Member
    Join Date
    Oct 2013
    Posts
    78
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Re: Multiple instances of a class

    I've progressed quite a bit with the program and it should be getting close to finished, thanks to all of the help you've given me!

    Right now I'm trying to add a way to print out all of the dates between a specified start and end date to a printer. As of currently, whenever a user logs in or out, a new date is created and stored along with the user account:
    (Within membersActionListener.java)
    mainFrame.date = new Date();
    member.logOutDates(mainFrame.date);

    (Whithin Member.java)
        Date [] logindates = new Date[900];
        Date [] logoutdates = new Date[900];
     
     
     
     
        public void logInDates(Date incomingDate)
        {
            logindates[numberoflogins] = incomingDate;
            numberoflogins++;
        }
     
        public void logOutDates(Date incomingDate)
        {
            logoutdates[numberoflogouts] = incomingDate;
            numberoflogouts++;
        }

    The creation and storage of the dates works well, it's even saved to and loaded from a .txt file for each individual user without problem. The issue is that I'm not quite sure as how to convert what the user types in into a date variable that can be compared to the stored dates.

    The date = new Date(); format currently gets this kind of result:
    "Wed Dec 04 12:22:20 CET 2013"

    I feel that would be really awkward to type in when specifying the start and end dates. Something like "2013 Dec 04 12:22:20" would be a lot easier, perhaps even without the seconds. But I'm not sure of how to get it to work.

    This is what my current code looks like when you press the "printerButton", before pressing it the user has written in the start and end dates in two JTextFields:

    	printerButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e)
    		{
    			try 
    			{
    			    String startDateText = startDate.getText();
    			    String endDateText = endDate.getText();
     
    			    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); //Currently not being used, not sure how to use it. =/
    			    SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMM dd", Locale.ENGLISH);
     
    			    //SimpleDateFormat formatter = new SimpleDateFormat("EEEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
    			    Date date;
     
    			    date = formatter.parse(startDateText); //Currently what gives out the error "java.text.ParseException: Unparseable date: "2013 08 15""
     
    			    Calendar cal;
     
     
     
    				int i = 0;
    				for(Members member : mainFrame.membersList)
    				{
    					if(member.logindates[i].compareTo(date) == 0)
    					{
    						JOptionPane.showMessageDialog(null, "0"); //Just trying to get the hang of comparing dates
    					}
     
    					if(member.logindates[i].compareTo(date) < 0)
    					{
    						JOptionPane.showMessageDialog(null, "< 0");
    					}
     
    					if(member.logindates[i].compareTo(date) > 0)
    					{
    						JOptionPane.showMessageDialog(null, "> 0");
    					}
     
    					i++;
    				}
     
    				} catch (ParseException e1) {
    					e1.printStackTrace();
    			}
     
    		}});

    Would it be possible to change the way the dates are stored to "yyyy MMM dd HH:mm:ss"? That would probably solve the issue.

    If not, how would I go about making it as user friendly as possible to enter the start and end dates?

  7. #80
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Multiple instances of a class

    DateFormat is a the parent class of SimpleDateFormat. If you are using SimpleDateFormat, you should not need a DateFormat object as well (at least not for your purposes).

    You are receiving the error most likely because in your SimpleDateFormat, you specified a 3 digit month (MMM), while you are providing a two digit month in the date text you are providing (08). From the API:
    Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
    This means that MMM is allowed, but it is expecting the name of a month, not the digits for the month. So, it is expecting "2013 Sep 15" instead of "2013 08 15" (I'm not sure if 08 is September or not, it depends on whether or not the DateFormat treats months like indexes or just numbers).
    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/

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

    Lora_91 (December 11th, 2013)

Page 4 of 4 FirstFirst ... 234

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, 10:46 PM
  3. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 08:42 AM
  4. Replies: 0
    Last Post: December 1st, 2010, 06: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