File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes <ArrayList> help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "<ArrayList> help" Watch "<ArrayList> help" New topic
Author

<ArrayList> help

Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
I am going to have ALOT of JMenuItems and i wanted to know if i could use two <ArrayList>s, one for the JMenuItems and one for the String names. will this work? and if not how can i make this better? thanks in advance!!!


Duct tape is like the Force. It has a dark side, a light side, and it binds the whole universe together.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Cody Long wrote:I am going to have ALOT of JMenuItems and i wanted to know if i could use two <ArrayList>s, one for the JMenuItems and one for the String names. will this work? and if not how can i make this better? thanks in advance!!!


By String names, do you mean the Action Commands, and also the text shown with each JMenuItem? I see no reason why you shouldn't be able to do that. There are other options too. But I don't see how storing the actual names of the JMenuItem objects themselves in a separate ArrayList is useful, even if you could do it, and I'm not sure you can.

p.s. In effect, the JMenuItems are referenced by their position in the array List, so they won't have names in any other sense than that.

Get my drift?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Are you planning on having element i of the string list match element i of the menu item list, for all possible values of i? If so, you may want to check out a Map<String,JMenuItem>. If insertion order is important you can use a LinkedHashMap:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Rob Prime wrote:Are you planning on having element i of the string list match element i of the menu item list, for all possible values of i? If so, you may want to check out a Map<String,JMenuItem>. If insertion order is important you can use a LinkedHashMap:


hmm ok, hadn't thought of that, but sure that would work. I'm not sure I see any point though. except that it would provide a little flexibility in how you name and reference an object, but at a cost of a fair bit of extra work?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

It will help if you want to quickly find a JMenuItem based on its name - you won't have to loop through all JMenuItems.

Ehm Cody, you do know that more menu items does mean more confusion for users, right? Usually you shouldn't get more than 10 or so menus, each with no more than 20 or so items, and no more than 3 levels of menu items. This is not a hard rule, but the more menus / menu items / levels, the harder it is to find one specific item.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Rob Prime wrote:It will help if you want to quickly find a JMenuItem based on its name - you won't have to loop through all JMenuItems.

....


Am I missing something here, if so I's like to get it right. You are talking about searching programmatically?

OK, I think by name you mean the text associated with the menu item, as oppose to an object name? Cause if you know the object name, then you wouldn't need to search for it, agreed?

I can live with your answer, I understand the how, the mechanics of it all, it's the why that I don't get. What's the point of storing all these items in an ArrayList if you are not going to reference them that way? If you have a practical scenario for doing that, I'm interested.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Rob's point was to *not* store them in an List, but in a Map, so it's an O(1) lookup.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
reading all of these comments made me realize that i should make alot lot less menu items and make a few more windows. thanks for all of your help!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
David Newton wrote:Rob's point was to *not* store them in an List, but in a Map, so it's an O(1) lookup.


Ah.. thanks David, That makes much more sense, I thought he was talking about a list and a map. I like the map idea now that I understand it.

Cody, you might be right about using windows, but don't give up on lots of munuitems yet. While I agree that a multitude of menu items can be confusing, if you group them appropriately in submenus, then maybe things won't be so bad.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Fred is right. First see how it will work with all those menu items; perhaps "LOTS" is not as bad as it seems.

Just make sure you let someone else try it as well; prorgammers and end users usually have different notions of what a good GUI is.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
Fred is right. First see how it will work with all those menu items; perhaps "LOTS" is not as bad as it seems.

Just make sure you let someone else try it as well; prorgammers and end users usually have different notions of what a good GUI is.


very true.


thanks for all of your help. i am going to make two different versions, one with a plethora of menu items and the other that uses more windows. i am also going to see if i can make a version in c++ just because i have a bunch of spare time. Agian thank you alll for your help!!!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Cody Long wrote:
Fred is right. First see how it will work with all those menu items; perhaps "LOTS" is not as bad as it seems.

Just make sure you let someone else try it as well; prorgammers and end users usually have different notions of what a good GUI is.


very true.


thanks for all of your help. i am going to make two different versions, one with a plethora of menu items and the other that uses more windows. i am also going to see if i can make a version in c++ just because i have a bunch of spare time. Agian thank you alll for your help!!!


If you have the time, it's a good idea, trying different ways of doing things only increases your learning. It provides an opportunity to learn about portability and modularity too. By properly designing your listeners and event handlers, a large portion of your work should be re-usable in both versions.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
Here is the link to download my program that i wrote in c++. it took a while but this is what i want my menus to be like. any help would be apreciated on how to make this work in java.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Cody Long wrote:Here is the link to download my program that i wrote in c++. it took a while but this is what i want my menus to be like. any help would be apreciated on how to make this work in java.

As said before in another thread, I doubt people will want to install a program just to see the menu structure. Just create a few screen shots (the Prt Scr button will be enough) and post those here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <ArrayList> help