JavaFX - ListView Selection and Making Text Appear - java

This is my first post and I'm still a beginner in Java. The purpose of this program is to create a menu with food items in it and be able to select the items using ListView (which is the variable menu in this case). When I select the food items in the ListView, they are supposed to appear on the right side of the screen showing the text of what is selected (i.e. if Hamburger and Hotdog are selected from ListView, Hamburger and Hotdog appear as text on the right side of the screen). I got some pictures to include too, using ImageView and those images appear just fine without a problem. The problem is when I select from ListView, nothing appears because I'm not sure how to make it appear... I've tried get the index and then setting the text like that, getting the items, etc... I'm just really lost at the moment. I'm just not sure why it's not working because in my head the plan works fine, I get the index and then set that index to the ListView, getting the strings, but it's just not working. What am I doing wrong, or how should I be thinking about solving this?
Edit:
When I try to add the text from this line:
vBox.getChildren().add(foodItemsList[i]);
foodItemsList[i] has the error of
ArrayType expected, instead found
javafx.collections.ObservableList
This is my code:
menu.getSelectionModel().selectedItemProperty().addListener( e -> {
flowPane.getChildren().clear();
for (Integer i : menu.getSelectionModel().getSelectedIndices()) {
flowPane.getChildren().add(foodPics[i]);
}
});
// ### This just displays the current value and the value I last selected, if I selected 3+ values,
// the last 2 are the only ones that show.
menu.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
item1.setText(newValue);
item2.setText(oldValue);
}
});
menu.getSelectionModel().selectedItemProperty().addListener( e -> {
flowPane.getChildren().clear();
for (String i : menu.getSelectionModel().getSelectedItems()) {
vBox.getChildren().add(foodItemsList[i]);
}
});

Related

getText() always return empty string from dynamically created Chip component?

