jsf - How to Set the background color for h:commandButton in backing bean -
i have command button, when click that, background colour of button should changed. should done in backing bean method. how set colour command button in java method? tried 1
if (dt.equals(getdate())) { system.out.println("date equal...."); button.setbackground(color.yellow); } else { system.out.println("date different"); }
but shows error
cannot find method set background(java.awt.colour).
you're making conceptual mistakes.
- you're confusing jsf swing.
- you're trying manipulate view in controller.
to learn jsf is, start here. learn swing is, start here. not same. stop thinking in swing or searching swing solutions when developing jsf.
as mvc aspect, backing bean controller. should manipulate model (bean's properties), not view (xhtml file). view (xhtml file) should access model (bean's properties) via controller (the managed bean instance).
below right way:
private boolean dateequal; public void someactionmethod() { dateequal = dt.equals(date); } public boolean isdateequal() { return dateequal; }
<h:commandbutton ... style="background: #{bean.dateequal ? 'yellow' : 'none'}" />
alternatively, can away without additional property if have getter methods both dt
, date
properties:
<h:commandbutton ... style="background: #{bean.dt eq bean.date ? 'yellow' : 'none'}" />
note using inline css via style
attribute poor practice in html perspective. best create css class representing specific condition. e.g. "highlight" (or whatever specific term particular conditon has).
.highlight { background: yellow; }
<h:outputstylesheet name="style.css" /> ... <h:commandbutton ... styleclass="#{bean.dateequal ? 'highlight' : ''}" />
Comments
Post a Comment