简体   繁体   English

如何使用 Javafx 显示我在选项卡窗格中选择的文件的内容?

[英]How can i display the content of a file i chose in my tab pane using Javafx?

This code will use filechooser to select a file that is anything that ends with.cvs so when i choose the grades.cvs file i need it to display the content within the file within the tab.此代码将使用 filechooser 到 select 一个以.cvs 结尾的文件,因此当我选择grades.cvs 文件时,我需要它在选项卡内显示文件中的内容。 All it does is once the file is picked it does not display it.它所做的只是一旦选择了文件,它就不会显示它。 I tried to use tab1.setContent this is my first time using tab i see people display messages in tab with label.我尝试使用 tab1.setContent 这是我第一次使用选项卡,我看到人们使用 label 在选项卡中显示消息。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;[enter image description here][1]
import java.util.Scanner;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

public class Controller {

    @FXML
    private Tab tab1;

    @FXML
    private Tab tab2;


    @FXML
    void openFile(ActionEvent event) {
        FileChooser fc = new FileChooser();
        fc.setTitle("Open Text File");
        fc.setInitialDirectory(new File("."));
        fc.getExtensionFilters().addAll(new ExtensionFilter("Text Files", "*.csv"));
        File selectedFile = fc.showOpenDialog(null);


        TextArea content = new TextArea();
        content.setEditable(false);



        if (selectedFile == null)
            content.setText("No file chosen.");
        else
        {
            Scanner scan = new Scanner(selectedFile);

            String info = "";
            while (scan.hasNext())
                info += scan.nextLine() + "\n";

            content.setText(info);
        }
        tab1.setContent(content);
    }

    @FXML
    void openWindow(ActionEvent event) {
        Stage subWindow = new Stage();
        Label lbl1 = new Label("This is about the developer of app");

        Group grp = new Group(lbl1);
        Scene sn = new Scene(grp,300,300);
        subWindow.setScene(sn);
        subWindow.show();
    }

}

Here is some code we wrote in 2015 to test using a CSV file as a database这是我们在 2015 年编写的一些代码,用于使用 CSV 文件作为数据库进行测试
It is backed by a Model Class so we are using Model View Controller它由 Model Class 支持,所以我们使用 Model 查看 Z9BBF373797BF7CF72562C8002368
the first code post is how we save the data then how we fetch the data第一个代码帖子是我们如何保存数据然后我们如何获取数据

    @FXML
