JavaFX - Check if a checkbox is ticked - checkbox

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
}
}
});

Related

ANGULAR2: Detecting Change in static variable of a class

I am writing a dragDirective. Eements are dragged in dragZones. On mouse releases I do a hitTest against all availabe dragZones. I am maintaining a static boolean flag which ends up being false if all hittests return false. In such a situation I would like to resposition the element in the dragZone it originally belonged to. How do I check against this change in variable value?
this._messageBus.listen("dragStart", (obj, event) => {
DragZoneDirective.HITTEST = false;
});
this._messageBus.listen("dragStop", (obj, event) => {
if (this.hitTest(event.x, event.y))
{
//clone object
let clone: Object = JSON.parse(JSON.stringify(obj));
this.dragZoneElems.push(clone);
DragZoneDirective.HITTEST = true;
}
let index = this.dragZoneElems.indexOf(obj);
if (index > -1)
this.dragZoneElems.splice(index, 1);
});
You can't use Angular binding that is checked by Angular change detection on static fields.
You could add a getter on a component that forwards to that static field, then a binding to that getter would be checked by Angulars change detection.
IMHO the preferred way is to use an Observable that emits an event on change. Interested code can subscribe and get notified about updates.

Vaadin - How to convert a Grid column which has boolean value to Checkbox

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.

JavaFX: Capture "Enter" key pressed

I need to keep an indeterminably sized list of strings. I figured the best way to do this would be through a combo box which would take user input, and in turn add that user input upon an "Enter" keystroke detected to the ComboBox list of items, and also allow the user to remove those items by a "Delete" keystroke.
I had hoped this would be a very simple task handled like so:
this.cbx.setOnKeyTyped((KeyEvent E) -> {
switch(E.getCode()){
case ENTER:
this.cbx.getItems().add(this.cbx.valueProperty().get());
this.cbx.valueProperty().set("");
E.consume();
break;
case DELETE:
if (this.cbx.getItems().contains(
this.cbx.valueProperty().get()
)) this.cbx.getItems().remove(this.cbx.valueProperty().get());
this.cbx.valueProperty().set("");
E.consume();
break;
}
});
Unfortunately, Enter does not trigger the event. So clearly I am mistaken.
I also tried with onKeyPressed, and that did not work either.
What need I do to capture when "Enter" and "Delete" are pressed (It picks up "Shift" just fine which is maddening).
EDIT 1:
Have also tried with
If(E.getCode().Equals(KeyCode.ENTER)){
...
} else if (E.getCode().equals(KeyCode.DELETE)){
...
}
No Love.
Edit 2:
Based on James_D's answer below which put me on the right course, in order to accomplish what I was seeking to do, I employed the following method:
ComboBox<String> cb = new ComboBox<>();
cb.setEditable(true);
cb.getEditor().addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent E) -> {
switch(E.getCode()){
case ENTER:{
if (cb.getItems().contains(cb.getEditor().getText()))
E.consume();
else{
cb.getItems().add(cb.getEditor().getText());
cb.getEditor().clear();
E.consume();
}
break;
}
case DELETE:{
if (E.isControlDown() && cb.getItems().contains(cb.getEditor().getText()))
cb.getItems().remove(cb.getEditor().getText());
else if (E.isAltDown()) cb.getItems().clear();
if (E.isControlDown() || E.isAltDown()){
cb.getEditor().clear();
E.consume();
}
break;
}
}
});
Are you looking to have an editable combo box, that adds items to its popup list when the user types in items that are not there?
If so, try:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class UpdatingComboBox extends Application {
#Override
public void start(Stage primaryStage) {
ComboBox<String> combo = new ComboBox<>();
combo.setEditable(true);
combo.valueProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null && ! combo.getItems().contains(newValue)) {
combo.getItems().add(newValue);
}
});
HBox root = new HBox(combo);
root.setAlignment(Pos.TOP_CENTER);
primaryStage.setScene(new Scene(root, 350, 150));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
For deleting, the first thing I would ask is if you really want the functionality as you've described it. Users would typically expect pressing delete in an editable combo box to delete the next character, not remove an item entirely from the list. If you do want to do this, you have to get your hands a little more dirty and use a key listener. For some reason, adding the key listener to the combo box directly seems to have somewhat unpredictable results; it works however if you add it to the text field underlying the editable combo box:
combo.getEditor().addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.DELETE) {
combo.getItems().remove(combo.getValue());
event.consume();
}
});

Smartgwt: How to get checkbox value on a listgrid?

I've a listener on cellClick, I get the selected Record but I can't find a way to understand if this record is checked
Method ListGrid.isSelected(ListGridRecord) returns true if row is selected, not if is checked
My Code:
listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
listGrid.addCellClickHandler(new CellClickHandler() {
#Override
public void onCellClick(CellClickEvent event) {
if(event.getColNum() == 0 && idMenu != null){
boolean isChecked = event.getRecord().???;
if(isChecked)
....
else
....
}
I've tried also with event.getRecord().getAttributeAsBoolean("_checkField") with no success...
I found a simply solution...
My task is solved using a special boolean field in the DataSource named, for example, "checked"
In ListGrid I've a field "checked", and with a RecordClickHandler I can manage check or uncheck event.
DataSource code:
DataSourceBooleanField checkField = new DataSourceBooleanField("checked");
ListGrid code:
listGrid.addRecordClickHandler(new RecordClickHandler() {
#Override
public void onRecordClick(RecordClickEvent event) {
Record rec = event.getRecord();
boolean checked = rec.getAttributeAsBoolean("checked");
if(checked){
...
}else{
...
}
rec.setAttribute("checked", !checked);
catPgrid.saveAllEdits();
catPgrid.refreshFields();
}
});
ListGridField checkField = new ListGridField("checked", "Sel");
Maybe getSelectedRecords() method would help you!
Here is an API reference: http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/widgets/grid/ListGrid.html#getSelectedRecords()
Definitely this will provide all records which are selected (using checkbox) but there should be some values which you could use for identifying each record uniquely!

how to get checkbox of actionbar work (added by setCustomView )?

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

Resources