我有一个名为“line”的对象包含两个属性“stCpl”和“list”`
public class line {
private String stCpl;
static int[] list = new int[26];
public void setStCpl(String stCpl) {
this.stCpl = stCpl;
}
public String getStCpl() {
return stCpl;
}
public static int[] getList() {
return list;
}
public static void setList(int[] list) {
line.list = list;
}
public line(String stCpl,int[] list) {
this.stCpl = stCpl;
line.list = list;
}
@Override
public String toString() {
return "line{" + "stCpl=" + stCpl + ", list=" + list + '}';
}
}
我不知道这个课程是否创建得很好,但我能做什么。
所以在 JavaFX 控制器上,我想创建“line”类的实例并将它们推送到 tableView 上
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Function;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
import javafx.util.Callback;
import org.omg.CORBA.INITIALIZE;
public class MainController implements Initializable{
@FXML
private TableView tableView;
@FXML
private TextField card;
@FXML
private TextField scndCpl;
@FXML
private TextField frstCpl;
@FXML
private Text cplContent;
@FXML
private Button validCpl;
@FXML
private Button anulCpl;
private String cpl = "";
private int limit ;
private couples couple;
private line line;
int[] list;
/**
* Initializes the controller class.
*/
public void setTableSize (){
int dimension = Integer.parseInt(card.getText()); // nombre des lignes et colonnes
tableView.getColumns().clear(); // vider toutes les colonnes
tableView.getItems().clear(); // vider toutes les lignes
limit = dimension * dimension ;
list = new int [dimension];
for (int i = 0; i < Integer.parseInt(card.getText()); i++) { //initialize all array elements to 0
list[i] = 0;
}
for (int i = 0; i < dimension +1; i++) {
char alphabet = (char) (i + 96);
if(i == 0) {alphabet = ' ';}
TableColumn<line, String> column = new TableColumn<>(Character.toString(alphabet));
if(i == 0) {
column.setCellValueFactory(new PropertyValueFactory<>("stCpl"));
} else {
column.setCellValueFactory(new PropertyValueFactory<>("list"));
}
tableView.getColumns().add(column);
}
int[] list = {0,0,0};
for (int i = 0; i < dimension; i++) {
char alphabet = (char) (i + 97);
tableView.getItems().add(new line(Character.toString(alphabet),list));
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
frstCpl.setFocusTraversable(false); // focus on 'cardinalité' TextField
scndCpl.setFocusTraversable(false);
tableView.setFocusTraversable(false);
}
}
现在表格视图充满了奇怪的符号,我猜这意味着数组是空的。
如果有更好的方法(或真正的解决方案)请告诉我它是什么。