NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

JSF : How to populate textbox from Dropdown selection

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
4fingers



Joined: 31 Mar 2009
Posts: 9
Location: South Africa

PostPosted: Thu Aug 27, 2009 4:19 pm    Post subject: JSF : How to populate textbox from Dropdown selection Reply with quote

Hi, I hope someone can please help me with this
I select a value from a Dropdown, then click on an Edit button to edit the values for the selected Item. I want my page to now display the selected values in various text boxes below the Dropdown. When I check the values in the backing bean (prerender), I can see the new values which I want and even set the fields to, but when the page is displayed, I get blanks in those fields.

I have read that JSF will do a page validation and if it detects no changes it will display the same values again?

How do I get around this problem - I have done a lot of searching and simply cannot find a solution ?

Any help will be greatly appreciated.
Embarassed
Back to top
4fingers



Joined: 31 Mar 2009
Posts: 9
Location: South Africa

PostPosted: Tue Sep 01, 2009 7:06 am    Post subject: Reply with quote

Hi

After bashing my head for a couple of days, I finally found the answer to my question. I will describe my fix below for the benefit of others just in case someone has a similar problem :
I left out a crucial piece of information in my original question Embarassed
I did not mention that I was making use of Virtual Forms, which was in fact causing the problem in my code.

I added the following code in my button's Change_action() event :
form1.discardSubmittedValues("ItemDelete");
form1.discardSubmittedValues("ItemOk");
form1.discardSubmittedValues("ItemPreview");

This solved my problem of the latest value being displayed when selected
from the list and clicking the "Update" button.

Hope this helps someone else !
Back to top
ceyezumma



Joined: 02 Feb 2009
Posts: 122

PostPosted: Tue Sep 01, 2009 12:53 pm    Post subject: forms Reply with quote

Impressive
good job.
I have been trying to work with the jsf2.0 netbeans 6.8
Is this what you are working with.

I can't get the items loaded to the dropdown list. It would be great to see your code. if possible.

I'm trying to understand the JSF
Thanks
-ceyesumma
Back to top
4fingers



Joined: 31 Mar 2009
Posts: 9
Location: South Africa

PostPosted: Tue Sep 01, 2009 8:28 pm    Post subject: Reply with quote

Hi ceyesumma
I am currently using Netbeans 6.7.1 and Faces 1.2

In my case I am populating an Option[] in my Sessionbean.
You then bind your Dropdown to this array (right-click on Dropdown and click on bind to data. Select the listItems Option array).

I populate the array by calling it from the formsBean at the point
that I need it. In my web app, I have an Items tab and when I select it,
I call getSessionBean1().loadItems();

In the SessionBean, I have the following code :
Add :
private Option[] listItems; //Use this array to populate the list
private List<ItemsDTO> myItems; //Use this array to retrieve data
private ItemsDTO[] itemsArray; //Use array to hold Item details

Generate the relavant Getters and Setters for listItems:
public Option[] getListItems() {
return listItems;
}

public void setListItems(Option[] listItems) {
this.listItems = listItems;
}


Create a load method which will populate the array
public void loadItems(){
int listcount = 0;

// Check for items in array
if (getItemsArray() != null){
listcount = itemsArray.length;
}

// Clear array
for (int i=0; i<listcount; i++){
itemsArray[i] = null;
}


List loadList = new ArrayList();

// Create a new Data Access Object - connect to Oracle db
ORACLE_DAO myDAO = new ORACLE_DAO();

//Create a new Arraylist Collection and populate from db call
myItems = new ArrayList<ItemsDTO>();
myItems = myDAO.getItems(getSelectedInitiative());

//Process records retrieved from database
if (!myItems.isEmpty()){
itemsArray = new ItemsDTO[myItems.size()];

int i = 0;
for(ItemsDTO item : myItems){
itemsArray[i] = item;
loadList.add(item.getItemName());

//Pass the index and display value to method
addOption(item.getItemId(), item.getItemName());

i++;
}
}else{
//Create empty array
itemsArray = new ItemsDTO[10];
itemsCounter = 10;
}

if (itemsArray != null){
itemsCounter = itemsArray.length;
}

}

//Method to populate Option array with new values
public void addOption(int ii, String s) {
// Add a new item to the list by creating an
// updated array that contains the new item
Option opt = new Option(ii,s);

//Create a temp array to hold existing values
Option[] current;

//Populate temp array
current = getListItems();

int size = 0;

if (current == null){
size = 1;
}else{
size = current.length + 1;
}

// Create a new Option array with the new size
Option[] newOpts = new Option[size];

if (size > 1){
// add the current items to the new array
for (int i = 0; i < size - 1; i++){
newOpts[i] = current[i];
}
}
newOpts[size-1] = opt;

// Add the new Option to the list in question
setListItems(newOpts);

}


I hope I am not confusing you with my code Wink

Cheers
Back to top
ceyezumma



Joined: 02 Feb 2009
Posts: 122

PostPosted: Tue Sep 01, 2009 9:28 pm    Post subject: dropdownlist Reply with quote

Thanks for your code.
I am now working with netbeans 6.7 again I think 6.8 should be running in November.

I worked with actionscript for the last 6months and I trying to switch gears back into java and good examples are hard to come by.
Are you aware of a good source of examples? The netbean page isn't really working for me at the moment.
Thanks again
-ceyesumma
Back to top
4fingers



Joined: 31 Mar 2009
Posts: 9
Location: South Africa

PostPosted: Wed Sep 02, 2009 5:10 am    Post subject: Reply with quote

Hi ceyezumma
I make use of many of the Netbeans tutorials - http://www.netbeans.org/kb/index.html which is a great source of information. Then "University of Google" is your best friend Very Happy

Some sites that I often look at :
www.javapassion.com - Training
http://java.sun.com/docs/books/tutorial/
http://java.dzone.com/
www.javaranch.com
www.nabble.com/
http://blogs.sun.com/
http://blogs.sun.com/divas/


There are numerous other good java sites, really depends on what you are looking for, and there Google is your best friend.

Good luck and happy hunting
Back to top
ceyezumma



Joined: 02 Feb 2009
Posts: 122

PostPosted: Wed Sep 02, 2009 5:48 am    Post subject: java links Reply with quote

hey thanks I really need somathose.
these should help me while working on my associates in googleology Razz Laughing Shocked
Back to top
jyeary



Joined: 21 Oct 2008
Posts: 605
Location: Simpsonville, SC

PostPosted: Tue Sep 08, 2009 11:57 pm    Post subject: JSF : How to populate textbox from Dropdown selection Reply with quote

I have an example on my blog on how to do this sort of binding.

http://javaevangelist.blogspot.com/2009/07/woodstock-dropdown-list-binding.html


On Thu, Aug 27, 2009 at 12:19 PM, 4fingers <address-removed ([email]address-removed[/email])> wrote:
Quote:
Hi, I hope someone can please help me with this

I select a value from a Dropdown, then click on an Edit button to edit the values for the selected Item. I want my page to now display the selected values in various text boxes below the Dropdown.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo