How to change text alignment of a combo box items in javafx? - java

I have a Combo Box in my app . I added some items as string to it . Items have left alignment . I want the right alignment . I searched a lot but i cant find anything.
Thanks.

I figured out the following solution, although I'm not sure if it's a good one:
For button cell:
box.setButtonCell(new ListCell<String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
setAlignment(Pos.CENTER_RIGHT);
Insets old = getPadding();
setPadding(new Insets(old.getTop(), 0, old.getBottom(), 0));
}
}
});
For popup list view:
box.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
#Override
public ListCell<String> call(ListView<String> list) {
return new ListCell<String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
setAlignment(Pos.CENTER_RIGHT);
setPadding(new Insets(3, 3, 3, 0));
}
}
};
}
});

Related

Click event on ImageView inside TableCell in JavaFX

I have a JavaFX TableView populated with many TableColumns, one of which is matImageColumn. The code for it looks like:
matImageColumn.setCellFactory(new Callback<TableColumn<CustomObject, ImageView>, TableCell<CustomObject, ImageView>>() {
#Override
public TableCell call(final TableColumn<CustomObject, ImageView> param) {
final TableCell<CustomObject, ImageView> cell = new TableCell<CustomObject, ImageView>() {
ImageView img = new ImageView();
#Override
public void updateItem(ImageView item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
CustomObject co = getTableView().getItems().get(getIndex());
img = co.getImageViewOfMat();
setGraphic(img);
setText(null);
}
}
};
return cell;
}
});
Now as it is, I am looking to include a click event on my imageview but I am clueless from where to start.Can you show me possible solutions to this issue? Thanks in advance.

Not able to get javafx Listview variable height of rows

view.setCellFactory(lst ->
new ListCell<AnchorPane>() {
int k=1;
#Override
protected void updateItem(AnchorPane item,boolean empty) {
super.updateItem(item, empty);
if (empty) {
setPrefHeight(0.0);
setGraphic(null);
} else {
if(k == 1){
setPrefHeight(100.00);
k++;
}
else{
setPrefHeight(10.00);
}
setGraphic(item);
}
}
});
Setting different height of row by overriding the update Item and passing anchor pane but not getting different height for the rows.

Delete button in TableView does not dissapear

I've got a little problem (it's really not that big of a deal but it annoys me), because after I delete a record from my table, I also want my delete button to disapear, right now it works like this:
BEFORE clicking the DELETE button:
AFTER clicking first record's DELETE button:
Also, this is the function that I'm using to handle this event:
private class ButtonCell extends TableCell<Record, Boolean> {
final Button cellButton = new Button("Delete");
ButtonCell(){
cellButton.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent t){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Delete the entire row?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
Animal currentAnimal = (Animal) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
data.remove(currentAnimal);
cellButton.setVisible(false);
}
}
});
}
//Display button if the row is not empty
#Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if(!empty){
setGraphic(cellButton);
}
}
}
Try this:
#Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if(empty){
setGraphic(null);
} else {
setGraphic(cellButton);
}
}

How to get row value inside updateItem() of CellFactory

ageColumn.setCellFactory(param -> new TableCell<Person, String>(){
#Override
protected void updateItem(String item, boolean empty) {
param.getCellFactory().
super.updateItem(item, empty);
setText(empty ? null : String.valueOf(item));
if(person.getName.equals("MATEUS")) {
setStyle("-fx-background-color: red;");
}
}
});
How to get this "Person" which is the row value from the Table? I can only get the value from the cell, but not the entire object.
You can do
Person person = getTableView().getItems().get(getIndex());
You can also do
Person person = (Person) getTableRow().getItem();
but this is less desirable (in my opinion) because getTableRow() returns a raw type, and consequently it requires the unchecked downcast.
Obviously either of these only works if empty is false, so they should be inside a check for that:
ageColumn.setCellFactory(param -> new TableCell<Person, String>(){
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setStyle("");
} else {
setText(item);
Person person = getTableView().getItems().get(getIndex());
if(person.getName.equals("MATEUS")) {
setStyle("-fx-background-color: red;");
} else {
setStyle("");
}
}
}
});

Stop multiple ListCell Context Menus from Showing

I made a Custom ListView Cell so i can add a Context Menu but it keeps opening multiple context menus when you click more than once and the old ones just raise Exceptions when used.
Here is my SongCell class
public SongCell(ListView<Song> list, Playlist playlist) {
setAlignment(Pos.CENTER_LEFT);
ContextMenu listContextMenu = new ContextMenu();
MenuItem removeItem = new MenuItem("Remove");
MenuItem editID3Item = new MenuItem("Edit ID3");
MenuItem playNextItem = new MenuItem("Play Next");
removeItem.setOnAction((ActionEvent event) -> {
list.getItems().remove(getIndex());
});
editID3Item.setOnAction((ActionEvent event) -> {
Optional<Pair<String, String>> show = new FXID3Edit(getItem()).show();
});
playNextItem.setOnAction((ActionEvent event) -> {
Song song = getItem();
list.getItems().remove(song);
playlist.addSongRequest(new SongRequest(song, SongRequest.type.NEXT));
});
listContextMenu.getItems().add(removeItem);
listContextMenu.getItems().add(playNextItem);
listContextMenu.getItems().add(editID3Item);
setOnMousePressed(event -> {
if (event.getButton().equals(MouseButton.SECONDARY)) {
if (getItem() != null) {
if (getItem().equals(playlist.getSong())) {
playNextItem.setDisable(true);
removeItem.setDisable(true);
} else {
playNextItem.setDisable(false);
removeItem.setDisable(false);
}
listContextMenu.show(list, event.getScreenX(), event.getScreenY());
}
}
if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {
playlist.setIndex(this.getIndex());
playlist.play();
}
event.consume();
});
}
#Override
protected void updateItem(Song item, boolean empty) {
super.updateItem(item, empty);
if (!empty && item != null) {
this.setText(item.toString());
} else {
this.setText("");
}
}
I also greatly appreciate help on my Coding Style
Instead of registering a mouse listener to show the context menu, use the built-in setContextMenu(...) property. I.e.:
public SongCell(ListView<Song> list, Playlist playlist) {
// ...
setOnMousePressed(event -> {
if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {
playlist.setIndex(this.getIndex());
playlist.play();
}
event.consume();
});
}
#Override
public void updateItem(Song item, boolean empty) {
super.updateItem(item, empty);
if (!empty && item != null) {
this.setText(item.toString());
this.setContextMenu(listContextMenu);
} else {
this.setText("");
this.setContextMenu(null);
}
}

Categories