private void onSave(ActionEvent e) throws IOException {

    // TableData = FXCollections.observableArrayList();
    // Hold data for the table

    if (txfFName.getText().isEmpty() || txfLName.getText().isEmpty()|| txfPNum.getText().isEmpty() || txfEMail.getText().isEmpty()) {
        Alert alert = new Alert(AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText("");
        alert.setContentText("Data Entry NOT Complete\n"
        + "\nENTER New Data in All Fields and Alt + S to SAVE");
        alert.showAndWait();

    if (txfFName.getText().isEmpty()) {
        txfFName.requestFocus();
    return;
    }
    if (txfLName.getText().isEmpty()) {
        txfLName.requestFocus();
    return;
    }
    if (txfPNum.getText().isEmpty()) {
        txfPNum.requestFocus();
    return;
    }
    if (txfEMail.getText().isEmpty()) {
        txfEMail.requestFocus();
    return;
    }
    return;
    }

    if (txfFName.getText().length() > 15) {
        Alert alert = new Alert(AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText("");
        alert.setContentText("First Name Max Length is 15 Characters");
        alert.showAndWait();
        txfFName.requestFocus();
    return;
    }

    if (txfLName.getText().length() > 16) {
        Alert alert = new Alert(AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText("");
        alert.setContentText("Last Name Max Length is 16 Characters");
        alert.showAndWait();
        txfLName.requestFocus();
    return;
    }

    if (txfPNum.getText().length() > 12) {
        Alert alert = new Alert(AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText("");
        alert.setContentText("Phone Number Max Length is 12 Characters");
        alert.showAndWait();
        txfPNum.requestFocus();
    return;
    }

    if (txfEMail.getText().length() > 30) {
        Alert alert = new Alert(AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText("");
        alert.setContentText("E-Mail Max Length is 30 Characters");
        alert.showAndWait();
        txfEMail.requestFocus();
    return;
    }

    String fname = txfFName.getText().trim();
    String lname = txfLName.getText().trim();
    String pnum = txfPNum.getText().trim();
    String email = txfEMail.getText().trim();

    String data = (fname + "," + lname + "," + pnum + "," + email + "," + '\r');

    File dirPath = new File("C:/A_CSV");
    dirPath.mkdirs();// Make the directory

    File file = new File(dirPath, "People.csv");
    if (!file.exists()) {
            file.createNewFile();// Create an empty text file
    }

Her is the fetch data code她是获取数据代码

    @FXML
// Load CSV Data
private void onLoad(ActionEvent e) throws IOException {

// TableData = FXCollections.observableArrayList();//Hold data for the table

    File fileInfo = new File("C:/A_CSV/People.csv");
    if (fileInfo.length() == 0) {
        Alert alert = new Alert(AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText("");
        alert.setContentText("No Data in File at " + fileInfo + "\n"
        + "\nEnter Data and Save");
        alert.showAndWait();
    return;
    }

    fc.setTitle("Load Contacts Info");
    fc.setInitialDirectory(new File("C:/"));
    fc.setInitialDirectory(new File("C:/A_CSV"));
    fc.setInitialFileName("People.csv");
    File file = fc.showOpenDialog(null);
    if (file == null) {
            return;
    }

    String correctFile = file.getName();
    if (!(correctFile.matches("People.csv"))) {
        Alert alert = new Alert(AlertType.ERROR);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText("");
        alert.setContentText("The File at " + file + "\n\n"
        + "is NOT accociated with this application\n\n"
        + "Select the File at " + fileInfo);
        alert.showAndWait();
    return;
    }

    unAdd();
    table.getItems().clear();// Clears the table
    onShowTableData();

}

Here is the Model Class Just a bunch of Getters and Setters这是 Model Class 只是一堆 Getter 和 Setter

public class Person {
// This belongs to CSV Controller
// ===============================
private final StringProperty firstName = new SimpleStringProperty(this,"firstName", null);
private final StringProperty lastName = new SimpleStringProperty(this,"lastName", null);
private final StringProperty phoneNumber = new SimpleStringProperty(this,"phoneNumber", null);
private final StringProperty emailAddress = new SimpleStringProperty(this,"emailAddress", null);

public Person() {
    this(null, null, null, null);
}

public Person(String firstName, String lastName, String phoneNumber,String emailAddress) {
    this.firstName.set(firstName);
    this.lastName.set(lastName);
    this.phoneNumber.set(phoneNumber);
    this.emailAddress.set(emailAddress);
}

/* firstName Property */
public final String getFirstName() {// 1
    return firstName.get();
}

public final void setFirstName(String firstName) {// 1
    firstNameProperty().set(firstName);
}

public final StringProperty firstNameProperty() {// 1
    return firstName;
}

public final String getLastName() {// 2
    return lastName.get();
}

public final void setLastName(String lastName) {// 2
    lastNameProperty().set(lastName);
}

public final StringProperty lastNameProperty() {// 2
    return lastName;
}

public final String getPhoneNumber() {// 3
    return phoneNumber.get();
}

public final void setPhoneNumber(String phoneNumber) {// 3
    phoneNumberProperty().set(phoneNumber);
}

public final StringProperty phoneNumberProperty() {// 3
    return phoneNumber;
}

public final String getEmailAddress() {
    return emailAddress.get();
}

public final void setEmailAddress(String emailAddress) {
    emailAddressProperty().set(emailAddress);
}

public final StringProperty emailAddressProperty() {
    return emailAddress;
}

If you want the whole project let me know and I will zip it up and put on GitHub如果您想要整个项目,请告诉我,我会 zip 安装并安装 GitHub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM