我正在用javafx构建GUI。我是这个学科的新手,所以我仍然处于学习过程中。
在我的GUI中,我在表中显示一些数据。该数据在ObservableList中定义
private ObservableList<myclass> list = FXCollections.observableArrayList();
。
现在,我想通过在列表视图上选择表,创建类的对象并将该对象添加到list
来向表中添加一些数据。更清楚地说:我的类具有一个字符串变量和一个ComboBox变量,并且在将其添加到表中时,组合框应该为空。
将数据添加到表中时,我要确保在表中没有两次显示任何数据。在下面的代码中,我尝试实现此功能,但它不起作用:总是添加数据。我认为这是因为类对象包含一个组合框。
这是我的代码:
private void add_element(ActionEvent event) {
errorlabel.setText("");
if (listview.getSelectionModel().getSelectedItem() != null) {
String training;
training = listview.getSelectionModel().getSelectedItem();
ComboBox<String> newbox = new ComboBox<>(FXCollections.observableArrayList(choices));
myclass newtraining = new myclass(training, newbox);
if (list.contains(newtraining)) {
errorlabel.setText("Already existing!");
} else {
list.add(newtraining);
}
} else {
errorlabel.setText("Choose new training!");
errorlabel.setTextFill(Color.web("#ff0000"));
}
}
与
private String choices[] = {"Yes", "No", "Maybe"};
private ListView<String> listview = new ListView<>();
正如我说的那样,即使表中已经存在具有选定训练和类别的类对象,也总是将选择的数据添加到表中!
期待您的答复!
更新:我更改了我的问题,因为帖子被标记为重复,但我不认为其他主题确实回答了我的问题。
编辑2:您要求提供一个最小的示例,所以就在这里。我正在使用Netbeans和Scenebuilder,因此我总共有4个文件:
文件1:JavaFXApplication12.java
package javafxapplication12;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication12 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);
}
}
文件2:myclass.java
package javafxapplication12;
import javafx.scene.control.ComboBox;
public class myclass {
String training;
ComboBox<String> combobox;
public myclass(String training, ComboBox<String> combobox) {
this.training = training;
this.combobox = combobox;
}
public void setTraining(String training) {
this.training = training;
}
public void setCombobox(ComboBox<String> combobox) {
this.combobox = combobox;
}
public String getTraining() {
return training;
}
public ComboBox<String> getCombobox() {
return combobox;
}
}
文件3:FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="351.0" prefWidth="458.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication12.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TableView fx:id="table" layoutX="210.0" layoutY="84.0" prefHeight="200.0" prefWidth="206.0" />
<ListView fx:id="listview" layoutX="43.0" layoutY="104.0" prefHeight="179.0" prefWidth="150.0" />
<Button fx:id="add" layoutX="100.0" layoutY="297.0" mnemonicParsing="false" onAction="#add_element" text="Add" />
<Label fx:id="errorlabel" layoutX="211.0" layoutY="301.0" prefHeight="17.0" prefWidth="200.0" />
</children>
</AnchorPane>
最后是文件4(相关文件):
FXMLDocumentController.java
package javafxapplication12;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
public class FXMLDocumentController implements Initializable {
private String choices[] = {"Yes", "No", "Maybe"};
@FXML
private Label errorlabel;
@FXML
private ListView<String> listview = new ListView<>();
@FXML
private TableView<myclass> table = new TableView<myclass>();
@FXML
private TableColumn<myclass, String> training_col = new TableColumn<myclass, String>("Training");
@FXML
private TableColumn<myclass, ComboBox> box_col = new TableColumn<myclass, ComboBox>("Box");
private ObservableList<myclass> list = FXCollections.observableArrayList();
@FXML
private void add_element(ActionEvent event) {
errorlabel.setText("");
if (listview.getSelectionModel().getSelectedItem() != null) {
String training;
training = listview.getSelectionModel().getSelectedItem();
ComboBox<String> newbox = new ComboBox<>(FXCollections.observableArrayList(choices));
myclass newtraining = new myclass(training, newbox);
if (list.contains(newtraining)) {
errorlabel.setText("Already existing!");
} else {
list.add(newtraining);
}
} else {
errorlabel.setText("Choose new training or category!");
errorlabel.setTextFill(Color.web("#ff0000"));
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> trainings = FXCollections.observableArrayList();
trainings.add("Java");
trainings.add("C++");
trainings.add("Matlab");
trainings.add("Python");
listview.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
listview.getItems().addAll(trainings);
ComboBox<String> combo = new ComboBox<>(FXCollections.observableArrayList(choices));
list.add(new myclass("Java", combo));
combo.getSelectionModel().select("Yes");
table.setEditable(true);
table.setItems(list);
training_col.setMinWidth(110);
box_col.setMinWidth(45);
training_col.setCellValueFactory(new PropertyValueFactory<>("training"));
box_col.setCellValueFactory(new PropertyValueFactory<>("combobox"));
table.getColumns().addAll(training_col, box_col);
}
}
我试图使其尽可能简单,并将类简化为一个字符串变量和一个组合框。我想要的是:在列表视图中选择Java并按添加时,不应在表中添加其他条目。