How to get radiobutton value? - java

Here is my code
//unlock
rbtnUTrue = new RadioButton("rbtnUnlock", "True");
rbtnUFalse = new RadioButton("rbtnUnlock", "False");
hp = new HorizontalPanel();
hp.add(rbtnUTrue);
hp.add(rbtnUFalse);
vlc.add(new FieldLabel(hp, "Has Unlock"), new VerticalLayoutData(1, -1, new Margins(10)));
if (rbtnUTrue.isChecked()) {
dummy_u = 1;
} else if (rbtnUFalse.isChecked()) {
dummy_u = 0;
}
but it always says that I clicked the false button

To answer your question: "how to get data from radio buttons in gwt?":
radioButton.getValue(); or radioButton.isChecked();
I'm not sure what do you want to achieve with the if-clause. If you need an initial value then just set it.
What you probably want to do is to register when the button is clicked:
rbtnUTrue.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
#Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
if (rbtnUTrue.isChecked()) {
dummy_u = 1;
} else if (rbtnUFalse.isChecked()) {
dummy_u = 0;
}
}
});

Related

RadioGroupButton deselect value when button is clicked in Vaadin 14

I've been working on an online exam project and currently adding some multiple choice feature. My problem is, each time I moved to the next question the value from the previous radiogroupbutton is removed/deselect. But still the assigned value of the object is present.
I tried removing/adding the component as well, still the selected value for the RadioGroupButton is missing.
public class TestQuestionaire extends Dialog {
TQCoverageService tqcs = new TQCoverageServiceImpl();
CellItemService cis = new CellItemServiceImpl();
ItemKeyService iks = new ItemKeyServiceImpl();
VerticalLayout mainLayout;
TQCoverage tqCoverage;
List<CellItem> ciList = new ArrayList();
Map<CellItem, CellItemOption> cellItemAnswerMap = new HashMap();
CellItem cellItem;
Binder<CellItem> binder;
private int tqCoverageId;
private int cellItemIndex = 0;
private int cellItemIndexSize = 0;
private int score = 0;
Button next;
Button prev;
String stem;
Paragraph stemHolder;
RadioButtonGroup<CellItemOption> cioGroup;
public TestQuestionaire(int tqCoverageId) {
this.tqCoverageId = tqCoverageId;
tqCoverage = tqcs.findTQCoverage(tqCoverageId);
cellItemIndexSize = tqCoverage.getTotalItems();
for(TQItems tqi : tqcs.findAllTQItems(tqCoverageId)){
cellItem = cis.findCellItem(tqi.getCellItemId());
List<CellItemOption> cioList = cis.findAllItemOptions(tqi.getCellItemId());
cellItem.setCellItemOptionList(cioList);
ItemKey ik = iks.findItemKey(tqi.getItemKeyId());
cellItem.setItemKey(ik);
TQAnswerKey tqak = tqcs.findTQAnswerKeyByTQItem(tqi.getTqItemId());
cellItem.setTQAnswerKey(tqak);
cellItemAnswerMap.put(cellItem, new CellItemOption());
ciList.add(cellItem);
}
stemHolder = new Paragraph();
stemHolder.setWidthFull();
stemHolder.getStyle().set("font-weight", "500");
Hr hr = new Hr();
hr.setWidthFull();
cioGroup = new RadioButtonGroup<>();
cioGroup.setRenderer(new TextRenderer<>(CellItemOption::getCellItemOption));
cioGroup.addThemeVariants(RadioGroupVariant.LUMO_VERTICAL);
cioGroup.addValueChangeListener(event -> {
if(event.getValue() == null){
return;
}
if(!event.getValue().equals(event.getOldValue())){
cellItemAnswerMap.replace(getCellItem(), event.getValue());
}
});
mainLayout = new VerticalLayout(stemHolder, hr, cioGroup);
mainLayout.setWidth("600px");
hr = new Hr();
hr.setWidthFull();
mainLayout.add(hr);
binder = new Binder();
binder.forField(cioGroup)
.bind(CellItem::getCellItemOption, CellItem::setCellItemOption);
changeCellItem();
prev = new Button(VaadinIcon.BACKWARDS.create());
prev.addClickListener(event -> {
cellItemIndex--;
if(cellItemIndex == 0){
prev.setEnabled(false);
next.setEnabled(true);
} else {
prev.setEnabled(true);
next.setEnabled(true);
}
changeCellItem();
});
prev.setEnabled(false);
next = new Button(VaadinIcon.FORWARD.create());
next.getStyle().set("margin-left", "490px");
next.addClickListener(event -> {
cellItemIndex++;
if((cellItemIndex + 1) == cellItemIndexSize){
next.setEnabled(false);
prev.setEnabled(true);
} else {
next.setEnabled(true);
prev.setEnabled(true);
}
changeCellItem();
});
//this button is only to test if the current value for radiobuttongroup is removed/deselect when clicked!!
mainLayout.add(new Button("TEST", event -> {
changeCellItem();
}));
HorizontalLayout buttons = new HorizontalLayout(prev, next);
buttons.setWidthFull();
buttons.setJustifyContentMode(FlexComponent.JustifyContentMode.START);
mainLayout.add(buttons);
add(mainLayout);
open();
}
//refresh components for new/previous set if item
private void changeCellItem(){
stemHolder.removeAll();
cellItem = ciList.get(getCellItemIndex());
stem = cellItem.getItem().replace("{key}", cellItem.getItemKey().getItemKey());
stemHolder.add(stem);
cioGroup.clear();
cioGroup.setItems(cellItem.getCellItemOptionList());
cellItem.setCellItemOption(cellItemAnswerMap.get(cellItem));
binder.readBean(cellItem);
//binder.setBean(cellItem);
}
public TQCoverage getTQCoverage() {
return tqCoverage;
}
public CellItem getCellItem() {
return cellItem;
}
public int getCellItemIndex() {
return cellItemIndex;
}
public int getCellItemIndexSize() {
return cellItemIndexSize;
}
}

