Im trying to use a checkbox in actionbar,im using actionbarsherlock.
i've tried very hard to get the checkbox work,now I've made the UI (bu using the setCustomView method),but I'm stucked at catch the check event of the checkbox,I did some research of some similar questions but get the answer that "checkbox cant be used in actionbar ,it can only be used in submemus or etc", I doubt that and wondered whether there is a way to get it work...
here is my UI:
here is my CustomView xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="center_vertical" >
<CheckBox
android:id="#+id/action_anoni_check"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:checked="false"
android:gravity="center"
android:text="#string/anonymity" />
</LinearLayout>
here is how i added it in my ui:
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.write_actionbar_top);
I know this is an old question, but I thought I'd chime in because I couldn't get Craig's solution to work correctly. The "intended" approach to do this is:
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.write_actionbar_top);
CheckBox mCheckbox = (CheckBox) getActionBar().getCustomView().findViewById(R.id.action_anoni_check);
mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//DO YOUR on CHECK ACTION IN HERE
}
}
});
You may want to create a class instance variable and inflate your write_actionbar_top view so you have reference to it, then add a onCheckListener to your checkbox:
private CheckBox mCheckbox;
...
...
mCheckbox = getLayoutInflater().inflate(R.layout.write_actionbar_top, null,false);
mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
//DO YOUR on CHECK ACTION IN HERE
}
}
}
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(mCheckbox);
This code hasn't been tested but you should get the idea.
There are also some more ways to get the click or check action on your CheckBox view here -> Android: checkbox listener
Related
I have simple code:
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
When I first time click button i have items "11" and "22" present in combobox and value "22" is selected.
Second click makes value cleared but items "11" and "22" are still present.
In case I select "11" or leave "22" selected in combobox and click button - value clears.
It seems that setValue() only works when combobox is empty but following code do not helps as well:
combo.setValue(null);
combo.clear();
combo.setItems("11","22");
combo.setValue(null);
combo.clear();
combo.setValue("22");
Following code sets value of ComboBox correctly, no matter if I select some value or clear it before click:
ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems("11","22");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setValue("22");
}
});
But I have to set items of Combobox dynamically and the last solution do not suitable for me.
Vaadin version is 10.0.9.
Has anyone some suggestions or advices ?
PS.
Thanks !
I've tried following code:
combo.setItems(Collections.emptyList());
combo.setItems("11","22");
combo.setValue("22");
But it do not work as well.
This code works only if value in combo is empty, but if I input something in combo the code just clears value by .setItems() and further .setValue() do not work.
If value of combo is empty the code works well.
Your code works perfectly fine in a minimum project based on https://vaadin.com/start/latest/project-base (which uses Vaadin 12.0.7)
#Route("")
#PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {
public MainView() {
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
add(combo,button);
}
}
Whatever value you set in the ComboBox via UI .. when the button is clicked, the selected value will switch to 22.
If that's an option for you, you could update to a newer Vaadin version and try it with that.
To better show what I meant in my comment, I'm writing it as an answer.
What I meant was to set an empty collection not in the clickListener but directly after initializing the ComboBox:
ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems(Collections.emptyList());
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
please try it out and let me know if that works
i am New to ADF, i want display/enable the input text box when checkbox is checked and i should disable when it is unchecked below is the check box ADF code,
ADF Code:
<af:selectBooleanCheckbox label="Apply WITSML Filter" id="sbc11"
autoSubmit="true" contentStyle="margin-left:10px;" valueChangeListener="#{pageFlowScope.welljobs_bean.applyWITSMLFilterIndicator}"/>
Bean:
private transient RichSelectBooleanCheckbox applyWITSMLFilterIndicator;
public void setapplyWITSMLFilterIndicator(RichSelectBooleanCheckbox applyWITSMLFilterIndicator) {
this.applyWITSMLFilterIndicator= applyWITSMLFilterIndicator;
}
public RichSelectBooleanCheckbox getapplyWITSMLFilterIndicator() {
return applyWITSMLFilterIndicator;
}
The input text i want to show:
<af:inputText id="it140" autoComplete="off"
binding="#{pageFlowScope.welljobs_bean.applyWITSMLFilterIndicator.curvesFilter}"
dimensionsFrom="content" editable="inherit" rendered="true"/>
Bean:
private transient RichInputText curvesFilter;
public void setCurvesFilter(RichInputText curvesFilter) {
this.curvesFilter = curvesFilter;
}
public RichInputText getCurvesFilter() {
return curvesFilter;
}
Can anybody please help?
it is also giving me javax.faces.FacesException: javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'curvesFilter'. Exception
You can do this with EL Expression, partial trigger/autosubmit and ValueChangeEvent.
You want to save the boolean value of the checkedbox inside your bean so you can render or disable the inputText when this value change inside your valueChangeEventListener.
You then want to refresh the inputText so it will display it's new render/disable value by adding the following property to the inputText parent :
partialTriggers="sbc11"
partialTriggers refresh the whole content of a container when an action occur on the element id you give him.
Assuming you want to disable/enable the inputText :
Bean :
public boolean checkboxIsChecked = false; //or private with getter and setter
public void checkBoxValueChange(ValueChangeEvent ve){
this.checkboxIsChecked = ve.getNewValue();
}
Jsf :
<af:selectBooleanCheckbox label="Apply WITSML Filter" id="sbc11"
autoSubmit="true" contentStyle="margin-left:10px;" valueChangeListener="#
{pageFlowScope.welljobs_bean.checkBoxValueChange}"/>
...
<af:inputText id="it140" autoComplete="off"
binding="#{pageFlowScope.welljobs_bean.applyWITSMLFilterIndicator.curvesFilter}"
dimensionsFrom="content" disabled="#{pageFlowScope.welljobs_bean.checkboxIsChecked}"/>
don't forget to add the partialTriggers="CHECKBOXID" to the inputText parent container
For official example see documentation : https://docs.oracle.com/cd/E16764_01/web.1111/b31973/af_lifecycle.htm#CIAHCFJF
I have list of check boxes inside rich:dataTable and I want to check all the boxes at once with a single check box from header column.
<rich:column id="includeInWHMapping" >
<f:facet name="header">
<h:selectBooleanCheckbox value="#{checkallbox.selectAll}">
<f:ajax actionListener="#{checkallbox.selectAllBox}" render="selectedForWHProcess" />
</h:selectBooleanCheckbox>
</f:facet>
<h:selectBooleanCheckbox id="selectedForWHProcess" value="#{checkallbox.checked[data]}">
<f:ajax actionListener="#{checkallbox.selectAllRows}"/>
</h:selectBooleanCheckbox></rich:column>
Code in checkallbox Bean:
private Map<StandardStructure, Boolean> checked = new HashMap<StandardStructure, Boolean>();
private boolean selectAll;
public boolean isSelectAll() {
return selectAll;
}
public void setSelectAll(boolean selectAll) {
this.selectAll = selectAll;
}
public Map<StandardStructure, Boolean> getChecked() {
return checked;
}
public void setChecked(Map<StandardStructure, Boolean> checked) {
this.checked = checked;
}
public void selectAllBox(ValueChangeEvent e){
boolean newSelectAll = (Boolean) e.getNewValue();
Iterator<StandardStructure> keys = checked.keySet().iterator();
while(keys.hasNext()){
StandardStructure ss = keys.next();
checked.put(ss, newSelectAll);
}
}
When I check the h:selectBooleanCheckBox of header column nothing happens. What am I missing here? Should I have to implement Map for "selectAll" property too?
Thanks.
First of all. f:ajax doesn't have actionListener, it has a listener. Read the docs here. Second thing, you can use valueChangeListener on h:selectBooleanCheckbox and only there. Third, listener inside rows boxes is wrong. Basically, it looks like you need to read this topic.
Now, here is the working example:
<h:form>
<rich:dataTable value="#{checkallbox.allValues}" var="data" id="dataTable">
<rich:column>
<f:facet name="header">
<h:selectBooleanCheckbox value="#{checkallbox.selectAll}"
valueChangeListener="#{checkallbox.selectAllBox}">
<f:ajax render="dataTable" />
</h:selectBooleanCheckbox>
</f:facet>
<h:selectBooleanCheckbox value="#{checkallbox.checked[data]}">
<f:ajax />
</h:selectBooleanCheckbox>
</rich:column>
<!-- other columns -->
</rich:dataTable>
</h:form>
Other possible problems with your code (since you've shared just a part).
The data table needs to be in form, since you're executing ajax inside.
Your keys in map are objects. You have to make sure that equals method is good. In 95% of case the default is not, especially if they are #Entity.
You have to make sure that the map is populated with false at the beginning. I use #PostConstruct:
#PostConstruct
protected void performPostConstructAction() {
for (StandardStructure s : getAllValues()) {
checked.put(s, false);
}
}
Thanks Emil! I solved it.
public void selectAllBox(){
if(!selectAll){
for(StandardStructure item : ssTable.getDto()){
checked.put(item, true);
}
}else{
for(StandardStructure item : ssTable.getDto()){
checked.put(item, false);
}
}
}
I am using Vaadin 7.6.4 for my UI work. This has the following:-
I have a window which contains a grid with data in it. This window is actually a kind of a pop up[ which shows up when my main screen gets a click on the settings icon( not shown here). This is working fine( getting the UI screen to open the Vaadin window when the settings icon the main screen is clicked).
The problem is in showing the data as mentioned below.
This grid will have 4 columns for which the data comes from a bean container.
The first column is a boolean field with true/false getting displayed based on the data from the bean container.
I need to convert this boolean field column into a checkbox with true showing the field as a checkbox with a value selected. If the value is false, then show a checkbox which is not selected.
I am trying to do that as shown in the code below but I keep getting this checkbox name printed. I dont see the checkbox but the word "checkbox" printed in there?
This checkbox should be editable. The idea is that the user should be able to select some checkboxes and the ones selected should be shown in a panel ( not shown here). Thus, the checkbox has to be editable.
How do I fix this? The code for the window is shown below
package com.platform28.mybatis;
import java.util.List;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.GeneratedPropertyContainer;
import com.vaadin.data.util.PropertyValueGenerator;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
#SuppressWarnings("serial")
public class ConfigPopUp extends Window {
VaadinUtils vaadinUtils = null;
public ConfigPopUp(List<TableColumnData> tblDataLst) {
vaadinUtils = new VaadinUtils();
// Some basic content for the window
VerticalLayout configLayout = new VerticalLayout();
configLayout.addComponent(new Label("Settings"));
configLayout.setMargin(true);
//configLayout.setWidth(null);;
setContent(configLayout);
//adding grid.
List<SettingsColumnData> settingsList = vaadinUtils.processSettingsList(tblDataLst);
final BeanItemContainer<SettingsColumnData> gridDataSource = new BeanItemContainer<SettingsColumnData>(SettingsColumnData.class, settingsList);
//change boolean value to checkbox.
GeneratedPropertyContainer gp = new GeneratedPropertyContainer(gridDataSource);
gp.addGeneratedProperty("columnDisplayed", new PropertyValueGenerator<CheckBox>() {
#Override
public CheckBox getValue(Item item, Object itemId, Object propertyId) {
boolean currentCheckBoxValue = (boolean) item.getItemProperty("columnDisplayed").getValue();
CheckBox chkBox = new CheckBox();
chkBox.setValue(currentCheckBoxValue);
return chkBox;
}
#Override
public Class<CheckBox> getType() {
return CheckBox.class;
}
});
Grid settingsGrid = new Grid(gp);
settingsGrid.setWidth("100%");
settingsGrid.setSizeFull();
settingsGrid.setColumnOrder("columnDisplayed", "columnName","columnShortName","columnDescription");
configLayout.addComponent(settingsGrid);
//configLayout.setExpandRatio(settingsGrid, 1);
// Disable the close button
setClosable(false);
HorizontalLayout hLayout = new HorizontalLayout();
hLayout.setSpacing(true);
hLayout.setMargin(true);
// Trivial logic for closing the sub-window
Button ok = new Button("Ok");
ok.addClickListener(new ClickListener() {
public void buttonClick(ClickEvent event) {
close(); // Close the sub-window
}
});
hLayout.addComponent(ok);
// Trivial logic for closing the sub-window
Button cancelBtn = new Button("Cancel");
cancelBtn.addClickListener(new ClickListener() {
public void buttonClick(ClickEvent event) {
close(); // Close the sub-window
}
});
hLayout.addComponent(cancelBtn);
configLayout.addComponent(hLayout);
// set pop up to center.
center();
// should be resizable
setResizable(true);
// should not be draggable
setDraggable(false);
//set it as modal window
setModal(true);
setWidth("50%");
setHeight("75%");
}
}
Ok, we used the SelectionMode.MULTI to show the selection of rows in there.
https://cdn.vaadin.com/vaadin-core-elements/latest/vaadin-grid/demo/selection.html
Still, I would love to learn more as to how we get the change done as shown in the question above.
Still looking for an answer to that.
Use a Renderer and a Converter, you don't need to use SelectionMode.MULTI.
An example of this is posted here.
I am trying to write some code to determine whether or not my checkbox is ticked, I am aware I can write something like to change its state to checked
checkbox.setSelected(true);
But I want to write something along the lines of
if(checkbox.setSelected(true)){
write login-username to config file
} else {
clear the config file
}
How would I go about doing this? I've been trauling through Oracle documentation but have yet to find anything useful
thanks.
you could use .isSelected() to find out if the checkbox is Ticked.
if(checkbox.isSelected()){
write login-username to config file
} else {
clear the config file
}
Have you tried registering a listener to the "selected" property of the checkbox?
It would look something like this:
yourCheckbox.selectedProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
// TODO Auto-generated method stub
if(newValue){
// your checkbox has been ticked.
// write login-username to config file
}else{
// your checkbox has been unticked. do stuff...
// clear the config file
}
}
});