Please Help List box Issue
I did a post yesterday and this is probably a basic issue so that抯 why I haven抰 got a response yet.
In the list box component world in Java sun Creator is it possible upon a button action to capture everything in the list box with out selecting anything?
The demo that is out there:
http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/lis tbox_components.html
This demo uses a selection to capture what is in the list box and I do not require a selection. I need all of the values that the user inputs into the list box and I do not require the user to select anything from the list box.
I have been stuck on this portion for 2 weeks now and I would like some assistance, perhaps a web page or an example or even advice.
I am using sessionbean1 to bind the list box to a property and I require the add and remove that I currently have working.
The demo on the Java Sun web site is really good, but i can not capture all of the values and is this because the session bean requires that the end user makes a selection?
Thank you for any assistance that you may be able to provide
Message was edited by:
dupey00_ca
Message was edited by:
dupey00_ca
[1251 byte] By [
dupey00_ca] at [2007-11-26 9:32:01]

# 1
I didn't answer your last question because I did not understand what you what. This post is a little clearer.
If you want to get the contents of the list box, then iterate through the object to which the listbox is bound. For example:
String[] selected =
new String[getSessionBean1().getListOptions().length];
Option[] myOptions = getSessionBean1().getListOptions();
for (int j = 0; j < myOptions.length; j++) {
selected[j] = (String) myOptions[j].getValue();
}
Not sure whether this answers your question. Hope so.
Chris
# 2
Thank you but I tried to iterate through the optionlist and I am getting an int returned.
Some thing I have done or omitted is preventing me from selecting all the values from the listbox when an end user clicks the submit button
I am including my code so if some one spots an error or an ommision from me that they can point it out.
//*********************************************************
This is the select button code:
// this is the code that does not work the way i want it to
public String btnGoPhraseSelect_action() {
try {
String[] mySelections = getSessionBean1().getGoPhraseChoices();
String showSelections = "";
if (mySelections != null) {
for (int iPhrase = 0; iPhrase < mySelections.length; iPhrase ++)
showSelections = showSelections + mySelections[iPhrase] +"\n";}
if (showSelections.equals("")){
showSelections = "nothing selected"; }
else{
showSelections = showSelections + "\n" ;
getTextArea1().setValue(showSelections);}
getTextArea1().setValue(showSelections);
setValue("#{sessionScope.goPhrase}", showSelections);}
catch (Exception e)
{
log("Exception occurred!!", e);
}
return null;
}
//********************************************************
This is the add button code
// this works perfectly
public String btnGoPhraseAdd_action() {
try {
String strGoPhraseAdd = (String) txtGoPhrase.getText();
if(strGoPhraseAdd != null){
getSessionBean1().addGoPhraseOption(strGoPhraseAdd);
txtGoPhrase.setText("");
}else{
return null;
}
}
catch (Exception e) {
log("Exception occurred!!", e);
}
return null;
}
//*********************************************************
This is the remove button code
// this works perfectly
public String btnGoPhraseRemove_action() {
try {
if((String[])this.getLstbGoPhrase().getSelected() != null){
getSessionBean1().removeGoPhraseOptions((String[])this.getLstbGoPhrase().getSel ected());
}else{
return null;
}
}
catch (Exception e) {
log("Exception occurred!!", e);
}
this.textArea2.setValue("");
return null;
}
//*********************************************************
This is the session bean1 code:
This is the add to listbox code
public void addGoPhraseOption(String strGoPhraseAdd) {
String newItemVal = strGoPhraseAdd;
Option opt = new Option(newItemVal, strGoPhraseAdd);
Option[] current = this.goPhraseListOptions;
Option[] newOpts = new Option[current.length + 1];
for (int iPhrase = 0; iPhrase < current.length; iPhrase++)
newOpts[iPhrase] = current[iPhrase];
newOpts[current.length] = opt;
this.setGoPhraseListOptions(newOpts);
}
//********************************************************
This is the remove code of selections from the listbox
public void removeGoPhraseOptions(String[] selectedValues) {
Option[] current = this.goPhraseListOptions;
int curLength = current.length;
if (selectedValues != null) {
int numSelected = selectedValues.length;
int newLength = curLength - numSelected;
Option[] newOpts = new Option[newLength]; // updated list array
int k = 0; // counter for the updated list
boolean found = false;
for (int i = 0; i < current.length; i++) {
found = false;
for (int j = 0; j < numSelected; j++) {
if (current.getValue().equals(selectedValues[j])) {
found = true;
break;
}
}
if (!found) {
newOpts[k] = current;
k++;
}
}
//*********************************************************
I think it might be in the constructor of session bean1 as I am only calling the following:
goPhraseListOptions = new com.sun.rave.web.ui.model.Option[] {};
Should I be using for example:
goPhraseListOptions = new com.sun.rave.web.ui.model.OptionGroup[] {};
or
goPhraseListOptions = new com.sun.rave.web.ui.model.OptionList[] {};
session bean 1 constructor
public SessionBean1() {
goPhraseListOptions = new com.sun.rave.web.ui.model.Option[] {};
this.stopWordsListOptions = new com.sun.rave.web.ui.model.Option[] {};
this.stopWordsChoices = new String[] {};
goPhraseChoices = new String[] {};
phraseCounter = 0;
this.stopCounter = 0;
}
# 3
I am trying to bind the data to the listbox and I am so stumped. I can add and delete from the listbox and select items with in the list box and show the results in a text area but i can not through code Either
(1)Select all via coding so that the text area shows all results from the list box
(2)Select all by iterating through them.
I have tried .getItems() and I can not for the life of me get the list of items out of the option[] array.
Any Ideas?
# 4
> Thank you but I tried to iterate through the
> optionlist and I am getting an int returned.
Is your getValue returning an Integer? That is, is the first object in your Option item an Integer?
then you have to modify the code to work with an Integer instead of a string.
For example, your page bean code might look like:
public String selectAllButton_action() {
// TODO: Process the button click action. Return value is a navigation
// case name where null will return to the same page.
Integer[] selected =
new Integer[getSessionBean1().getListOptions().length];
Option[] myOptions = getSessionBean1().getListOptions();
for (int j = 0; j < myOptions.length; j++) {
selected[j] = (Integer)myOptions[j].getValue();
}
getSessionBean1().setChoices(selected);
return null;
}
Your session bean code might look like:
public SessionBean1() {
// Initialize the property values and
//add some choices to the listOptions property to get started.
listOptions = new com.sun.rave.web.ui.model.Option[] {
new Option(new Integer(1), "My Choice 1"),
new Option(new Integer(1), "My Choice 2"),
new Option(new Integer(1), "My Choice 3")};
choices = new Integer[] {};
}
/**
* Holds value of property choices.
*/
private Integer[] choices;
/**
* Getter for property choices.
* @return Value of property choices.
*/
public Integer[] getChoices() {
return this.choices;
}
/**
* Setter for property choices.
* @param choices New value of property choices.
*/
public void setChoices(Integer[] choices) {
this.choices = choices;
}
String[] selected =
new String[getSessionBean1().getListOptions().length];
Option[] myOptions = getSessionBean1().getListOptions();
for (int j = 0; j < myOptions.length; j++) {
selected[j] = (String) myOptions[j].getValue().toString();
}
# 5
In my last response there is the following code at the bottom that you must ignore. It should not be there
String[] selected =
new String[getSessionBean1().getListOptions().length];
Option[] myOptions = getSessionBean1().getListOptions();
for (int j = 0; j < myOptions.length; j++) {
selected[j] = (String) myOptions[j].getValue().toString();
}
# 6
I solved this issue about a week ago and I just wanted to let people know how... Thank you to all whom assisted me...
Here is the code example and I finally saw where my issue was... now that I know sessionbeans... The rest of my app is finally falling into place...
reiterate through the bean so as to select all and once this was done then I just went through the existing code to see what the results were...
This was not to complex of an issue but once I understood that I did not have to create any methods in the session beans and it was a simple 5 lines of code in my submit button...
anyways, here is the code to accomplish this as some people in this forum are extremely helpful and I really do appreciate the help...
Just Wait I am almost to the JNI part of this project...
String[] selectedStopWords = new String[getSessionBean1().getStopWordsListOptions().length];
Option[] myOptionsStopWords = getSessionBean1().getStopWordsListOptions();
for (int jSW = 0; jSW < myOptionsStopWords.length; jSW++) {
selectedStopWords[jSW] = (String)myOptionsStopWords[jSW].getValue();
}
getSessionBean1().setStopWordsChoices(selectedStopWords);
String[] mySelectionsSW = getSessionBean1().getStopWordsChoices();
String showSelectionsSW = "";
if (mySelectionsSW != null) {
// Create a list of the values of the selected items
for (int iStop = 0; iStop < mySelectionsSW.length; iStop++)
showSelectionsSW = showSelectionsSW + mySelectionsSW[iStop] +"\n";
}
if (showSelectionsSW.equals(""))
{
showSelectionsSW = "nothing selected";
}
else
{
showSelectionsSW = showSelectionsSW + "\n" ;
}
setValue("#{sessionScope.stopWords}", showSelectionsSW);
