Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How To Make an Array from Multiple Selection of a Drop-Down Menu (or Scrollable List)

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a drop-down menu (or a scrollable list). Visitors can make multiple selection from the list. How do I put those selected items into an array?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The specific answer depends, of course, on what we're talking about -- a JList, or JComboBox, or a LIST in a JSP, or an SWT List, or ... Once you specify what you're asking about, exactly, it's generally a simple question. In fact, most of the above options simply have an API method that returns an array or Collection of the selected items.
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I will be reading multiple selections from a drop-down menu (users highlight selections by pressing the Ctrl key). And then, I would like to put the selections into a Java array.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
The specific answer depends, of course, on what we're talking about -- a JList, or JComboBox, or a LIST in a JSP, or an SWT List, or ... Once you specify what you're asking about, exactly, it's generally a simple question. In fact, most of the above options simply have an API method that returns an array or Collection of the selected items.


You didn't answer my question -- you just restated your original post without giving any new information!
[ October 04, 2004: Message edited by: Ernest Friedman-Hill ]
 
Daniel Gee
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not being very clear about my question. Okay, I have a clearer idea about what I am working on:

I get a Collection of JavaBeans from a database table. I then create a table in a Java Server Page (JSP). Each row of the table displays the properties of the individual JavaBean in the Collection. And each row has a check box. Putting a check mark in a check box results in submiting the email address of a mail recipient.

Users of the JSP make multiple selections using the check boxes. The multiple choices can result in tens of thousands of selections. The size of the selections is a variable.

The goal is to send a batch of e-mails out. I have to store the multiple choices somewhere. Originally, I thought it was an array. Now, an array is not a good idea because the size of the selections is unknown.

How do I convert multiple choices into some kind of storage and then a batch mail can be sent out?
 
Daniel Gee
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I could explain my self better.

I am creating check boxes in a JSP. I am showing the HTML code to make it simple:


I have to get all the "e-mail addresses" of those who are selected (if a client puts a check mark in the check box). Therefore, I cannot predict ahead of time how many e-mail addresses will be read by my web application.

In a servlet, how to get all the values of the "emailAddress" from a long list shown above (that is displayed by code in a JSP) and put them in a proper storage; for example, ArrayList, Vector, or ......

Yes, I am going to have String(s) - e-mail address(es).

My questions are:

1. Because I may have tens of thousands of String(s) and the actual numbers of those String(s) cannot be predicted, shoudl I use an "Array"? What is proper to use in my case; ArrayList or something else?

2. In a servlet, how to get the values of checked boxes in a JSP?

[EJFH: Fixed CODE tag.]
[ October 05, 2004: Message edited by: Ernest Friedman-Hill ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now you've explained your question well enough to answer.

The code that handles the form submittal could be in the same JSP (although in a different invocation) or it could be in a servlet. I'm not going to talk about application architectures here: other people in this forum will be sure to chime in on that.

In either case, all the values of the checked emailAddress inputs will be available together as a String array by calling getParameterValues("emailAddress") on your HttpServletRequest object, so I think you don't need to worry about what you asked about.
[ October 05, 2004: Message edited by: Ernest Friedman-Hill ]
 
Daniel Gee
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I created a String ArrayList instead of a String Array by calling getParameterValues("emailAddress") on the HttpServletRequest object?

The length of this Array is a variable and could be a very large one.

Vector is not a good choice because "thread safe' is not required.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you feel a need to convert it to anything else?
 
Daniel Gee
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because an Array has fixed length. In my case, the length is a variable and the number of checked "check boxes" can be very large.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when you make the call to getParameterValues() an array of the correct size, no matter how large, is returned already filled in with request parameter values. Why do you feel the need to change it?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic