java - 如何将javaFX中使用filechooser加载的文件名插入到表中?

标签 java javafx tableview scenebuilder filechooser

我正在尝试使用 filechooser 获取的文件名填充表,在 fileName 列中,如何在 Tableview 的此列中包含文件名?

这是与 sceneBuilder 的界面

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import javafx.scene.media.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<VBox fx:id="VBox" prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
  <children>
    <MenuBar fx:id="MenuBar" nodeOrientation="RIGHT_TO_LEFT" VBox.vgrow="NEVER">
      <menus>
        <Menu fx:id="Help" mnemonicParsing="false" text="Help">
          <items>
            <MenuItem fx:id="GuidaOnline" mnemonicParsing="false" text="GuidaOnline" />
                  <MenuItem fx:id="Manuale" mnemonicParsing="false" text="Manuale" />
                  <SeparatorMenuItem fx:id="Separator" mnemonicParsing="false" />
                  <MenuItem fx:id="About" mnemonicParsing="false" text="About" />
                  <MenuItem fx:id="Riconoscimenti" mnemonicParsing="false" text="Riconoscimenti" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
    <SplitPane fx:id="Split1" dividerPositions="0.289532293986637, 0.7806236080178174" focusTraversable="true" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
      <items>
        <AnchorPane fx:id="Anchor1">
          <children>
                  <TabPane fx:id="Tab1" layoutX="6.0" layoutY="5.0" prefHeight="391.0" prefWidth="245.0" tabClosingPolicy="UNAVAILABLE">
                    <tabs>
                      <Tab fx:id="TabElenco" text="Elenco suoni">
                           <content>
                              <ScrollPane fx:id="Scroll1" prefHeight="362.0" prefWidth="227.0">
                                 <content>
                                    <TableView fx:id="Table1">
                                      <columns>
                                        <TableColumn fx:id="colCount" prefWidth="26.0" text="#" />
                                        <TableColumn fx:id="colName" prefWidth="124.0" text="NomeFile" />
                                          <TableColumn fx:id="colDate" prefWidth="79.0" text="Data" />
                                      </columns>
                                    </TableView>
                                 </content>
                              </ScrollPane>
                           </content>
                        </Tab>
                      <Tab fx:id="tabRegistra" text="Registra" />
                    </tabs>
                  </TabPane>
                  <Button fx:id="btnAggiungi" layoutX="18.0" layoutY="437.0" mnemonicParsing="false" onAction="#AddFileAudio" prefHeight="43.0" prefWidth="98.0" text="Aggiungi" />
                  <Button fx:id="btnRimuovi" layoutX="145.0" layoutY="437.0" mnemonicParsing="false" prefHeight="43.0" prefWidth="98.0" text="Rimuovi" />
          </children>
        </AnchorPane>
        <ScrollPane fx:id="Scroll2" prefHeight="551.0" prefWidth="496.0" />
        <AnchorPane fx:id="Anchor2" prefHeight="551.0" prefWidth="205.0">
               <children>
                  <Label fx:id="lblAnalisiSing" alignment="CENTER" layoutX="14.0" layoutY="14.0" opacity="0.5" prefHeight="37.0" prefWidth="142.0" text="Analisi singola:" />
                  <Button fx:id="btnSpettrogram" layoutX="14.0" layoutY="51.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="142.0" text="Spettrogramma" />
                  <Button fx:id="btnSpettro" layoutX="15.0" layoutY="83.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="142.0" text="Spettro" />
                  <Button fx:id="btnFormanti" layoutX="14.0" layoutY="117.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="142.0" text="Formanti" />
                  <Button fx:id="btnPicchi" layoutX="15.0" layoutY="150.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="142.0" text="Picchi" />
                  <Button fx:id="btnIntensita" layoutX="14.0" layoutY="183.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="142.0" text="Intensità" />
                  <Button fx:id="btnPulsazioni" layoutX="15.0" layoutY="217.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="142.0" text="Pulsazioni" />
                  <Button fx:id="btnAnalizza" layoutX="14.0" layoutY="395.0" mnemonicParsing="false" prefHeight="43.0" prefWidth="142.0" text="Analizza" />
                  <Label fx:id="lblAnalisiCom" alignment="CENTER" layoutX="16.0" layoutY="352.0" opacity="0.5" prefHeight="27.0" prefWidth="142.0" text="Analisi completa:" />
               </children>
        </AnchorPane>
      </items>
    </SplitPane>
    <HBox id="HBox" fx:id="Hbox" alignment="CENTER_LEFT" spacing="5.0" VBox.vgrow="NEVER">
      <children>
        <Label fx:id="LeftStatus" maxHeight="1.7976931348623157E308" maxWidth="-1.0" HBox.hgrow="ALWAYS">
          <font>
            <Font size="11.0" fx:id="x3" />
          </font>
          <textFill>
            <Color blue="0.625" green="0.625" red="0.625" fx:id="x4" />
          </textFill>
        </Label>
        <Pane fx:id="Pane" prefHeight="-1.0" prefWidth="-1.0" HBox.hgrow="ALWAYS" />
        <Label fx:id="RightStatus" font="$x3" maxWidth="-1.0" textFill="$x4" HBox.hgrow="NEVER" />
      </children>
      <padding>
        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
      </padding>
    </HBox>
  </children>
