我们所要做的就是打印用户创建的列表,我们已经实现了一个保存按钮,并且一度出现了保存对话框窗口,但现在没有显示任何内容。
作为参考,我们尝试打印到.txt或.jpg的列表视图名为boxTeam。
package poolproject;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.ListView;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.Window;
/**
*
* @author Alex
*/
public class FXMLDocumentController implements Initializable {
private Window primaryStage;
public void start(Stage primaryStage) {
}
@FXML
private Button buttonSave;
@FXML
private Button BtnAdd;
@FXML
private ListView<String> boxTeam;
@FXML
private ListView<String> boxPlayers;
ArrayList<String> team = new ArrayList();
String player;
final ObservableList<String> playersAvailable = FXCollections.observableArrayList(
"Kardi", "Gilmore", "Clark");
final ObservableList<String> teamOutput = FXCollections.observableArrayList(team);
@FXML
private void deleteAction(ActionEvent action) {
String selectedItem = boxPlayers.getSelectionModel().getSelectedItem();
playersAvailable.remove(selectedItem);
teamOutput.add(selectedItem);
playersAvailable.remove(selectedItem);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
boxPlayers.setItems(playersAvailable);
boxTeam.setItems(teamOutput);
}
// TRying a save button
private void saveAction(ActionEvent action) {
buttonSave.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
//SaveFile(My_Team, file);
}
}
}
}
}
);
}
}