Home > Software design >  How to render outputText when inputText is focused or changes value?
How to render outputText when inputText is focused or changes value?

Time:04-13

I am learning Java EE and I a ran into an issue I can't figure out. I have this xhtml:

<h:form>
            <fieldset>
                <legend>Random Number (with execute="someId")</legend>
                Range:
                <h:inputText value="#{numberGenerator.range}"
                             id="rangeField">
                    <f:ajax event="change" execute="@this"
                            render="numField3"/>
                    <f:ajax event="focus" execute="@this"
                            render="numField3"/>
                </h:inputText><br/>
                <h:commandButton value="Show Number"
                                 action="#{numberGenerator.randomize}">
                </h:commandButton><br/>
                <h2><h:outputText value="#{numberGenerator.number}"
                                  id="numField3"/></h2>
            </fieldset>
        </h:form>

and here is the bean:

package javaeetutorial.ajaxguessnumber;
import javax.faces.bean.*;
@ManagedBean

    public class NumberGenerator  {
        private double number = Math.random();
        private double range = 1.0;

        public double getRange() {
            return(range);
        }

        public void setRange(double range) {
            this.range = range;
        }

        public double getNumber() {
            return(number);
        }

        public double getNumber2() {
            return(Math.random() * range);
        }

        public String randomize() {
            number = range * Math.random();
            return(null);
        }
    }

My question is how can I manage to make the value rendered in outputText whenever the inputText is focused or changes value? So far I only managed to update it when the inputText is focused, but on value change nothing happens.

CodePudding user response:

Try keyup instead of change. The list of supported events are: blur, change, click, action, dblclick, focus, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, mouseup, and select.

This will likely fire a very large amount of ajax events to your server though, so be prepared for that. You'll get an event for every keystroke.

source: https://www.beyondjava.net/a-comprehensive-guide-to-jsf-ajax

  • Related