How to add textArea based on selection from a dropdown or radio button in java?

穿精又带淫゛_ 提交于 2019-12-11 18:58:48

问题


I modified the feedback form of dspace code to add a category that will ask the user for their 'user type'.

    Select category = form.addItem().addSelect("category");
    category.setLabel("Please select your category");
    category.addOption("UG","Undergraduate/BS");
    category.addOption("MS","MS Student");
    category.addOption("PHD","PhD Student");
    category.addOption("FAC","Faculty");
    category.addOption("RES","Researcher");
    category.addOption("TRA","Trainee");
    category.addOption("BUS","Businessman/Private");
    category.addOption("FF","Fish farmer");
    category.addOption("OT","Other");

    String itemSelected = parameters.getParameter("category","");
    if (StringUtils.equals(itemSelected,"OT"))
    {
        TextArea other = form.addItem().addTextArea("other");
        other.setHelp("Write here if you selected Other");
        other.setValue(parameters.getParameter("other",""));
    }

My goal here is to display a text area only if the user selected Other. Also, the text area should be a required field if the user choose other. How can I achieve this? I would also like to try using radio buttons instead of select.

[EDIT] For those who are not familiar with DSpace, here's the original code that I modified from DSpace github: feedback form


回答1:


You can use radio buttons the same way you used the select:

Radio category = form.addItem().addRadio("category");
category.setLabel("Please select your category");
category.addOption("UG", "Undergraduate/BS");
...

Like terrywb said, the java code renders the page server side. You could make a 2 step process where the first step only submits the category. But including a hidden TextArea by default and then showing it using javascript is more user friendly.

Item item = form.addItem("item-name","hidden");
TextArea other = item.addTextArea("other");

Add a reference to the javascript file in the PageMeta:

public void addPageMeta(PageMeta pageMeta) throws SAXException,
            WingException, UIException, SQLException, IOException,
            AuthorizeException
    {       
        pageMeta.addMetadata("javascript", "static", null, true).addContent("static/js/feedbackform.js");
        // leave the rest
        ...
    }

Put the javascript in /dspace/modules/xmlui/src/main/webapp/static/js/feedbackform.js so it can be served.

Also parameters.getParameter("category",""); won't work out of the box. The parameters instance variables contains parameter given to it by cocoon in /aspects/ArtifactBrowser/sitemap.xmap:

<map:match pattern="feedback">
        <map:act type="SendFeedbackAction">
                <map:transform type="FeedbackForm">
                        <map:parameter name="comments" value="{comments}"/>
                        <map:parameter name="email" value="{email}"/>
                        <map:parameter name="page" value="{page}"/>
                </map:transform>

                <map:serialize type="xml"/>
        </map:act>
        <map:transform type="FeedbackSent"/>
        <map:serialize type="xml"/>
</map:match>

The SendFeedbackAction gets the data from the request:

Request request = ObjectModelHelper.getRequest(objectModel);
String page = request.getParameter("page");

And provides the value for {page} by adding it to the map that it returns:

    // Check all data is there
    if ((address == null) || address.equals("")
        || (comments == null) || comments.equals(""))
    {
            // Either the user did not fill out the form or this is the
            // first time they are visiting the page.
            Map<String,String> map = new HashMap<String,String>();
            map.put("page",page);
            ...
            return map;

The same happens for comment, email and all other fields you would like to see. Enforcing a required field should be done in there as well.




回答2:


If I were you I would use a JTextArea instead of TextArea...

public void valueChanged(ListSelectionEvent e) {
    if ( e.getSource() == item) { /*Item is whatever the selected choice you want is*/ ) {
        other.setVisible(true);
    } else {
        other.setVisible(false);
    }
}

Be sure your class extends ActionListener! And that JTextArea other is public! ;)



来源:https://stackoverflow.com/questions/25954776/how-to-add-textarea-based-on-selection-from-a-dropdown-or-radio-button-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!