在TableView[JavaFX]中插入不同的元素
我想知道如何在tableView的同一列中插入不同的元素 在我的视图(FXML)中,我有一个包含“person”的组合框,一个用于输入个人号码的文本字段和一个按钮,并在表视图中添加这些元素 FXML文件在TableView[JavaFX]中插入不同的元素,java,javafx,tableview,Java,Javafx,Tableview,我想知道如何在tableView的同一列中插入不同的元素 在我的视图(FXML)中,我有一个包含“person”的组合框,一个用于输入个人号码的文本字段和一个按钮,并在表视图中添加这些元素 FXML文件 <SplitPane dividerPositions="0.3002754820936639" focusTraversable="true" prefHeight="160.0" prefWidth="200.0"> <items> <AnchorPa
<SplitPane dividerPositions="0.3002754820936639" focusTraversable="true" prefHeight="160.0" prefWidth="200.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView fx:id="personnes" minWidth="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="200.0" text="Peronnes" fx:id="column">
<cellValueFactory>
<PropertyValueFactory property="nom" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ComboBox fx:id="listPeronnes" layoutX="25.0" layoutY="40.0" prefWidth="148.0">
</ComboBox>
<TextField fx:id="num" layoutX="25.0" layoutY="95.0" prefWidth="149.0" />
<Label layoutX="25.0" layoutY="78.0" prefWidth="66.0" text="numero" />
<Label layoutX="25.0" layoutY="14.0" prefWidth="66.0" text="Personnes" />
<Button onAction="#handleAdd" layoutX="25.0" layoutY="121.0" mnemonicParsing="false" text="Ajouter" />
</children>
</AnchorPane>
</items>
</SplitPane>
所以我要做的是,当用户启动应用程序时。他在文本字段中输入了一个数字,我必须在tableview中插入这个数字,然后如果用户想插入另一个人,但通过使用combobox,他在combobox中选择了一个人,并将这个人的名字添加到tableview中(因此我的Table View将包含数字和名字(两个字符串)
import java.net.URL;
导入java.util.ResourceBundle;
导入javafx.application.Platform;
导入javafx.beans.property.ListProperty;
导入javafx.beans.property.SimpleListProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.TextField;
导入javafx.stage.stage;
公共类SampleController实现可初始化{
私人舞台;
@FXML
受保护的组合框列表;
@FXML
受保护的TableView人员;
@FXML
保护表列;
@FXML
受保护的文本字段数;
可观察列表列表=
FXCollections.observableArrayList(新员工(“员工1”,1),新员工(“员工2”,2));
受保护的ListProperty prs=新的SimpleListProperty(FXCollections.observableArrayList());
@FXML
受保护的无效handleAdd()
{prs.add(num.getText());
添加(listPeronnes.getSelectionModel().getSelectedItem().getNom());
personnes.getItems().setAll(prs);
}
@凌驾
公共void初始化(URL、ResourceBundle rb){
listPeronnes.getItems().setAll(liste);
}
}
您不能在同一列中添加不同的元素类型,但您可以在TableView上加载和fxml,其中包含许多内容。请看这个。TableView上的节点将是带有文本字段的fxml,combobox…好的,谢谢,我会检查:)
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class SampleController implements Initializable {
private Stage stage;
@FXML
protected ComboBox<Personnes> listPeronnes;
@FXML
protected TableView personnes;
@FXML
protected TableColumn column;
@FXML
protected TextField num;
ObservableList<Personnes> liste =
FXCollections.observableArrayList(new Personnes("personne 1", 1),new Personnes("Personne 2",2));
protected ListProperty<String> prs=new SimpleListProperty<>(FXCollections.<String>observableArrayList());
@FXML
protected void handleAdd()
{ prs.add(num.getText());
prs.add(listPeronnes.getSelectionModel().getSelectedItem().getNom());
personnes.getItems().setAll(prs);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
listPeronnes.getItems().setAll(liste);
}
}