Search filter in Container Codename One

I'm trying to implement a search filter in my Container which contains a set of buttons.
Here's my code:
public void listMenu() {
Dialog loading = new InfiniteProgress().showInifiniteBlocking();
loading.show();
final Form listMenu = new Form("List Menu");
listMenu.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Container list = new Container();
list.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
list.removeAll();
Button back = new Button("Back to Main Menu");
ParseQuery<ParseObject> query = ParseQuery.getQuery("mylist");
query.whereExists("Title");
List<ParseObject> results = null;
try {
Button btn = null;
results = query.find();
if(!results.isEmpty()) {
int index = 0;
int size = results.size();
for(;index < size;++index) {
list.add(btn = new Button(results.get(index).getString("Title")));
addListener(btn);
}
}
} catch (com.parse4cn1.ParseException e) {
Dialog.show("Err", "Server is not responding.", "OK", null);
}
listMenu.add(list);
listMenu.add(back);
listMenu.show();
loading.dispose();
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
new StateMachine("/theme");
}
});
}
This code basically is querying data from the Database then setting its results into buttons then added to a Container. My question is how to implement a search filter to my Container? I've seen FilterProxyListModel<T> but not sure if ListModel<T> is compatible with Container. I'd appreciate to see an example of search filter implementation in my code.
FilterProxyListModel is for List which we don't recommend anymore. There is a full sample of searching a container here. It uses MultiButton but using Button would work just as well:
hi.getToolbar().addSearchCommand(e -> {
String text = (String)e.getSource();
if(text == null || text.length() == 0) {
// clear search
for(Component cmp : hi.getContentPane()) {
cmp.setHidden(false);
cmp.setVisible(true);
}
hi.getContentPane().animateLayout(150);
} else {
text = text.toLowerCase();
for(Component cmp : hi.getContentPane()) {
Button mb = (Button)cmp;
String line1 = mb.getText();
boolean show = line1 != null && line1.toLowerCase().indexOf(text) > -1;
mb.setHidden(!show);
mb.setVisible(show);
}
hi.getContentPane().animateLayout(150);
}
}, 4);

Check all CheckBoxTreeCells in a TreeView JavaFX

I have created a TreeView that contains many CheckBoxTreeCells. I also have a Button that I would like to check all of the CheckBoxTreeCells. I've been reading this tutorial, and I am a bit lost. Here is what I have so far:
Button Code:
public void fooMethod() {
/*selectAll is an instance variable*/
selectAll = new Button("Select All");
selectAll.setOnMouseClicked(e -> handleSelectAllButtonAction(e));
}
private void handlSelectAllButtonAction(MouseEvent e) {
/*Code goes here*/
}
TreeView Code:
public void fooMethod2() {
/*myTreeView is also an insance variable*/
myTreeView = new TreeView<String>();
CheckBoxTreeItem<String> root = new CheckBoxTreeItem<>();
CheckBoxTreeItem<String> branch1 = new CheckBoxTreeItem<>("Branch 1");
CheckBoxTreeItem<String> branch2 = new CheckBoxTreeItem<>("Branch 2");
CheckBoxTreeItem<String> branch3 = new CheckBoxTreeItem<>("Branch 3");
root.getChildren.add(branch1);
root.getChildren.add(branch2);
root.getChildren.add(branch3);
myTreeView.setCellFactory(CheckBoxTreeCell.forTreeView());
myTreeView.setRoot(root);
myTreeView.setShowRoot(false);
myTreeView.setEditable(true);
}
The example provided in the link is a bit more complex than what I need, and I think it is confusing me. How do I edit a CheckBoxTreeItems in a TreeView?
Unless there are independent CheckBoxTreeItems, its enough to select the root:
root.setSelected(true);
since this automatically selects the children.
Best way I could find was to do this:
private void handlSelectAllButtonAction(MouseEvent e) {
CheckBoxTreeItem<String> root = (CheckBoxTreeItem<String>)myTreeView.getRoot();
int numBranches = root.getChildren().size();
for(int i = 0; i < numBranches; i++) {
if(((CheckBoxTreeItem<String>)root.getChildren().get(i)).isSelected()) {
((CheckBoxTreeItem<String>)root.getChildren().get(i)).setSelected(false);
}
}
}