</VBox>

这是主类

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("StartGUI.fxml"));
            Scene scene = new Scene(root,900,550);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这是一个 Controller

/**
 * Sample Skeleton for 'StartGUI.fxml' Controller Class
 */

package application;

import java.io.File;
import java.io.PipedInputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;


import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.util.Callback;
import utility.TableFileValue;
import utility.Person;
import utility.PopulateTable;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;

public class Controller implements Initializable{

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="Help"
    private Menu Help; // Value injected by FXMLLoader

    @FXML // fx:id="btnSpettro"
    private Button btnSpettro; // Value injected by FXMLLoader

    @FXML // fx:id="lblAnalisiCom"
    private Label lblAnalisiCom; // Value injected by FXMLLoader

    @FXML // fx:id="tabRegistra"
    private Tab tabRegistra; // Value injected by FXMLLoader

    @FXML // fx:id="Split1"
    private SplitPane Split1; // Value injected by FXMLLoader

    @FXML // fx:id="MenuBar"
    private MenuBar MenuBar; // Value injected by FXMLLoader

    @FXML // fx:id="lblAnalisiSing"
    private Label lblAnalisiSing; // Value injected by FXMLLoader

    @FXML // fx:id="btnSpettrogram"
    private Button btnSpettrogram; // Value injected by FXMLLoader

    @FXML // fx:id="btnAnalizza"
    private Button btnAnalizza; // Value injected by FXMLLoader

    @FXML // fx:id="ColData"
    private TableColumn<PopulateTable, String> colDate; // Value injected by FXMLLoader

    @FXML // fx:id="GuidaOnline"
    private MenuItem GuidaOnline; // Value injected by FXMLLoader

    @FXML // fx:id="ColNome"
    private TableColumn<PopulateTable, String> colName; // Value injected by FXMLLoader

    @FXML // fx:id="Table1"
    private TableView<PopulateTable> Table1; // Value injected by FXMLLoader

    @FXML // fx:id="Hbox"
    private HBox Hbox; // Value injected by FXMLLoader

    @FXML // fx:id="btnPulsazioni"
    private Button btnPulsazioni; // Value injected by FXMLLoader

    @FXML // fx:id="btnRimuovi"
    private Button btnRimuovi; // Value injected by FXMLLoader

    @FXML // fx:id="ColNum"
    private TableColumn<PopulateTable,Integer> colCount; // Value injected by FXMLLoader

    @FXML // fx:id="tabElenco"
    private Tab tabElenco; // Value injected by FXMLLoader

    @FXML // fx:id="RightStatus"
    private Label RightStatus; // Value injected by FXMLLoader

    @FXML // fx:id="Scroll2"
    private ScrollPane Scroll2; // Value injected by FXMLLoader

    @FXML // fx:id="Scroll1"
    private ScrollPane Scroll1; // Value injected by FXMLLoader

    @FXML // fx:id="Pane"
    private Pane Pane; // Value injected by FXMLLoader

    @FXML // fx:id="About"
    private MenuItem About; // Value injected by FXMLLoader

    @FXML // fx:id="Anchor1"
    private AnchorPane Anchor1; // Value injected by FXMLLoader

    @FXML // fx:id="Anchor2"
    private AnchorPane Anchor2; // Value injected by FXMLLoader

    @FXML // fx:id="LeftStatus"
    private Label LeftStatus; // Value injected by FXMLLoader

    @FXML // fx:id="btnAggiungi"
    private Button btnAggiungi; // Value injected by FXMLLoader

    @FXML // fx:id="btnPicchi"
    private Button btnPicchi; // Value injected by FXMLLoader

    @FXML // fx:id="Manuale"
    private MenuItem Manuale; // Value injected by FXMLLoader

    @FXML // fx:id="btnFormanti"
    private Button btnFormanti; // Value injected by FXMLLoader

    @FXML // fx:id="btnIntensita"
    private Button btnIntensita; // Value injected by FXMLLoader

    @FXML // fx:id="Tab1"
    private TabPane Tab1; // Value injected by FXMLLoader

    @FXML // fx:id="x3"
    private Font x3; // Value injected by FXMLLoader

    @FXML // fx:id="x4"
    private Color x4; // Value injected by FXMLLoader

    @FXML // fx:id="VBox"
    private VBox VBox; // Value injected by FXMLLoader

    @FXML // fx:id="Riconoscimenti"
    private MenuItem Riconoscimenti; // Value injected by FXMLLoader

    @FXML // fx:id="Separator"
    private SeparatorMenuItem Separator; // Value injected by FXMLLoader



    String fileName;

    @FXML
    /**
     * Apertura della finestra fileChooser per il caricamento dei file audio,
     * sono accettati solo file con estensione .wav e .mp3.
     * Estrazione del nome del file.
     * 
     * @param event 
     */
    public void AddFileAudio(ActionEvent event) {
         FileChooser fileChooser = new FileChooser();
         fileChooser.setTitle("Seleziona il file da caricare");
         fileChooser.getExtensionFilters().addAll(
                 new ExtensionFilter("Audio Files", "*.wav", "*.mp3"));
         File selectedFile = fileChooser.showOpenDialog(new Stage());        
         if (selectedFile != null) {
             fileName = selectedFile.getName();
             colName.setText(fileName);

             }

   }


    public ObservableList<PopulateTable>    list = FXCollections.observableArrayList(
            new PopulateTable(1, fileName, "date1"),
            new PopulateTable(2, "name2", "date2"),
            new PopulateTable(3, "name3", "date3")); 


    public void initialize(URL location, ResourceBundle resource) {

        colCount.setCellValueFactory(new PropertyValueFactory<PopulateTable,Integer>("count"));
        colName.setCellValueFactory(new PropertyValueFactory<PopulateTable,String>("name"));
        colDate.setCellValueFactory(new PropertyValueFactory<PopulateTable,String>("date"));
        Table1.setItems(list);

        assert Help != null : "fx:id=\"Help\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnSpettro != null : "fx:id=\"btnSpettro\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert lblAnalisiCom != null : "fx:id=\"lblAnalisiCom\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert tabRegistra != null : "fx:id=\"tabRegistra\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Split1 != null : "fx:id=\"Split1\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert MenuBar != null : "fx:id=\"MenuBar\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert lblAnalisiSing != null : "fx:id=\"lblAnalisiSing\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnSpettrogram != null : "fx:id=\"btnSpettrogram\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnAnalizza != null : "fx:id=\"btnAnalizza\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert colDate != null : "fx:id=\"ColData\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert GuidaOnline != null : "fx:id=\"GuidaOnline\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert colName != null : "fx:id=\"ColNome\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Table1 != null : "fx:id=\"Table1\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Hbox != null : "fx:id=\"Hbox\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnPulsazioni != null : "fx:id=\"btnPulsazioni\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnRimuovi != null : "fx:id=\"btnRimuovi\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert colCount != null : "fx:id=\"ColNum\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert tabElenco != null : "fx:id=\"tabElenco\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert RightStatus != null : "fx:id=\"RightStatus\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Scroll2 != null : "fx:id=\"Scroll2\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Scroll1 != null : "fx:id=\"Scroll1\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Pane != null : "fx:id=\"Pane\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert About != null : "fx:id=\"About\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Anchor1 != null : "fx:id=\"Anchor1\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Anchor2 != null : "fx:id=\"Anchor2\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert LeftStatus != null : "fx:id=\"LeftStatus\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnAggiungi != null : "fx:id=\"btnAggiungi\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnPicchi != null : "fx:id=\"btnPicchi\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Manuale != null : "fx:id=\"Manuale\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnFormanti != null : "fx:id=\"btnFormanti\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert btnIntensita != null : "fx:id=\"btnIntensita\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Tab1 != null : "fx:id=\"Tab1\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert x3 != null : "fx:id=\"x3\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert x4 != null : "fx:id=\"x4\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert VBox != null : "fx:id=\"VBox\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Riconoscimenti != null : "fx:id=\"Riconoscimenti\" was not injected: check your FXML file 'StartGUI.fxml'.";
        assert Separator != null : "fx:id=\"Separator\" was not injected: check your FXML file 'StartGUI.fxml'.";

    }

}

这是一个实用程序类 PopulateTable

package utility;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class PopulateTable {
    private final SimpleIntegerProperty count;
    private final SimpleStringProperty name;
    private final SimpleStringProperty date;
    public PopulateTable(int count, String name, String date) {
        super();
        this.count = new SimpleIntegerProperty(count);
        this.name = new SimpleStringProperty(name);
        this.date = new SimpleStringProperty(date);
    }
    public int getCount() {
        return count.get();
    }
    public String getName() {
        return name.get();
    }
    public String getDate() {
        return date.get();
    }
}

最佳答案

就这么做

Table1.getItems().add(new PopulateTable(0, fileName, ""));

public void AddFileAudio(ActionEvent event) {

    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Seleziona il file da caricare");
    fileChooser.getExtensionFilters().addAll(
            new ExtensionFilter("Audio Files", "*.wav", "*.mp3"));
    File selectedFile = fileChooser.showOpenDialog(new Stage());        
    if (selectedFile != null) {
        String fileName = selectedFile.getName();
        Table1.getItems().add(new PopulateTable(0, fileName, ""));
    }

}

(您可能需要将第一个和第三个参数值更改为适当的值。)

关于java - 如何将javaFX中使用filechooser加载的文件名插入到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533457/

相关文章:

java - Android Recyclerview 滚动更新日历

德国的 Java-Scanner 语言环境在对 double 值求和时返回 0

java - 在 libgdx 中定位 Sprite

java - 如何通过更改标题(即旋转对象)来避免与边框或彼此之间的碰撞

java - 如何以编程方式旋转背景?

java - 如何删除或更新 ObservableList 中的某些行

java - 获取 Int 的存储值并在不同的类中使用它

java - JavaFX TableView 中的属性绑定(bind)不起作用

JavaFX TableView 行已添加但不可见

ios - 带有复选标记和详细信息披露的表格 View