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.

Results 1 to 4 of 4

Thread: Multiple listeners? || one program or one for everyone?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Multiple listeners? || one program or one for everyone?

    Hello, I have more than one question.


    1.I will need to open a "skin selection"(it will be some jlabels with imageicon set as images). I will add MouseListeners to them. My "controller" class is in a different pacakge. I will want to notify when the user clicks any of the images. I will create a custom listener. And it will be my second listener, and the program is like 10% done. Is that normal? Or should I create one listener and add ALL the data to it? Although the data is diferent.
    1.1 How do I know which "skin" was selected? I don't want to add 300(the amount of skins) private instance variables.
    1.2 I will need to load 300 images. It seems it's taking a while to do it. What about loading it into memory before? How non-efficiect would that be?


    2.My program will(i hope) run on a website and allow multiple users to use it. I'm afraid that there will be only ONE program and everyone will see the same one. Is the main method called everytime someone opens it?

    3. My program will have login,registration and so on. For example, once a user logs in I show him the "profile" menu. To hide the login GUIs I just getContentPane and removeAll. Is that the way to do it?


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

    Default Re: Multiple listeners? || one program or one for everyone?

    Perhaps instead of JLabels, you should use JButtons (or some variant of that).

    Why don't you store the skins in an array? That way you don't need a new variable for each one.

    Have you looked into somehow "lazy" loading them? Meaning, only loading one when the user asks for it?

    If your program runs on a website, is it an applet? If so, applets are downloaded to the client, so every client gets their own version.

    I use something called: CardLayout to do this. Basically, you add each "screen" as a JPanel to the layout's list of cards. You can then swap in and out the cards programatically. So the program starts on the login screen. Once the user logs in, you swap out the login screen with the profile screen (or whatever). The states of all the "screens" do not change when you swap between them, because they are all stored in memory.
    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/

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Multiple listeners? || one program or one for everyone?

    Storing skins in an array. I didn't want that because I thought that will take a lot of memory to do so. But now that I think about it... I'll do it.

    The "lazy" loading way got me to this question. Because it's slow. Once the user clicks to open the selection it freezes until all images are loaded(I added printlines to see).

    Well I will channge it to an applet immediately. I develop all of it for learning purposes only and had no idea it needs to be an applet to run in a website

    I'll look into this "CardLayout", thanks

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

    Default Re: Multiple listeners? || one program or one for everyone?

    The increased memory is not significant, and it really is by far the best way of approaching the problem.

    You should load the image on a different thread. Do some research on multithreading. Computational tasks (especially those that are lengthy) should not be performed on the same thread as the GUI, because doing this causes "freezing" or "lock ups". You should start a new thread, begin the process on the new thread, and then provide the user with some sort of loading message on the GUI thread. SwingWorker is a good jumping off point for this: Worker Threads and SwingWorker (The Java™ Tutorials > Creating a GUI With JFC/Swing > Concurrency in Swing)

    Yes. An applet is required to run in a browser (although, be warned, Oracle is making some security changes this month which are expected to effect applets significantly). It is very similar to an Application. Applets do not have mains, but rather a class which extends from JApplet or Applet. You then specify the "main" class when you embed the applet in the browser (preferably using JNLP: Java Network Launch Protocol (The Java™ Tutorials > Deployment > Deployment In-Depth))
    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/

Similar Threads

  1. Listeners not working!
    By Alex-Green in forum Member Introductions
    Replies: 1
    Last Post: February 19th, 2012, 06:49 PM
  2. 1 Server- Multiple Clients Program
    By jclark1186 in forum Java Networking
    Replies: 2
    Last Post: September 23rd, 2011, 11:44 AM
  3. Serializing Listeners
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 11th, 2011, 04:47 PM
  4. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 08:27 AM
  5. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 08:44 PM