Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:16:19+00:00 2026-05-26T05:16:19+00:00

I created one checkbox and one textfield for each line using wicket listview. When

  • 0

I created one checkbox and one textfield for each line using wicket listview.

When the user selects the checkbox I am able to set the related boolean value
succesfully.

But I needed to put a restriction. Only one checkbox can be
selected to submit the form in the following structure.

Textarea checkbox
Textarea checkbox
Textarea checkbox
Textarea checkbox

The text that I typed into the textareas are removed when I unset the checkbox that I selected.
As far as I know the following line is used to prevent this problem but it doesnt work. Do you have any idea?

listView.setReuseItems(true); 

 

public class CheckBoxListPage extends WebPage {      
    private Form inputForm; 

    public CheckBoxListPage() 
    { 
            add(inputForm = new InputForm("inputForm")); 
    } 

    private class InputForm extends Form 
    {  
            private List<NameWrapper> data; 

            public InputForm(String name) 
            { 
                    super(name);                    
                    data = new ArrayList<NameWrapper>(); 
                    data.add(new NameWrapper("one")); 
                    data.add(new NameWrapper("two")); 
                    data.add(new NameWrapper("three")); 
                    data.add(new NameWrapper("four")); 

                    final ListView<NameWrapper> listView = new ListView<NameWrapper>("list", data) 
                    { 
                            protected void populateItem(ListItem<NameWrapper> item) { 
                                    final NameWrapper wrapper = (NameWrapper)item.getModelObject(); 
                                    item.add(new Label("name", wrapper.getName())); 
                                    final CheckBox checkBox = new CheckBox("check", new PropertyModel<Boolean>(wrapper, "selected")); 
                                    item.add(checkBox); 
                                    checkBox.add(new OnChangeAjaxBehavior() { 
                                            @Override 
                                            protected void onUpdate(AjaxRequestTarget target) {                                                                                                   
                                                    if (wrapper.getSelected()) { 
                                                            for (NameWrapper entity : data) { 
                                                                    if (!entity.equals(wrapper)) { 
                                                                            entity.setSelected(Boolean.FALSE); 
                                                                    } 
                                                            } 
                                                    } 
                                                    target.addComponent(inputForm); 
                                            }                                              
                                    }); 
                            } 
                    }; 
                listView.setReuseItems(true); 
                add(listView); 
            }             
    } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:16:20+00:00Added an answer on May 26, 2026 at 5:16 am

    It would be helpful if you included in the code how are you building those textareas, and if you’re adding them back to the AjaxRequestTarget.

    Anyway, I see you’re trying to implement a server-side behavior to make something you could do client-side (actually, it’s a client-side operation). You’d be far better unchecking all other checkboxes client-side, for instance, with javascript.

    You can make your Component implement IHeaderContributor, and output the checkboxes’ HTML id attributes to Javascript. Then, every checkbox can have an onchange event handler in which you can uncheck all other checkboxes selecting them with document.getElementById().

    With this approach, you wouldn’t need setReuseItems(true). In case you write the ListView back to the AjaxRequestTarget, setReuseItems() would help you keep the same Components that were instantiated in the first execution of populateItem() (i.e, they would keep the same markupId’s). In general, if you need to manage state of components inside the ListView, it’s recommendable to use setReuseItems(true). In this case this is done client-side, so it’s not necessary at first glance.

    For instance:

    private class InputForm extends Form implements IHeaderContributor {
    
        private List<String> checkboxIds = new ArrayList();
        //...
            protected void populateItem(ListItem<NameWrapper> item) { 
                // PropertyModel can be used on Models, too, 
                // not necessarily modelObejcts.
                IModel checkboxModel = new PropertyModel<Boolean>(item.getModel(), "selected"));
                final CheckBox checkBox = new CheckBox("check", checkboxModel);                                 
                checkBox.setOutputMarkupId(true);
    
                // If checkboxMarkupId is null at this point, 
                // you can always set it this way
                // checkBox.setMarkupId("check" + item.getIndex());
    
                String checkboxMarkupId = checkBox.getMarkupId();
                checkboxIds.add(checkboxMarkupId);            
                item.add(checkBox); 
                String js = "deselectChecks('" + checkboxMarkupId + "');";
                checkbox.add(new SimpleAttributeModifier("onchange", js));
            }
        //...
        public void renderHead(IHeaderResponse response) {
            String jsArrDecl = "var checkIds = [";
            for (String checkId : checkboxIds){
                jsArrDecl += "'" + checkId + "', ";
            }
            jsArrDecl = jsArrDecl.substring(0, jsArrDecl.length()-1);
            jsArrDecl += "];";
            response.renderOnDomReadyJavascript(jsArrDecl);
        }
    }
    

    Javascript:

    function deselectChecks(selected){
         for each (var checkId in checkIds) {
              if (checkId != selected){
                  document.getElementById(checkId).checked = false;
              }
         }
    }
    

    You can append onchange="deselectChecks(this.id);" to the checkboxes in the HTML file, or add it with a SimpleAttributeModifier in the Java class.

    As always, you should also implement this restriction/behavior server-side to prevent users without javascript to bypass it. I’d suggest a FormValidator to that effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created listview using layoutinflator. Listview contains checkbox and textview for each row.
I have created one listview, in this each row contain check box and the
I have created one table using the below command: create table Table1( Id int
I have created one application in flex that is accessing the Java webservice using
I have created one user named tuser with create database rights in SQL server
What gives? My previously created user control is visible, but the newly created one
I created a setup file using InstallShield application. Now, in one of the Installation
I created list of todos. Now I want to put the checkbox for each
I have created a dynamic listview which contains a CheckBox.what I want is at
I created one GWT webapplication project.Inthat i want to create servlet program,but in that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.