Tree View JavaFX Memory out of Space

I have created javaFX tree with custom Objects (SystemNode).
Tree Items has graphics: check-box and image icon which I have set through updateItems() method.
Whenever I expand or collapse Item in tree ,twice or thrice I get JAVA HEAP MEMORY OUT OF SPACE and whole UI hangs UP.
PS: updateItems() method is invoked every time I expand or collapse tree node
I have tried adding event handlers but they didn't work.
Can anyone give some solutions.
Here is how I set cellFactory :
treeView_technicalAreas.setCellFactory(Util.getTreeCellFactory());
Here is code for cell factory:
public static Callback<TreeView<SystemNode>, TreeCell<SystemNode>> getTreeCellFactory() {
Callback<TreeView<SystemNode>, TreeCell<SystemNode>> callback = new Callback<TreeView<SystemNode>, TreeCell<SystemNode>>() {
#Override
public TreeCell<SystemNode> call(TreeView<SystemNode> p) {
TreeCell<SystemNode> cell = new TreeCell<SystemNode>() {
#Override
protected void updateItem(SystemNode t, boolean isEmpty) {
super.updateItem(t, isEmpty); //To change body of generated methods, choose Tools | Templates.
if (!isEmpty) {
System.out.println("util call back : " + t.getSystem().getName());
setText(t.getSystem().getName());
HBox hBox = new HBox();
CheckBox checkBox = new CheckBox();
checkBox.setSelected(t.getSelected());
checkBox.selectedProperty().bindBidirectional(t.getSelectedProperty());
hBox.setSpacing(SPACING_BETWEEN_ICON_AND_CHECKBOX);
ImageView imageView_icon = null;
if (t.getSystem().getType() == TYPE.BAREA) {
imageView_icon = new ImageView(Constant.Image_AREAS);
} else if (t.getSystem().getType() == TYPE.AREA) {
imageView_icon = new ImageView(Constant.Image_AREAS);
} else if (t.getSystem().getType() == TYPE.DOCUMENT) {
imageView_icon = new ImageView(Constant.Image_DOCUMENTS);
} else if (t.getSystem().getType() == TYPE.NOUN_NAME) {
imageView_icon = new ImageView(Constant.Image_NOUN_NAME);
} else if (t.getSystem().getType() == TYPE.CHANGE) {
imageView_icon = new ImageView(Constant.Image_DCC);
} else if (t.getSystem().getType() == TYPE.TASK) {
imageView_icon = new ImageView(Constant.Image_TASK);
}
hBox.getChildren().addAll(checkBox, imageView_icon);
setGraphic(hBox);
}
}
};
return cell;
}
};
return callback;
}

Making a button write to one JTextField in an Array

I'm working on a TextTwist java implementation and I'm currently working on the GUI. I was wondering if some of you Swing geniuses could help me out. I'm trying to do a GUI where whenever a button is clicked, the text of that button is written into the first empty textField below it. I'm having trouble thinking through how to do it. What I've tried so far makes the first button click fill all the TextFields.
Any help or pointers in the right direction would be great.
private void makeButtonLayout() {
this.charArray = new char[6];
ArrayList<Character> charArrayList = new ArrayList<Character>();
this.charArray = this.randomString.toCharArray();
for(char tempCharacter : this.charArray){
charArrayList.add(tempCharacter);
}
for (int i = 0; i< 6; i++){
JButton letterButton = new JButton();
Character buttonCharacter = charArrayList.get(i);
charArrayList.remove(i);
String letterString = buttonCharacter.toString();
letterButton.setText(letterString);
this.letterButtonsArray.add(letterButton);
}
for (final JButton currentButton : this.letterButtonsArray){
this.buttonPanel.add(currentButton);
currentButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
int i = 0;
currentButton.setVisible(false);
JTextField temporaryTextField = new JTextField();
String temporaryString = currentButton.getText();
temporaryTextField.setText(temporaryString);
if(textFieldArray.get(i).getText().isEmpty()){
textFieldArray.get(i).setText(temporaryString);
return;
}else{
i++;
return;
}
}
});
}
}
I think you method is really strange. You mixed initialization code and execution code. Do it like this:
for (final JButton currentButton : this.letterButtonsArray){
this.buttonPanel.add(currentButton);
currentButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
currentButton.setVisible(false);
String temporaryString = currentButton.getText();
for(int i = 0; i < textFieldArray.size(); i++)
JTextField elem = textFieldArray.get(i);
if(elem.getText().equals("")){ // or if you don't want spaces do: elem.getText().trim().equals("");
elem.setText(temporaryString);
break;
}
}
}
});
}

Categories