Java 为什么一个TableView排序而另一个不排序?
免责声明:我想出了如何使这项工作,但我希望有人能帮助我理解为什么它没有我原来的工作方式 我有两个TableView,用户可以使用添加和删除按钮前后移动项目。我的“可用”TableView使用从数据库检索的ObservableList填充。我的“Selected”TableView通过解析逗号分隔的值字符串(从另一个控制器发送)来填充。当我在“可用”表视图中选择一个项目并单击“添加”时,它将被添加到“选定”表视图中,并正确排序。当我在“Selected”TableView中选择一个项目并单击“Remove”时,它被添加到“Available”TableView中,但它被添加到末尾,并且没有正确排序 作为一项测试,我更改了“可用”表视图的填充,以解析ObservableList并添加每个项目,类似于填充“选定”列表的方式。然后,当我单击Remove时,该项目被添加到“可用”表视图并正确排序 所以我希望有人不会介意帮助我理解为什么必须这样做,如果我的方法效率不高,也许会建议更好的方法 TwoTablesView.fxml:Java 为什么一个TableView排序而另一个不排序?,java,sorting,javafx,tableview,fxml,Java,Sorting,Javafx,Tableview,Fxml,免责声明:我想出了如何使这项工作,但我希望有人能帮助我理解为什么它没有我原来的工作方式 我有两个TableView,用户可以使用添加和删除按钮前后移动项目。我的“可用”TableView使用从数据库检索的ObservableList填充。我的“Selected”TableView通过解析逗号分隔的值字符串(从另一个控制器发送)来填充。当我在“可用”表视图中选择一个项目并单击“添加”时,它将被添加到“选定”表视图中,并正确排序。当我在“Selected”TableView中选择一个项目并单击“Re
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="248.0" prefWidth="422.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="twotables.TwoTablesController">
<children>
<HBox prefHeight="240.0" prefWidth="600.0">
<children>
<TableView fx:id="availableMRTsTableView" prefHeight="240.0" prefWidth="150.0">
<columns>
<TableColumn fx:id="availableColumn" prefWidth="150.0" text="Available MRTs" />
</columns>
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</TableView>
<VBox alignment="CENTER" spacing="10.0">
<children>
<Button alignment="TOP_CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onAdd" text="Add -->" />
<Button alignment="TOP_CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onRemove" text="<-- Remove" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
</VBox>
<TableView fx:id="selectedMRTsTableView" prefHeight="240.0" prefWidth="150.0">
<columns>
<TableColumn fx:id="selectedColumn" prefWidth="150.0" text="Selected MRTs" />
</columns>
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TableView>
</children>
</HBox>
</children>
</AnchorPane>
package twotables;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class TwoTablesController implements Initializable
{
@FXML
private TableView<String> availableMRTsTableView;
@FXML
private TableView<String> selectedMRTsTableView;
@FXML
private TableColumn<String, String> availableColumn;
@FXML
private TableColumn<String, String> selectedColumn;
private ObservableList<String> availableList;
private ObservableList<String> selectedList;
@Override
public void initialize(URL url, ResourceBundle rb) {
// This will allow the table to select multiple rows at once
availableMRTsTableView.getSelectionModel().setSelectionMode(
SelectionMode.MULTIPLE);
selectedMRTsTableView.getSelectionModel().setSelectionMode(
SelectionMode.MULTIPLE);
// Add Cell Value Factory
availableColumn.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue())
);
selectedColumn.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue())
);
// Make it sortable
availableMRTsTableView.getSortOrder().add(availableColumn);
selectedMRTsTableView.getSortOrder().add(selectedColumn);
// Get rid of the "no content" when the table is empty
availableMRTsTableView.setPlaceholder(new Label(""));
selectedMRTsTableView.setPlaceholder(new Label(""));
// This was the original way I tried to populate the Table. If I
// populate the table like the following, the sorting doesn't work
//availableMRTsTableView.setItems(availableList);
// populate the Column lists (this will be coming from the database)
availableList = FXCollections.<String>observableArrayList("AA","DD","FF");
availableList.forEach(mrt -> {
availableMRTsTableView.getItems().add(mrt);
});
// real code would be calling this from another controller
setSelectedMRTs(new String("BB,CC,EE"));
}
@FXML
private void onAdd(ActionEvent event)
{
// Get the columns selected
ObservableList<String> userSelection = availableMRTsTableView.
getSelectionModel().getSelectedItems();
// Add the columns to the Selected MRTs Table
selectedMRTsTableView.getItems().addAll(userSelection);
// Remove the columns from the Available MRTs Table
availableMRTsTableView.getItems().removeAll(userSelection);
// Sort the Tables
availableMRTsTableView.sort();
selectedMRTsTableView.sort();
// Clear the selections form the table
availableMRTsTableView.getSelectionModel().clearSelection();
}
@FXML
private void onRemove(ActionEvent event) {
// Get the columns selected
ObservableList<String> userSelection = selectedMRTsTableView.
getSelectionModel().getSelectedItems();
// Add the columns to the Available MRTs Table
availableMRTsTableView.getItems().addAll(userSelection);
// Remove the columns from the Selected MRTs Table
selectedMRTsTableView.getItems().removeAll(userSelection);
// Sort the Tables
availableMRTsTableView.sort();
selectedMRTsTableView.sort();
// Clear the selections form the table
selectedMRTsTableView.getSelectionModel().clearSelection();
}
public void setSelectedMRTs(String mrtString)
{
String mrt;
// Populate the table
StringTokenizer st = new StringTokenizer(mrtString,",");
while (st.hasMoreTokens())
{
mrt = st.nextToken();
// Remove column from the Available Columns Table
availableMRTsTableView.getItems().remove(mrt);
// Add column to the Selected Columns Table
selectedMRTsTableView.getItems().add(mrt);
}
// Sort the tables
availableMRTsTableView.sort();
selectedMRTsTableView.sort();
}
}
package labelbindtest;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class LabelBindTest extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
(看看@Slaw,这次我的帖子的格式是对的,哈哈)请提供一个描述,让我们重现这个问题。除非我点击其中一列删除/反转排序,否则当我试图重现问题时,项目顺序是正常的…@fabien我添加了一条评论。很抱歉,我的原始版本没有明确指出,请包括一个描述,允许我们复制该问题。除非我点击其中一列删除/反转排序,否则当我试图重现问题时,项目顺序是正常的…@fabien我添加了一条评论。对不起,我原来的那张没有清楚地说明