I'm trying to dynamically create some choice chip components based on an ArrayList of String from some computation and following are the code to create the chips and adding them to a ChipGroup created in layout XML file.
if (mChipGroup.getChildCount() == 0 ){
int i = 0;
for (Classifier.Recognition res: results){
Chip resultChip = new Chip(getDialog().getContext());
ChipDrawable chipDrawable =
ChipDrawable.createFromAttributes(
getActivity(),
null,
0,
R.style.Widget_MaterialComponents_Chip_Choice);
resultChip.setId(i++);
resultChip.setChipDrawable(chipDrawable);
resultChip.setText(res.getTitle());
mChipGroup.addView(resultChip);
}
}
The Chips displayed correctly with the text but when I tried to call getText() on the chips, it always return empty String but not the text contained by the chips. I tested this by setting the OnCheckedChangeListener on the ChipGroup and making a Toast with the text (though it didn;'t work). When I tried to display only the checkedId it works.
mChipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
Chip chip = group.findViewById(checkedId);
if(chip != null){
Toast.makeText(getContext(), chip.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
});
My current workaround is to have a variable holding the array results and use ArrayList.get(selectedChipId.getTitle()). but don't think it should be that way though
I also found that it is able to get text from Chips added in layout file but not run-time added Chips. Tried with both 1.1.0/alpha06 and 1.1.0/alpha07 release but am having no luck. Would like to have some advice if possible. Thank you very much.
So, it seems like a bug as per answered in here and here. Current workaround is to use ((ChipDrawable) chip.getChipDrawable()).getText() instead.

Why I can't update my database when saving data from 10 radioButtons?

I have 10 toggleGroup Buttons with 4 radio buttons each. When you click one radio button within a toggle group, It will add the index value of the selected radio button to an Arraylist. THen I have a "save" button to save those values to a database.
public ArrayList<Integer> RightInt(){
for(ToggleGroup tg: toggleRDB()) {
tg.selectedToggleProperty().addListener((observable, oldValue, newValue) ->{
if(newValue != null) {
//adds the index of the selected RadioButton to selectedRDBIndex list
selectedRDBIndex.add(tg.getToggles().indexOf(newValue));
}
});
}
return selectedRDBIndex;
}
//selectedRDBIndex is an ArrayList<Integer> storage.
The code above returns a list of Integer with 10 values.
This code is the action performed by the save button.
for(int i = 0; i < 10; i++) {
//answerStore.storeRDB connects to my dataBase to save the values returned by the RightInt() function.
answerStore.storeRDB(i+1, TB, RightInt().get(i));
}
RightInt().clear();
The code above works well. It saves and updates values to my database.
Then I decided to use those values again here:
for(int i = 0; i<10 ; i++) {
toggleRDB().get(i).getToggles().get(answerStoration.retrieveDataRDBSet(i+1,TB)).setSelected(true);
}
//toggleRDB() returns a list of toggleGroups.
//answerStoration.retrieveDataRDBSet gets the radioButton integer data in the database that i used to setSelect an index of radioButton in each toggle group.
I used all these codes so that when the user selected a radioButton that progress will be saved. But after using the code above I can't update datas in my database. I hope you understood my situation and solve this problem. THanks in advance.
I got some hints on my problem. When I click a radio button
It adds another place on the arraylist that's why the values aren't changing. Now I need figure out where to place a set () method or how to replace those values. I would like to say that clearing the list is not an option.

Setting tab cursor position in TableView JavaFX

I have a javafx tableview and I'm trying to write a keylistener so the user can quickly select rows and edit their contents.
So far I can focus the table with a key press and select/focus a row with a digit key press. However I then want the user to be able to tab through the row to select the cell he wants to edit.
My problem is whenever I press the tab when a row is selected, it always starts selecting cells from the first row.
Here is the code from my key listener:
....
else if(code.isDigitKey()){
TableView tv = scene.getSelectedTable();
if(tv != null){
tv.getSelectionModel().select(code.ordinal()-24);
tv.getFocusModel().focus(code.ordinal()-24);
//Something required here to set tab position to start of this row
}else{
System.out.println("null");
}
}
Is there a way to set a tab starting position?
SOLUTION:
Tab simply goes to the next JavaFX control element that it knows about. The TableView had focus so when I pressed tab it went to the next control element which just so happened to be inside the table. Tab does not traverse through table cells.
So the work around I came to was to call the edit method on the first cell in the selected row:
....
else if(code.isDigitKey()){
TableView tv = scene.getSelectedTable();
if(tv != null){
tv.getSelectionModel().select(code.ordinal()-24);
tv.getFocusModel().focus(code.ordinal()-24);
tv.edit(code.ordinal()-24, ((TableColumn)(tv.getColumns().get(0))));
}else{
System.out.println("null");
}
}
Then in my CellFactory for that column (It happens to be a Spinner), I override startEdit:
#Override
public void startEdit(){
this.spinner.requestFocus();
}
Note the column and table must be editable.

Deselect selected row in Vaadin Grid

Can somebody give me a hint because I am stucked. And I haven't found the appropriate solution for my problem.
I have a Grid, with 1-3 rows. I click on the row -> the row is selected.
I want to have this row to be deselected after I click somewhere else (outside this row) but inside the grid.
Here is simple screenshot, to help you visialize it better.
What kind of listener should I use for this case? I have tried ItemClickListener -> haven't helped.
Try putting your grid in a separate layout and add LayoutClickListener to it:
gridLayout.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {
#Override
public void layoutClick(LayoutEvents.LayoutClickEvent event) {
if(grid.getSelectedRow() != null) {
grid.deselectAll();
}
}
});
gridLayout.asSingleSelect().addSelectionListener(e->{
if(e.getFirstSelectedItem().isPresent()) {
system.out.println("selected");
}else {
// when deselected make some actions here
system.out.println("deSelected");
}
});
Selection:
If you click on the same row of data for the first time , it will print selected ,since there are values present on what you clicked .
Deselection:
Now try to click on the same selected row for the second time , now it will go to deselection mode. since there are no values when you deselected.Now no need to click anywhere on the grid ,you can click on the same row of data for two times for the selection and deselection.

Changing image in adjacent ImageButtons (Android)

So I'm trying to make a little game for Android. It's about turning lights on and off. I have 25 ImageButtons and, when I press one, I need the ones that are adjacent (up, left, down, right) to change too.
The thing is that I can't find a way to get/identify this buttons.
I tried using "view.getX()" and "view.getY()" but don't know how to implement them the right way (if there is one).
Here's the code I use to change ONE button:
public void change(View view)
{
String state = view.getTag().toString();
if (state.equals("on"))
{
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOff));
view.setTag("off");
lightsOn -= 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
if (state.equals("off")) {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOn));
view.setTag("on");
lightsOn += 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
}
I use the tags to know if it's On or Off and set a txtView with the number of ON lights.
But, to the point, how do I get the ones adjacent to this one?
Thanks in advance.
You have to store the buttons on a bidimensional array.
When you click on one button, you set the tag and the background on the buttons you are interested on.
Pseudocode:
If (button_matrix[row][column] is clicked) then
Set_tag_background (button_matrix[row-1][column])
Set_tag_background (button_matrix[row+1][column])
Set_tag_background (button_matrix[row][column-1])
Set_tag_background (button_matrix[row][column+1])
Set_tag_background (button_matrix[row][column])
...
On the implementation you should take care about not generating ArrayOutOfBoundsException...

Categories