我正在尝试制作一个javafx程序。
答案 0 :(得分:1)
您的计划中有几件事情应该修复或可以改进:
List<Entry>
代替。TableColumn
需要cellValueFactory
来显示列中的内容。否则他们仍然是空的。StringTokenizer
是不应使用的遗留类。请改用split
。这使事情变得更简单,因为你可以简单地对元素数量施加限制,而不是自己连接最后的匹配。TableView
项目列表。static
字段传递数据。使用方法参数将信息传递给您的方法,并使用返回值来获取信息。public class Main extends Application {
//reads textfile into list
static List<Entry> readPhonebook(Path path) throws Exception {
try (Stream<String> lines = Files.lines(path)) {
return lines.map(line -> line.split("\\s", 4))
.filter(array -> array.length == 4)
.map(array -> new Entry(array[0], array[1], array[2], array[3]))
.collect(Collectors.toList());
}
}
//prints list to file
static void listToFile(Path path, List<Entry> entries) throws Exception {
try (BufferedWriter bw = Files.newBufferedWriter(path)) {
for (Entry entry : entries) {
bw.append(entry.first).append('\t')
.append(entry.last).append('\t')
.append(entry.number).append('\t')
.append(entry.notes);
bw.newLine();
}
}
}
// adds entry to the file
static void appendEntryToFile(Path path, Entry entry) throws IOException {
try (BufferedWriter bw = Files.newBufferedWriter(path, StandardOpenOption.APPEND)) {
bw.append(entry.first).append('\t')
.append(entry.last).append('\t')
.append(entry.number).append('\t')
.append(entry.notes);
bw.newLine();
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
...
//file
Path f = Paths.get("phonebook.txt");
...
//more table stuff
TableView<Entry> table = new TableView<>();
TableColumn<Entry, String> firstcol = new TableColumn<>("First Name");
firstcol.setMinWidth(100);
firstcol.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().first));
TableColumn<Entry, String> lastcol = new TableColumn<>("Last Name");
lastcol.setMinWidth(100);
lastcol.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().last));
TableColumn<Entry, String> numcol = new TableColumn<>("Number");
numcol.setMinWidth(100);
numcol.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().number));
TableColumn<Entry, String> notescol = new TableColumn<>("Notes");
notescol.setMinWidth(100);
notescol.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().notes));
table.getColumns().addAll(firstcol, lastcol, numcol, notescol);
...
table.getItems().setAll(readPhonebook(f));
...
//saves info and makes textfields not editable and shows array in gui
btnEnt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String stfirst = null;
if (tffirst.getText() != null) {
stfirst = tffirst.getText();
}
String stlast = null;
if (tflast.getText() != null) {
stlast = tflast.getText();
}
String stnumber = null;
if (tfnumber.getText() != null) {
stnumber = tfnumber.getText();
}
String stnotes = null;
if (tfnotes.getText() != null) {
stnotes = tfnotes.getText();
}
Entry newEntry = new Entry(stfirst, stlast, stnumber, stnotes);
table.getItems().add(newEntry);
try {
appendEntryToFile(f, newEntry);
} catch (java.lang.Exception e) {
System.out.println("Error: File not saved.");
}
...
}
});
}
}
class Entry {
public String first, last, number, notes;
public Entry(String stfirst, String stlast, String stnumber, String stnotes) {
first = stfirst;
last = stlast;
number = stnumber;
notes = stnotes;
}
}