I am using ControlsFX for CheckTreeView. I have lots of elements in CheckTreeView and i dont want to traverse through all the elements in this tree ( because it takes lots of time due to number of elements in the tree). Is there a method like checkTreeView.getLastUncheckedItem(); to get the last unchecked one.
Currently I am checking the number of elements that checked and comparing it with counter.
If (CountPrev > Count){
//Something Unchecked Do Stuff
}
But again, i cant find what is unchecked without traverse through all elements.
EDIT:
When user checks an item on CheckTreeView, I get that item by
String lastCheckedItem = checkTreeView.getCheckModel().
getCheckedItems().get(treeView.getCheckModel().getCheckedItems().size()-1).toString();
Now I need something like this for the unchecked item
Take a ArrayList 'allItem' and Store all TreeItems, then
after Store selected item in ObservableList 'Selecteditems' using
getCheckedItems() method, Now remove selected item in ArrayList like
below code:
Here allTreeItems is a CheckBoxTreeItem
List<String> allItem = new ArrayList<>();
for (int j = 0; j < allTreeItems.getValue().length(); j++) {
allItem.add(allTreeItems.getChildren().get(j).getValue());
}
if (CountPrev > Count) {
ObservableList<TreeItem<String>> Selecteditems = checkTreeView.getCheckModel().getCheckedItems();
allItem.remove(Selecteditems);
System.out.println("UnChecked Item :" + allItem);
for (int k = 0; k < allItem.size(); k++) {
System.out.println(allItem.get(k));
}
}
Guys thank you so much for your help!
I've accepted Calips' answer because of time and effort he gave for my question.
This is what I've been looking for:
checkTreeView.getCheckModel().getCheckedItems().addListener(new ListChangeListener<TreeItem<String>>() {
#Override public void onChanged(ListChangeListener.Change<? extends TreeItem<String>> change) {
updateText(checkedItemsLabel, change.getList());
while (change.next()) {
System.out.println("============================================");
System.out.println("Change: " + change);
System.out.println("Added sublist " + change.getAddedSubList());
System.out.println("Removed sublist " + change.getRemoved());
System.out.println("List " + change.getList());
System.out.println("Added " + change.wasAdded() + " Permutated " + change.wasPermutated() + " Removed " + change.wasRemoved() + " Replaced "
+ change.wasReplaced() + " Updated " + change.wasUpdated());
System.out.println("============================================");
}
}
});
Resource:
https://github.com/jinghai/controlsfx/blob/master/controlsfx-samples/src/main/java/org/controlsfx/samples/checked/HelloCheckTreeView.java
Stack<YourClassOfTheCheckModel> recentlyUnchcked = new Stack<YourClassOfTheCheckModel>();
yourTreeView.getSelectionModel().
selectedItemProperty().addListener( new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue,
Object newValue) {
TreeItem<YourClassOfTheCheckModel> selectedItem =
(TreeItem<YourClassOfTheCheckModel>) newValue;
CheckModel checkModel = checkTreeView.getCheckModel().
bool checked = checkModel.isChecked (selectedItem);
if(checked==false){
recentlyUnchcked.push(yourObjectOfTheCheckModel);
}
}
});
Hope this will help or give you an idea though i don't know if this will work (code not tested, have no IDE right now).
Related
public void String printRentalList(){
for(int v = 0; v < numOfRoomsHR; v++){
System.out.println(theRooms[v].toString);
}
}
public void addReservation(Room a){
if (numOfRoomsHR < theRooms.legnth){
theRooms[numOfRoomsHR] = a;
numofRoomsHR++;
} else {
System.out.println("Can not add anymore rooms to the hotel");
}
}
public void String findReservation(int l){
boolean flag = false;
for(int i = 0; i < numOfRoomsHR && !flag; i++){
if(theRooms[i] == l){
flag = true;
System.out.println("Reservation found for room number:" + l + "\n" + theRooms[i].toString);
} else {
System.out.println("Reservation not found for room number:" + l);
}
}
}
I was working through my project and compile checking to see if I did anything wrong through out the project I got this error:
error: '(' expected
For the line printRentalList() and the PrintList() yet the ( is there. any suggestions ?
It is probably the .toString on this line:
System.out.println("Reservation found for room number:" + l + "\n" + theRooms[i].toString);
Java would expect the ( after toString, as toString() is a method. I also noticed, as rgettman said: you have multiple return types with void and String. You do need to pick one and it seems, at least for the second function it should be void since you do not appear to be returning anything.
I have an observable which I want to emit Integer values, but if one of the emitted values is negative, I want the observable to emit values again with 1 second delay between emissions until there's no negative values.
I found this answer Repeat/Resubscribe to Observable with condition based on emitted item(s) and I don't really like it because it uses recursion which may slow a device down (depending on how many attempts it'd take to get the proper results).
Is there any other (proper) way of doing that? I also don't know how to set a delay to each emission except for the first one.
Here's what I have now:
getObservable().subscribe(valueList -> {
for (Integer value : valueList) {
Log.d("TAG", "value: " + value);
}
});
private Observable<List<Integer>> getObservable() {
return Observable.fromCallable(() -> {
List<Integer> list = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
list.add(random.nextInt(12) - 1);
}
return list;
}).flatMap(valueList -> {
for (int i = 0; i < valueList.size(); i++) {
if (valueList.get(i) < 0) {
Log.d("TAG", "Negative value = " + valueList.get(i) + ", index = " + i);
return getObservable().delay(1, TimeUnit.SECONDS);;
}
}
return Observable.just(valueList);
});
}
The assignment asks for entry of 10 patient records, to include patientId, patientFirstName, patientLastName, patientIllness, and notes; this is to be put into a TreeSet with a Comparator that will abc by last name.
This is the section of code that I am struggling with:
public void patientRecord() {
int i = 0;
int patientRecords = 10;
Set<Patient> patientHashSet;
System.out.println("This program will create ten patient records.");
System.out.println();
do {
getPatientId();
getPatientFirstName();
getPatientLastName();
getPatientIllness();
getNotes();
patientHashSet = new TreeSet<>(new PatientComparator());
patientHashSet.add(new Patient(patientId, patientFirstName, patientLastName, patientIllness, notes));
i++;
} while (i < patientRecords);
for (Patient record : patientHashSet) {
System.out.println(record.patientId + " " + record.patientLastName + ", " + record.patientFirstName + " "
+ record.patientIllness + " " + record.notes);
System.out.println("##########################################################################");
}
}
And this is the Comparator code:
import java.util.Comparator;
public class PatientComparator implements Comparator<Patient> {
#Override
public int compare(Patient o1, Patient o2) {
return o1.patientLastName.compareTo(o2.patientLastName);
}
}
I'm not really sure what it is that I'm doing wrong. I have also tried placing the "add" into an array, but that produces the same results--only the last patient's information prints out, along with the line of "####".
Put this line above the loop
patientHashSet = new TreeSet<>(new PatientComparator());
In your code, it is written inside the loop, so it is creating a new set on every iteration.
Check the correction.
I need some help. I have an ArrayList of Textfields.
static List<TextField> lfLetters = new ArrayList<>();
And I want to check if the value has changed. And if it was I want to know which textfield it was. I know I can do this with a Listener but that only worked for a single one.
TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
I want it to work on an Array and determine which textfield has changed.
Thanks in advance!
You can use ObservableList with appropriate extractor and add listener directly to list. This way it will be automatically watching changes in the specified properties of its elements. It is more convenient, than adding listener to each text field, but in this case you can't get old value:
ObservableList<TextField> oList =
FXCollections.observableArrayList(tf -> new Observable[]{tf.textProperty()});
oList.addListener((ListChangeListener.Change<? extends TextField> c) -> {
while (c.next()) {
if (c.wasUpdated()) {
for (int i = c.getFrom(); i < c.getTo(); ++i) {
System.out.println("Updated index: " + i + ", new value: " + c.getList().get(i).getText());
}
}
}
});
I was thinking that I will mark this quesiton as a duplicate, as you have a totally similar question here.
But in the end, you want to have the reference to the TextField also in the listener, so I will add an answer.
This snippet adds 10 TextField objects to the ArrayList and adds a listener to each.
for (int i = 0; i < 10; i++) {
TextField tf = new TextField();
final int index = i;
tf.textProperty().addListener((obs, oldVal, newVal) -> {
System.out.println("Text of Textfield on index " + index + " changed from " + oldVal
+ " to " + newVal);
});
lfLetters.add(tf);
}
Or if your ArrayList is already initialized, you can iterate through it simply:
lfLetters.forEach(tf -> {
tf.textProperty().addListener((obs, oldVal, newVal) -> {
System.out.println("Text of Textfield on index " + lfLetters.indexOf(tf) + " changed from " + oldVal
+ " to " + newVal);
});
});
Sample output
Text of Textfield on index 2 changed from InitialText - 2 to ModifiedText - 2
Text of Textfield on index 6 changed from InitialText - 6 to ModifiedText - 6
I am having problems on my insert method. When I go to add a number that needs to be swapped, I get an index out of bounds exception. here: Collections.swap(table, table.get(parent), table.get(child)); This is how I am adding to the heap. tHeap.insert(14); Thanks for any help.
public class Heap {
private ArrayList<Integer> table;
public Heap() {
table = new ArrayList<Integer>();
}
public void insert(Integer toInsert) {
table.add(toInsert);
int child = table.size() - 1;
int parent = (child - 1) / 2;
//TextIO.putln("1 " + parent + " " + toInsert + " " + child);
while (parent >= 0 && table.get(parent) > table.get(child)) {
TextIO.putln("Swapping: " + parent + " Parent for Child: " + child);
Collections.swap(table, table.get(parent), table.get(child));
}
}
public void printTable() {
for (int i = 0; i < table.size(); i++) {
TextIO.putln("Index: " + i + " Data: " + table.get(i));
}
}
}
I think you mean Collections.swap(table, parent, child);? ArrayList.get will return the element at an index (Java ArrayList API) Collections.swap swaps the elements at an index (Java Collections API). You want to be passing in the indices, not the values at the indices. Also I think in your while loop you may want to be updating child and parent.