0
A
回答
5
使用一排工廠,工具提示添加到TableRow
:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.Tooltip;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
;
public class TableViewSample extends Application {
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethan", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]")
);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
table.setRowFactory(tv -> new TableRow<Person>() {
private Tooltip tooltip = new Tooltip();
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
if (person == null) {
setTooltip(null);
} else {
tooltip.setText(person.getFirstName()+" "+person.getLastName());
setTooltip(tooltip);
}
}
});
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<>("lastName"));
TableColumn<Person, String> emailCol = new TableColumn<>("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
}
}
0
您可以創建一個動態的TableRow的實現:
import java.util.function.Function;
import javafx.scene.control.TableRow;
import javafx.scene.control.Tooltip;
public class TooltipTableRow<T> extends TableRow<T> {
private Function<T, String> toolTipStringFunction;
public TooltipTableRow(Function<T, String> toolTipStringFunction) {
this.toolTipStringFunction = toolTipStringFunction;
}
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if(item == null) {
setTooltip(null);
} else {
Tooltip tooltip = new Tooltip(toolTipStringFunction.apply(item));
setTooltip(tooltip);
}
}
}
,並設置它像這在你的控制器的initialize()
:
personTableView.setRowFactory((tableView) -> {
return new TooltipTableRow<Person>((Person person) -> {
return person.getFirstName()+" "+person.getLastName();
});
});
相關問題
- 1. JavaFX:工具提示 - 無法顯示工具提示
- 2. qTip在工具提示中的位置
- 3. 在自定義Google地圖上顯示位置工具提示
- 4. Tipsy在相同的位置顯示各種工具提示
- 5. 如何在多系列高圖中顯示固定的工具提示位置?
- 6. JavaFX TableView不顯示
- 7. OS X:顯示菜單項的工具提示的位置
- 8. Javascript工具提示定位
- 9. JqueryUI工具提示定位
- 10. CSS工具提示定位
- 11. WPF工具提示定位
- 12. 僅顯示jQuery的特定css類的工具提示
- 13. 使用ChoiceBox在TableView中顯示特定數據JavaFX
- 14. ng2-bootstrap工具提示內容在特定位置的換行符
- 15. 自定義工具提示無法顯示在正確的位置
- 16. 顯示gMAP中的特定位置
- 17. jQuery的工具提示負位置
- 18. ChartJS:更改工具提示的位置
- 19. 控制Winforms工具提示的位置
- 20. IE位置問題的工具提示
- 21. 顯示DataGridView中的rowHeader工具提示
- 22. 如何在Flex中顯示mx Datagrid行中的工具提示?
- 23. 在QTableWidget中顯示工具提示
- 24. 如何在引導滑塊中的鼠標位置顯示工具提示值
- 25. jQuery的工具提示定位問題
- 26. Bootstrap工具提示錯誤的定位
- 27. Bootstrap - 如何定位特定的工具提示?
- 28. 在javafx tableview中添加工具提示並改變表格的顏色
- 29. 工具提示位置設置
- 30. Winform工具提示位置設置