Here scenario is, there are three columns in tableview Task,Group Number and Status.Task number will have string, Group number will also have String but here in status column i want to disply colour filled circles below is my code.Its not giving any error and also not showing circles.
part of xml controller class:
#FXML
private TableView<TaskSchData> taskListTable = new TableView<TaskSchData>();
#FXML
private TableColumn<TaskSchData, String> taskName;
#FXML
private TableColumn<TaskSchData, String> grpNo;
#FXML
private TableColumn<TaskSchData, String> statuscheck;
taskName.setCellValueFactory(new PropertyValueFactory<TaskSchData, String>("taskName"));
grpNo.setCellValueFactory(new PropertyValueFactory<TaskSchData, String>("grpNo"));
statuscheck.setCellValueFactory(new PropertyValueFactory<TaskSchData, String>("statuscheck"));
TaskSchData taskSchData = new TaskSchData(data.gettaskName(), data.getsuffix(),circle);
TaskSchData class:
public final SimpleStringProperty taskName;
public final SimpleStringProperty grpNo;
public Circle status ;
Circle circle = new Circle(150, Color.web("red", 0.05));
public TaskSchData(String taskName, String groupNo,Circle cir) {
this.taskName = new SimpleStringProperty(taskName);
this.grpNo = new SimpleStringProperty(groupNo);
this.status=cir;
}
public Circle getStatus() {
return status;
}
public String getTaskName() {
return taskName.get();
}
public void setTaskName(String taskName) {
this.taskName.set(taskName);
}
public void setStatus(Circle img) {
this.status=img;
}
public SimpleStringProperty taskNameProperty() {
return taskName;
}
public SimpleStringProperty grpNoProperty() {
return grpNo;
}
fxml:
<TableColumn sortable="false" text="Tasks" fx:id="taskName">
<minWidth>
<Long fx:value="50" />
</minWidth>
<prefWidth>
<Long fx:value="150" />
</prefWidth>
</TableColumn>
<TableColumn sortable="false" text="Group No" fx:id="grpNo">
<prefWidth>
<Long fx:value="75" />
</prefWidth>
</TableColumn>
<TableColumn text="Status" fx:id="statuscheck">
<prefWidth>
<Long fx:value="100" />
</prefWidth>
</TableColumn>
Kindly help here.
Related
I have created a custom Master-Detail pane for my project, where i use a split pane, in each i have two Anchor Panes. In one there is a TableView filled with Users (ObservableList). On each row (User) i have implemented a ChangeListener
table.getSelectionModel().selectedItemProperty().addListener(listElementChangeListener());
when the row is selected, i pass the UserObject for my DetailPane, and visualize User data in TextFields as detail. I have implemented controls, to understand if the User is under modification in Detail, and if so i would like to prevent a row change in my TableView. I tried to remove the ChangeListener from the TableView when i modify the User, but it dosent work well. I'm thinking of a solution like setting the focus and holding it on the row until i cancel or save the User modified.
Is there any nice solutions?
Thanks for your help.
I would probably approach this a little differently. I would bind the controls in the "detail view" bidirectionally to the properties in the User object. That way they will be updated in the object (and the table) as the user edits them. If you like, you can also provide a "cancel" button to revert to the previous values.
Here's a complete solution that uses this approach:
User.java:
package usermasterdetail;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class User {
private final StringProperty firstName = new SimpleStringProperty();
private final StringProperty lastName = new SimpleStringProperty();
private final BooleanProperty admin = new SimpleBooleanProperty();
public User(String firstName, String lastName, boolean admin) {
setFirstName(firstName);
setLastName(lastName);
setAdmin(admin);
}
public final StringProperty firstNameProperty() {
return this.firstName;
}
public final String getFirstName() {
return this.firstNameProperty().get();
}
public final void setFirstName(final String firstName) {
this.firstNameProperty().set(firstName);
}
public final StringProperty lastNameProperty() {
return this.lastName;
}
public final String getLastName() {
return this.lastNameProperty().get();
}
public final void setLastName(final String lastName) {
this.lastNameProperty().set(lastName);
}
public final BooleanProperty adminProperty() {
return this.admin;
}
public final boolean isAdmin() {
return this.adminProperty().get();
}
public final void setAdmin(final boolean admin) {
this.adminProperty().set(admin);
}
}
DataModel.java:
package usermasterdetail;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class DataModel {
private final ObservableList<User> userList = FXCollections.observableArrayList(
new User("Jacob", "Smith", false),
new User("Isabella", "Johnson", true),
new User("Ethan", "Williams", false),
new User("Emma", "Jones", true),
new User("Michael", "Brown", true)
);
private final ObjectProperty<User> currentUser = new SimpleObjectProperty<>();
public final ObjectProperty<User> currentUserProperty() {
return this.currentUser;
}
public final User getCurrentUser() {
return this.currentUserProperty().get();
}
public final void setCurrentUser(final User currentUser) {
this.currentUserProperty().set(currentUser);
}
public ObservableList<User> getUserList() {
return userList;
}
}
TableController.java:
package usermasterdetail;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
public class TableController {
#FXML
private TableView<User> table ;
#FXML
private TableColumn<User, String> firstNameColumn ;
#FXML
private TableColumn<User, String> lastNameColumn ;
#FXML
private TableColumn<User, Boolean> adminColumn ;
private DataModel model ;
public void initialize() {
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
adminColumn.setCellValueFactory(cellData -> cellData.getValue().adminProperty());
adminColumn.setCellFactory(CheckBoxTableCell.forTableColumn(adminColumn));
}
public void setDataModel(DataModel dataModel) {
if (model != null) {
model.currentUserProperty().unbind();
}
this.model = dataModel ;
dataModel.currentUserProperty().bind(table.getSelectionModel().selectedItemProperty());
table.setItems(model.getUserList());
}
}
UserEditorController.java:
package usermasterdetail;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
public class UserEditorController {
#FXML
private TextField firstNameField ;
#FXML
private TextField lastNameField ;
#FXML
private CheckBox adminCheckBox ;
private String cachedFirstName ;
private String cachedLastName ;
private boolean cachedAdmin ;
private ChangeListener<User> userListener = (obs, oldUser, newUser) -> {
if (oldUser != null) {
firstNameField.textProperty().unbindBidirectional(oldUser.firstNameProperty());
lastNameField.textProperty().unbindBidirectional(oldUser.lastNameProperty());
adminCheckBox.selectedProperty().unbindBidirectional(oldUser.adminProperty());
}
if (newUser == null) {
firstNameField.clear();
lastNameField.clear();
adminCheckBox.setSelected(false);
} else {
firstNameField.textProperty().bindBidirectional(newUser.firstNameProperty());
lastNameField.textProperty().bindBidirectional(newUser.lastNameProperty());
adminCheckBox.selectedProperty().bindBidirectional(newUser.adminProperty());
cachedFirstName = newUser.getFirstName();
cachedLastName = newUser.getLastName();
cachedAdmin = newUser.isAdmin();
}
};
private DataModel model ;
public void setDataModel(DataModel dataModel) {
if (this.model != null) {
this.model.currentUserProperty().removeListener(userListener);
}
this.model = dataModel ;
this.model.currentUserProperty().addListener(userListener);
}
#FXML
private void cancel() {
firstNameField.setText(cachedFirstName);
lastNameField.setText(cachedLastName);
adminCheckBox.setSelected(cachedAdmin);
}
}
Table.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<StackPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="usermasterdetail.TableController">
<TableView fx:id="table">
<columns>
<TableColumn fx:id="firstNameColumn" text="First Name"/>
<TableColumn fx:id="lastNameColumn" text="Last Name"/>
<TableColumn fx:id="adminColumn" text="Administrator"/>
</columns>
</TableView>
</StackPane>
UserEditor.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="usermasterdetail.UserEditorController"
hgap="5" vgap="5" alignment="CENTER">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="NEVER"/>
<ColumnConstraints halignment="LEFT" hgrow="SOMETIMES"/>
</columnConstraints>
<padding>
<Insets top="5" left="5" bottom="5" right="5"/>
</padding>
<Label text="First Name:" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<Label text="Last Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<Label text="Admin Priviliges:" GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<TextField fx:id="firstNameField" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<TextField fx:id="lastNameField" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<CheckBox fx:id="adminCheckBox" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Button text="Cancel" onAction="#cancel" GridPane.columnIndex="0" GridPane.rowIndex="3" GridPane.columnSpan="2"
GridPane.halignment="CENTER"/>
</GridPane>
MainController.java:
package usermasterdetail;
import javafx.fxml.FXML;
public class MainController {
#FXML
private TableController tableController ;
#FXML
private UserEditorController editorController ;
private final DataModel model = new DataModel();
public void initialize() {
tableController.setDataModel(model);
editorController.setDataModel(model);
}
}
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.SplitPane?>
<SplitPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="usermasterdetail.MainController">
<items>
<fx:include fx:id="table" source="Table.fxml"/>
<fx:include fx:id="editor" source="UserEditor.fxml"/>
</items>
</SplitPane>
And finally Main.java:
package usermasterdetail;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("Main.fxml")), 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you prefer the user experience you described, you can (as #SSchuette describes in the comments), just bind the table's disable property to the modifying property. This will prevent the user from changing the selection while the data is being edited (i.e. is not consistent with the data in the table). For this you just need the modifying property in the model:
package usermasterdetail;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class DataModel {
private final ObservableList<User> userList = FXCollections.observableArrayList(
new User("Jacob", "Smith", false),
new User("Isabella", "Johnson", true),
new User("Ethan", "Williams", false),
new User("Emma", "Jones", true),
new User("Michael", "Brown", true)
);
private final ObjectProperty<User> currentUser = new SimpleObjectProperty<>();
private final BooleanProperty modifying = new SimpleBooleanProperty();
public final ObjectProperty<User> currentUserProperty() {
return this.currentUser;
}
public final usermasterdetail.User getCurrentUser() {
return this.currentUserProperty().get();
}
public final void setCurrentUser(final usermasterdetail.User currentUser) {
this.currentUserProperty().set(currentUser);
}
public ObservableList<User> getUserList() {
return userList;
}
public final BooleanProperty modifyingProperty() {
return this.modifying;
}
public final boolean isModifying() {
return this.modifyingProperty().get();
}
public final void setModifying(final boolean modifying) {
this.modifyingProperty().set(modifying);
}
}
then in the table controller you can bind the disable property to it:
package usermasterdetail;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
public class TableController {
#FXML
private TableView<User> table ;
#FXML
private TableColumn<User, String> firstNameColumn ;
#FXML
private TableColumn<User, String> lastNameColumn ;
#FXML
private TableColumn<User, Boolean> adminColumn ;
private DataModel model ;
public void initialize() {
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
adminColumn.setCellValueFactory(cellData -> cellData.getValue().adminProperty());
adminColumn.setCellFactory(CheckBoxTableCell.forTableColumn(adminColumn));
}
public void setDataModel(DataModel dataModel) {
if (model != null) {
model.currentUserProperty().unbind();
}
this.model = dataModel ;
dataModel.currentUserProperty().bind(table.getSelectionModel().selectedItemProperty());
table.setItems(model.getUserList());
table.disableProperty().bind(model.modifyingProperty());
}
}
The only place there is a bit of work to do is to make sure the modifying property is set to true any time the data are not in sync (though it sounds like you have already done this):
package usermasterdetail;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
public class UserEditorController {
#FXML
private TextField firstNameField ;
#FXML
private TextField lastNameField ;
#FXML
private CheckBox adminCheckBox ;
private DataModel model ;
private ChangeListener<Object> modifyingListener = (obs, oldValue, newValue) -> {
if (model != null) {
if (model.getCurrentUser() == null) {
model.setModifying(false);
} else {
model.setModifying(! (model.getCurrentUser().getFirstName().equals(firstNameField.getText())
&& model.getCurrentUser().getLastName().equals(lastNameField.getText())
&& model.getCurrentUser().isAdmin() == adminCheckBox.isSelected()));
}
}
};
private ChangeListener<User> userListener = (obs, oldUser, newUser) -> {
if (oldUser != null) {
oldUser.firstNameProperty().removeListener(modifyingListener);
oldUser.lastNameProperty().removeListener(modifyingListener);
oldUser.adminProperty().removeListener(modifyingListener);
}
if (newUser == null) {
firstNameField.clear();
lastNameField.clear();
adminCheckBox.setSelected(false);
} else {
firstNameField.setText(newUser.getFirstName());
lastNameField.setText(newUser.getLastName());
adminCheckBox.setSelected(newUser.isAdmin());
newUser.firstNameProperty().addListener(modifyingListener);
newUser.lastNameProperty().addListener(modifyingListener);
newUser.adminProperty().addListener(modifyingListener);
}
};
public void setDataModel(DataModel dataModel) {
if (this.model != null) {
this.model.currentUserProperty().removeListener(userListener);
}
this.model = dataModel ;
this.model.currentUserProperty().addListener(userListener);
}
public void initialize() {
firstNameField.textProperty().addListener(modifyingListener);
lastNameField.textProperty().addListener(modifyingListener);
adminCheckBox.selectedProperty().addListener(modifyingListener);
}
#FXML
private void cancel() {
if (model != null) {
firstNameField.setText(model.getCurrentUser().getFirstName());
lastNameField.setText(model.getCurrentUser().getLastName());
adminCheckBox.setSelected(model.getCurrentUser().isAdmin());
}
}
#FXML
private void update() {
if (model != null && model.getCurrentUser() != null) {
model.getCurrentUser().setFirstName(firstNameField.getText());
model.getCurrentUser().setLastName(lastNameField.getText());
model.getCurrentUser().setAdmin(adminCheckBox.isSelected());
}
}
}
This solution requires an additional button to force the update in the data (and table):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.HBox?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="usermasterdetail.UserEditorController"
hgap="5" vgap="5" alignment="CENTER">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="NEVER"/>
<ColumnConstraints halignment="LEFT" hgrow="SOMETIMES"/>
</columnConstraints>
<padding>
<Insets top="5" left="5" bottom="5" right="5"/>
</padding>
<Label text="First Name:" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<Label text="Last Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<Label text="Admin Priviliges:" GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<TextField fx:id="firstNameField" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<TextField fx:id="lastNameField" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<CheckBox fx:id="adminCheckBox" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<HBox spacing="5" alignment="CENTER" GridPane.columnIndex="0" GridPane.rowIndex="3" GridPane.columnSpan="2">
<Button text="Update" onAction="#update"/>
<Button text="Cancel" onAction="#cancel"/>
</HBox>
</GridPane>
I have this TableView
<TableView fx:id="tableView">
<columns>
<TableColumn prefWidth="220.0" text="Reference"
editable="true">
<cellFactory>
<fx:define>
<FXCollections fx:factory="observableArrayList" fx:id="items">
<Contract userFriendlyName=""/>
<Contract userFriendlyName="M1"/>
<Contract userFriendlyName="M2"/>
<Contract userFriendlyName="M3"/>
<Contract userFriendlyName="M4"/>
<Contract userFriendlyName="Q1"/>
<Contract userFriendlyName="Q2"/>
<Contract userFriendlyName="SWS1"/>
<Contract userFriendlyName="SWS2"/>
</FXCollections>
</fx:define>
<ChoiceBoxTableCellFactory
maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" items="$items">
</ChoiceBoxTableCellFactory>
</cellFactory>
<cellValueFactory>
<PropertyValueFactory property="referenceContract" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<GridRowModel>
<referenceContract>
<Contract name="" userFriendlyName="" />
</referenceContract>
</GridRowModel>
</FXCollections>
</items>
</TableView>
These are the underlying types used for the ChoiceBox items and rows.
public final class Contract {
private String name;
private String userFriendlyName;
public Contract() {
}
public Contract(String name) {
this(name, null);
}
public Contract(String name, String userFriendlyName) {
this.name = name;
this.userFriendlyName = userFriendlyName;
}
public String getName() {
return name;
}
public String getUserFriendlyName() {
return userFriendlyName;
}
public void setUserFriendlyName(String userFriendlyName) {
this.userFriendlyName = userFriendlyName;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return userFriendlyName;
}
}
public class GridRowModel {
private ObjectProperty<Contract> referenceContract = new SimpleObjectProperty<>();
public ObjectProperty<Contract> referenceContractProperty() {
return referenceContract;
}
public Contract getReferenceContract() {
return referenceContract.get();
}
public void setReferenceContract(Contract referenceContract) {
this.referenceContract.set(referenceContract);
}
}
I have this cellFactory with a ChoiceBox on the TableColumn
public class ChoiceBoxTableCellFactory<S, T> implements Callback<TableColumn<S, Contract>, TableCell<S, Contract>> {
private ObservableList<Contract> items;
private double maxHeight;
private double maxWidth;
public ChoiceBoxTableCellFactory() {
}
public ChoiceBoxTableCellFactory(
#NamedArg("maxHeight") double maxHeight,
#NamedArg("maxWidth") double maxWidth,
#NamedArg("items") ObservableList<Contract> items) {
this.maxHeight = maxHeight;
this.maxWidth = maxWidth;
this.items = items;
}
#Override
public TableCell<S, Contract> call(TableColumn<S, Contract> param) {
return new TableCell<S, Contract>() {
ChoiceBox<Contract> choiceBox = new ChoiceBox<>(items);
{
choiceBox.setMaxHeight(maxHeight);
choiceBox.setMaxWidth(maxWidth);
choiceBox.valueProperty().addListener((obs, oldValue, newValue) -> {
ObservableValue<Contract> value = getTableColumn().getCellObservableValue(getIndex());
if (value instanceof WritableValue) {
((WritableValue<Contract>) value).setValue(newValue);
}
});
}
#Override
protected void updateItem(Contract item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
setText(null);
setGraphic(null);
} else {
choiceBox.setValue(item);
setText(null);
setGraphic(choiceBox);
}
}
}
};
}
}
The table is rendered fine and I see the values from the fxml in the ChoiceBox. But when I click on any of the values, for example M1, the updateItem method is called with the item parameter which has the userFriendlyName set to "". That selects the first option on the ChoiceBox.
When I click again on the same ChoiceBox and select any option the updateItem method is not called again and the value does not change.
How do I need to setup the ChoiceBox to use the complex type Contract as an option and how do I fix the state change on the ChoiceBox?
The problem was, that in the fxml I haven't set the name property which is used in the equals implementation of Contract. Any change to the dropdown always returned the initial item.
I'm attempting to implement lazy loading into my application with sorting, but am having little issues with sorting and paginator. SortBy does work so like Paginator single but when i click on sort on first column with page > 0, the LazyDataModel#load is called with "first" parameter = 0!
Then instead of sorting data of page 1 return on page 0
Here is my managed bean class code:
#ViewScoped
#ManagedBean
public class PlayerMB implements Serializable {
private static final long serialVersionUID = 1L;
private LazyDataModel<Player> players = null;
private int sizePlayer;
private Player player;
public LazyDataModel<Player> getAllPlayers() {
if (players == null) {
players = new PlayerLazyList();
}
setSizePlayer(players.getRowCount());
return players;
}
public Player getPlayer() {
if(player == null){
player = new Player();
}
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public int getSizePlayer()
{
return sizePlayer;
}
public void setSizePlayer(int sizePlayer) {
this.sizePlayer = sizePlayer;
}
}
Player class code:
public class Player implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Override
public int hashCode() {
return getId();
}
#Override
public boolean equals(Object obj) {
if(obj instanceof Player){
Player player = (Player) obj;
return player.getId() == getId();
}
return false;
}
/*public int sortByModel(Object player1, Object player2)
{
return ((Player) player1).getName().compareTo(((Player) player2).getName());
}*/
}
dataTable.xhtml:
<f:view>
<h:form>
<p:dataTable id="lazyDataTable"
value="#{playerMB.allPlayers}"
var="player" paginator="true"
rows="10"
selection="#{playerMB.player}"
selectionMode="single"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15"
style="width: 80%;margin-left: 10%;
margin-right: 10%;"
lazy="true">
<f:facet name="header">List of players</f:facet>
<p:ajax event="rowSelect" update=":playerDialogForm" oncomplete="PF('playerDetails').show();" />
<p:ajax event="page"
listener="#{playerMB.casePage}"/>
<p:column sortBy="#{player.name}" headerText="Name" > <!-- sortBy does not work with lazyDataTable but more work occurs-->
<h:outputText value="#{player.name}" />
</p:column>
<p:column sortBy="#{player.age}" headerText="Age">
<h:outputText value="#{player.age}" />
</p:column>
<f:facet name="footer">tot: #{playerMB.sizePlayer}</f:facet>
</p:dataTable>
</h:form>
PlayerLazyList class code who extends LazyDataModel
public class PlayerLazyList extends LazyDataModel<Player> {
//private int pagina;
#Override
public List<Player> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, Object> filters) {
List<Player> allplayers = CreatePlayers.players; //here load all Player
System.out.println("first =" + first + "; pagesize =" + pageSize + "; sortField = " + sortField);
players = CreatePlayers.getPlayers(first, pageSize,allplayers.size()); //here load player from "first" for "max" element
if (sortField != null && sortField.equals("name") && sortOrder.name().equals("ASCENDING"))
Collections.sort(players, new NamePlayerComparatorAsc());
if (sortField != null && sortField.equals("name") && sortOrder.name().equals("DESCENDING"))
Collections.sort(players, new NamePlayerComparatorDesc());
System.out.println("size dopo" + players.size());
// set the total of players
if(getRowCount() <= 0){
//setRowCount(playerDAO.countPlayersTotal());
setRowCount(allplayers.size());
}
// set the page dize
setPageSize(pageSize);
return players;
}
private static final long serialVersionUID = 1L;
private List<Player> players;
//private MyTransaction transaction;
#Override
public Object getRowKey(Player player) {
return player.getId();
}
#Override
public Player getRowData(String playerId) {
Integer id = Integer.valueOf(playerId);
for (Player player : players) {
if(id.equals(player.getId())){
return player;
}
}
return null;
}
}
Primefaces 5.2, Wildfly 8.2.0, Mojarra 2.2.8
You seem to expect it sorts just the page you are on. That is just not how it works. It works perfectly and as designed and as expected by (almost) everybody (that is everybody but you). It sorts the full possible resultset. All items that were on page 0 will now most likely be spread over several other pages. So staying on the newly populalted page 1 totally does not make any sense.
The behaviour you describe can be seen in the showcase to iirc
There is a <p:selectOneListbox> with about 20 items. The first 5 items can be viewed in the list and then the remaining can be scrolled to and selected. The list is ajaxified. When the item numbers greater than 5 is selected (after scrolling to it) the scrollbar does not remain at its place; it moves to the top position. This makes the selected item (for example item 9) invisible. The program uses a ajax listener. The app uses PrimeFaces 5.0.
But, when the ajax listener is removed the selected (item 9) is visible and the scroller does not move to top (remove the p:ajax tag's listener attribute in the below JSF page).
I would like to know how to make the scrollbar not move when any item is selected while using the ajax listener.
The JSF page:
<p:selectOneListbox id="list"
scrollHeight="100"
value="#{bean.todo}">
<f:selectItems value="#{bean.data}"
var="t"
itemLabel="#{t.name}"
itemValue="#{t.name}"/>
<p:ajax process="#this"
update="#this msg"
listener="#{bean.valueChanged}" />
</p:selectOneListbox>
<br /><h:outputText id="msg" value="#{bean.message}" />
The bean's code:
import javax.faces.bean.*;
import java.io.Serializable;
import java.util.*;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.component.UIOutput;
#ManagedBean(name="bean")
#SessionScoped
public class TodosBean implements Serializable {
private List<Todo> data;
private String todo; // selected item value
private String msg;
public TodosBean() {
loadData();
if (data.size() == 0) {
return;
}
Todo t = data.get(0);
String name = t.getName();
setTodo(name); // select the first item in the list
setMessage(name);
}
private void loadData() {
data = new ArrayList<>();
data.add(new Todo("1first", "1"));
data.add(new Todo("2second", "2"));
data.add(new Todo("3third", "3"));
data.add(new Todo("4fourth", "4"));
data.add(new Todo("5fifth", "5"));
data.add(new Todo("6sixth", "6"));
data.add(new Todo("7seventh", "7"));
data.add(new Todo("8eighth", "8"));
}
public List<Todo> getData() {
return data;
}
public void setMessage(String msg) {
this.msg = msg;
}
public String getMessage() {
return msg;
}
public String getTodo() {
return todo;
}
public void setTodo(String t) {
todo = t;
}
public void valueChanged(AjaxBehaviorEvent e) {
String name = (String) ((UIOutput) e.getSource()).getValue();
setMessage(name + " selected.");
}
}
public class Todo {
private String name;
private String desc;
public Todo() {}
public Todo(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
#Override
public String toString() {
return this.name;
}
}
Remove the update="#this will solve it. Updating a control will move it to a default state. It might also be that in a newer version of PF this default state is improved and it still is at the correct position
I tried all to populate a TableView with data. The next code inserts a new row in table but the data not appear the table. I tried to find an explication for this without success.
Please help. I can't what is wrong.
In controller.java
#FXML private TableView<TempTableData> tempTable;
#FXML private TableColumn<TempTableData,String> columnTime;
#FXML private TableColumn<TempTableData,Float> columnTempOne;
#FXML private TableColumn<TempTableData,Float> columnTempTwo;
#FXML private TableColumn<TempTableData,Float> columnTempThree;
#FXML protected void initialize() {
columnTime = new TableColumn<TempTableData,String>();
columnTime.setCellValueFactory(
new PropertyValueFactory<TempTableData,String>("Time"));
columnTempOne = new TableColumn<TempTableData,Float>();
columnTempOne.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 1"));
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempTwo.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 2"));
columnTempThree = new TableColumn<TempTableData,Float>();
columnTempThree.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 3"));
tempDataList = FXCollections.observableArrayList();
tempDataList.add(new TempTableData("0",3.0f, 4f, 5f));
tempTable.setItems(tempDataList);
}
TempTableData.java
public class TempTableData {
private final SimpleStringProperty time;
private final SimpleFloatProperty dataSensorOne;
private final SimpleFloatProperty dataSensorTwo;
private final SimpleFloatProperty dataSensorThree;
public TempTableData(String time, float dataSensorOne, float dataSensorTwo, float dataSensorThree){
this.time = new SimpleStringProperty(time);
this.dataSensorOne = new SimpleFloatProperty(dataSensorOne);
this.dataSensorTwo = new SimpleFloatProperty(dataSensorTwo);
this.dataSensorThree = new SimpleFloatProperty(dataSensorThree);
}
public String getTime() {
return time.get();
}
public void setTime(String time) {
this.time.set(time);
}
public float getDataSensorOne() {
return dataSensorOne.get();
}
public void setDataSensorOne(float dataSensorOne) {
this.dataSensorOne.set(dataSensorOne);
}
public float getDataSensorTwo() {
return dataSensorTwo.get();
}
public void setDataSensorTwo(float dataSensorTwo) {
this.dataSensorTwo.set(dataSensorTwo);
}
public float getDataSensorThree() {
return dataSensorThree.get();
}
public void setDataSensorThree(float dataSensorThree) {
this.dataSensorThree.set(dataSensorThree);
}
public String toString(){
String string = String.format("[time: %s | dataSensorOne: %f |dataSensorTwo: %f |dataSensorThree: %f ]",
time.get(), dataSensorOne.get(), dataSensorTwo.get(), dataSensorThree.get());
return string;
}
}
why you creating table columns again ? ,as they already created with #FXML annotation (injection!).
remove column instance creation lines from your code and everything will be fine
// remove these lines
columnTime = new TableColumn<TempTableData,String>();
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempThree = new TableColumn<TempTableData,Float>();
make sure your cell factory values are spelt exactly as the corresponding model class values.
e.g.
private final .... SimpleStringProperty time;
and
new ...... PropertyValueFactory("time"));