I have a MultiSelectComboBox but when trying to select some items through its select() method nothing happens.
My code looks as follows:
Binder<Technology> binder = new BeanValidationBinder<>(Technology.class);
MultiSelectComboBox<TechnologyLabel> multiComboBox = new MultiSelectComboBox<>("Labels");
binder
.forField(this.multiComboBox)
.bind(Technology::getLabels, Technology::setLabels);
List<TechnologyLabel> labelList = technologyLayout.technologyLabelService.getTechnologyLabels(technology);
List<Label> containedLabels = new ArrayList<>();
for (var tl : labelList) {
containedLabels.add(tl.getLabel());
}
List<Label> labels = labelService.findAllLabels();
for (var label : labels) {
if (!containedLabels.contains(label)) {
labelList.add(new TechnologyLabel(technology, label));
}
}
multiComboBox.setItems(labelList);
multiComboBox.setItemLabelGenerator(TechnologyLabel::getLabelName);
note.setMaxHeight("10em");
multiComboBox.select(labelList.get(0);
What I do above is querying my ManyToMany relation to find all labels given the technology. Then I find all the labels and create a list from that. Lastly, I try to select first item of the list, but it does not show a tick next to it.
Related
Okey so my problem is next: I use web scraping to take some data from web page IMDB in this case, that data is titles of movies, and I already tried to print it in console and that works fine. My problem is that I can not save that titles in my table columns, I put all needed codes for this problem, and I cant find where I made mistake and why titles wont show in table columns. At the and, user need to pick one title and that title need to be stored in text field. Have someone any idea, please?
I have table:
TableColumn izborAuta = new TableColumn("Izbor auta");
TableColumn lokacijaPreuzimanja = new TableColumn("Lokacija
preuzimanja");
TableColumn lokacijaVracanja = new TableColumn("Lokacija Vracanja");
TableColumn cena = new TableColumn("Cena");
I have this code to setup columns:
izborAuta.setCellValueFactory(new PropertyValueFactory<Vozila, String>
("izborAuta"));
lokacijaPreuzimanja.setCellValueFactory(new
PropertyValueFactory<Vozila, String>("lokacijaPreuzimanja"));
lokacijaVracanja.setCellValueFactory(new
PropertyValueFactory<Vozila, String>("lokacijaVracanja"));
cena.setCellValueFactory(new PropertyValueFactory<Vozila, String>
("cena"));
tableView.setItems(Baza.baza.prikazBaze());
tableView.getColumns().addAll(izborAuta, lokacijaPreuzimanja,
lokacijaVracanja, cena);
I have this code, so when I pick one item from table that item need to be stored in textField:
tableView.setOnMouseClicked((e) -> {
Vozila v = (Vozila)
tableView.getSelectionModel().getSelectedItem();
txIzborAuta.setText(v.getIzborAuta());
txLokacijaPreuzimanja.setText(v.getLokacijaPreuzimanja());
txLokacijaVracanja.setText(v.getLokacijaVracanja());
txCena.setText(v.getCena());
});
And at the end I use web scraping to save items in table:
Document doc = Jsoup.connect("https://www.imdb.com/chart/top?
ref_=nv_mv_250").get();
Elements elems = doc.select("table.chart.full-width");
for (Element e : elems) {
String izborAuta = e.select(".titleColumn").text();
String lokacijaPreuzimanja = e.select(".titleColumn").text();
String lokacijaVracanja = e.select(".titleColumn").text();
String cena = e.select(".titleColumn").text();
Vozila v = new Vozila();
v.setIzborAuta(izborAuta);
v.setLokacijaPreuzimanja(lokacijaPreuzimanja);
v.setLokacijaVracanja(lokacijaVracanja);
v.setCena(cena + " " + "RSD");
Baza.insertVozila(v);
}
I'm working on a project where the TableView looks like this:
The datas are inside are a MySQL database, and I use a modell and an observableArrayList to get them.
I want to make an initialize method which read the whole selected Line and save it into the TextFields (like Name-Name, Address - Address etc). I tried with this, and now I can Select the Whole Line data, but can't split it into Strings to set the TextField texts:
ListViewerModell details = (ListViewerModell) table.getSelectionModel().getSelectedItem();
//Checking if any row selected
if (details != null) {
//??? - split
editName.setText("*Name*");
editAddress.setText("*Address*");
}
Tried with the modell getName(), getAdress() methods but I got NullPointerException and TablePos methods.
// create table
TableView<Person> table = new TableView<Person>();
// create columns
TableColumn<Person, String> nameCol = new TableColumn<Person, String>("Name");
nameCol.setCellValueFactory(new PropertyValueFactory("name"));
TableColumn<Person, Address> addressCol = new TableColumn<Person, String>("Address");
addressCol.setCellValueFactory(new PropertyValueFactory("address"));
// add columns
table.getColumns().setAll(nameCol, addressCol);
// get data from db, return object List<Person> from DB
ObservableList<Person> persons = getPersonsFromDB();
table.setItems(persons);
tableView.setOnMouseClicked((MouseEvent event) -> {
if (event.getClickCount() > 1) {
onEdit();
}
});
public void onEdit() {
// check the table's selected item and get selected item
if (table.getSelectionModel().getSelectedItem() != null) {
Person selectedPerson = table.getSelectionModel().getSelectedItem();
nameTextField.setText(selectedPerson.getName());
addressTextField.setText(selectedPerson.getAddress());
}
}
One way could be extracting the value of the columns of the selected row, like so :
Object selectedItems = table.getSelectionModel().getSelectedItems().get(0);
String first_Column = selectedItems.toString().split(",")[0].substring(1);
System.out.println(first_Column);
I need to make a programm in JavaFX where I need to manage a movie library. I have a tableview that is filled by an observable list. Then I made bindings between the Properties and the Textboxes or Labels next to the tableview. Now the problem is, I have also pictures that are related to the movies, like cinema posters. and at the moment I just use a hardcoded one:
imgPoster = new Image(getClass().getResourceAsStream("../resources/images/posters/" + 5 + ".jpg"));
In the datafile there is one column with the ID of the movie and the same number is also the picture for it. So I need to replace that "5" in the sample code with a binding or so that it actively changes the picture as soon as I click on a row in the tableview.
How do I do that?
edit:
After the first comment i did that:
imgOscars = new Image(getClass().getResourceAsStream("../resources/images/oscar/Oscar-logo.png"));
oscars = new ImageView(imgOscars);
oscars.setPreserveRatio(true);
oscars.setFitHeight(45);
but question stays
Either add a listener to the selected item property in the table (I am just guessing at the model you are using for the table):
TableView<Movie> table = ... ;
table.getSelectionModel().selectedItemProperty().addListener((obs, oldSelectedMovie, newSelectedMovie) -> {
if (newSelectedMovie == null) {
oscars.setImage(null);
} else {
// get image from new selection
Image image = new Image(getClass().getResourceAsStream("../resources/images/oscar/"+newSelectedMoviegetId()+".png"));
oscars.setImage(image);
}
});
or use a binding:
oscars.imageProperty().bind(Bindings.createObjectBinding(() -> {
if (table.getSelectionModel().getSelectedItem() == null) {
return null ;
} else {
Movie selected = table.getSelectionModel().getSelectedItem();
return new Image(getClass().getResourceAsStream("../resources/images/oscar/"+selected.getId()+".png")); ;
},
table.getSelectionModel().selectedItemProperty());
I'm trying to implement a palette. I try to set a default selected list but it's empty.
myLists:
// here I get a Set of the categorys which are already in that group
Set<Category> selectedCategorysSet = new HashSet<Category>();
selectedCategorysSet = group.getCategorys();
// here I get all categorys exists
List<Category> listCategory = new ArrayList<Category>();
listCategory = catDao.getAll(Category.class);
List<Category> selectedCats = new ArrayList<Category>();
List<Category> tmpList = new ArrayList<Category>();
// the palette doesnt accept an Set so I added the set to a List
selectedCats.addAll(selectedCategorysSet);
// here I delete every Category from the whole List which is already selected (stored in a temporary list)
for(Category catList:listCategory){
for(Category cat:selectedCategorysSet){
if(cat.getCategoryId() == catList.getCategoryId()){
tmpList.add(catList);
}
}
}
listCategory.removeAll(tmpList);
/*
two multiple select boxes which switches items between each other
*/
IChoiceRenderer<Category> renderer = new ChoiceRenderer<Category>("title","categoryId");
final Palette<Category> palette = new Palette<Category>("palette",
new ListModel<Category>(selectedCats),
new CollectionModel<Category>(listCategory),
renderer, 10, false);
I already debugged that code, it works but my selected values are empty.
Here is a picture of my debugged variables:
but the selected field is still empty!
what am I doing wrong?
You should not delete every Category from the whole List which is already selected.
Palette component must store whole list of values in it's choicesModel which is listCategory in your code.
So, just remove following code from your implementation:
for(Category catList:listCategory){
for(Category cat:selectedCategorysSet){
if(cat.getCategoryId() == catList.getCategoryId()){
tmpList.add(catList);
}
}
}
listCategory.removeAll(tmpList);
I want to know how to highlight any specific road. For example i want to color a road yellow that has id=1. I am using Java to display the map.
I have found how to highlight the road which has ID=1. Steps that I followed are:
//create a filter object
Filter filter;
//create a datastore object from .shp file
FileDataStore store= FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource=store.getFeatureSource();
//I am using CQL query to select the road that is ID=1
filter=CQL.toFilter("ID=1");
//create a SimpleFeatureCollection object for the filtered features
SimpleFeatureCollection fc=featureSource.getFeatures(filter);
//create a feature iterator to traverse through the selected features
SimpleFeatureIterator iter=fc.features();
//create a Set object to store the featureIdentifiers.
Set<FeatureId> IDs=new HashSet<FeatureId>();
//add the selected features to IDs
try{
while(iter.hasNext()){
SimpleFeature f=iter.next();
IDs.add(f.getIdentifier());
System.out.println(" "+f.getIdentifier());
}
}
finally{
iter.close();
}
//create style object to store style of selected features
Style style=createSelectedStyle(IDs);
MapContext map=new DefaultMapContext();
//show the map
map.addLayer(featureSource,style);
JMapFrame.showMap(map);
//defining the createSelectedStyle method
private Style createSelectedStyle(Set<FeatureId> IDs) {
Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
selectedRule.setFilter(ff.id(IDs));
Rule otherRule = createRule(LINE_COLOUR, FILL_COLOUR);
otherRule.setElseFilter(true);
FeatureTypeStyle fts = sf.createFeatureTypeStyle();
fts.rules().add(selectedRule);
fts.rules().add(otherRule);
Style style2 = sf.createStyle();
style2.featureTypeStyles().add(fts);
return style2;
}
//defining the createRule method
private Rule createRule(Color outlineColor, Color fillColor) {
Symbolizer symbolizer = null;
Fill fill = null;//not required if working with line
Stroke stroke = sf.createStroke(ff.literal(outlineColor), ff.literal(LINE_WIDTH));
symbolizer = sf.createLineSymbolizer(stroke, "the_geom");
Rule rule = sf.createRule();
rule.symbolizers().add(symbolizer);
return rule;
}