2017-01-25 4 views
0

私はmediaplayerを作成しようとしており、mp3ファイルのメタデータをTableViewに挿入する必要があります。javafxを使ってTableViewにmp3/mp4ファイルのメタデータを簡単に挿入するには? (TableviewはScenebuilderを使用して構築されています)

ファイルのフォルダからデータを取得し、そのデータをTableViewに挿入するには、私は助けが必要です。検索されるデータは、タイトル、アルバム、およびアーティストです。

できるだけ単純なオプションが必要です。重要:TableViewはScenebuilderですでに作成されているため、コードがFXMLパートに接続されているのに役立ちます。

主な機能

package sample; 

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

public class Main extends Application { 


//launching from main page 
@Override 
public void start(Stage primaryStage) throws Exception{ 
    Parent root = FXMLLoader.load(getClass().getResource("main window.fxml")); 
    Scene scene = new Scene(root); 
    primaryStage.setTitle("WaterPlayer"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 


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

コントローラ

package sample; 

import javafx.event.ActionEvent; 
    import javafx.fxml.FXML; 
    import javafx.fxml.FXMLLoader; 
    import javafx.fxml.Initializable; 
    import javafx.geometry.Pos; 
    import javafx.scene.Node; 
    import javafx.scene.Parent; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.Label; 
    import javafx.scene.layout.VBox; 
    import javafx.stage.Modality; 
    import javafx.stage.Stage; 
    import java.io.IOException; 
    import java.net.URL; 
    import java.util.ResourceBundle; 




    public class Controller implements Initializable { 

    //button actions 
    @FXML 
    public void AddButtonClicked(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("add window.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 
    } 

    @FXML 
    public void MusicButtonClicked(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("music window.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 
    } 

    @FXML 
    public void MovieButtonClicked(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("movie window.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 
    } 

    @FXML 
    public void PlaylistButtonClicked(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("playlist window.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 

    } 


    @FXML 
    public void BackButtonClicked(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("main window.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 
    } 

    @FXML 
    public void RandomButtonClicked(ActionEvent event) throws IOException { 
     Stage window = new Stage(); 
     window.initModality(Modality.APPLICATION_MODAL); 
     window.setTitle(""); 
     window.setMinWidth(100); 
     Label label = new Label(); 
     label.setText("RANDOM:"); 
     Button musicButton = new Button("Music"); 
     Button movieButton = new Button("Movie"); 
     movieButton.setOnAction(e -> window.close()); 
     musicButton.setOnAction(e -> window.close()); 
     VBox layout = new VBox(10); 
     layout.getChildren().addAll(label, musicButton, movieButton); 
     layout.setAlignment(Pos.CENTER); 
     Scene scene = new Scene(layout); 
     window.setScene(scene); 
     window.showAndWait(); 
    } 

    @FXML 
    public void SearchBar(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("search window.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 
    } 

    @FXML 
    public void CreateButtonClicked(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("name playlist.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 
    } 

    @FXML 
    public void Playlistname(ActionEvent event) throws IOException { 
     Parent Main_parent = FXMLLoader.load(getClass().getResource("playlist window.fxml")); 
     Scene Main_scene = new Scene(Main_parent); 
     Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
     Main_stage.setScene(Main_scene); 
     Main_stage.show(); 
    } 



    @Override 
    public void initialize(URL location, ResourceBundle resources) { 

    } 
} 

FXMLファイル

<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.ChoiceBox?> 
<?import javafx.scene.control.ScrollPane?> 
<?import javafx.scene.control.TableColumn?> 
<?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.BorderPane?> 
<?import javafx.scene.layout.HBox?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> 
    <bottom> 
     <Button fx:id="Back" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#BackButtonClicked" prefHeight="25.0" prefWidth="60.0" text="Back" BorderPane.alignment="CENTER"> 
     <BorderPane.margin> 
      <Insets bottom="12.5" left="555.0" /> 
     </BorderPane.margin> 
     </Button> 
    </bottom> 
    <top> 
     <HBox prefHeight="50.0" prefWidth="200.0" BorderPane.alignment="CENTER"> 
     <children> 
      <ChoiceBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="150.0"> 
       <HBox.margin> 
        <Insets left="10.0" top="12.5" /> 
       </HBox.margin> 
      </ChoiceBox> 
      <TextField fx:id="SearchBar" onAction="#SearchBar" prefHeight="25.0" prefWidth="185.0" promptText="Search..."> 
       <HBox.margin> 
        <Insets left="295.0" top="12.5" /> 
       </HBox.margin> 
      </TextField> 
     </children> 
     <BorderPane.margin> 
      <Insets /> 
     </BorderPane.margin> 
     </HBox> 
    </top> 
    <center> 
     <ScrollPane prefHeight="196.0" prefWidth="197.0" BorderPane.alignment="CENTER"> 
     <content> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="400.0" prefWidth="630.0"> 
      <children> 
       <TableView fx:id="Musiclist" prefHeight="386.0" prefWidth="615.0"> 
       <columns> 

        <TableColumn fx:id="Title" prefWidth="219.0" text="Title"> 

        </TableColumn> 

        <TableColumn fx:id="Album" prefWidth="200.0" text="Album"> 

        </TableColumn> 

        <TableColumn fx:id="Author" prefWidth="219.0" text="Author"> 

        </TableColumn> 
       </columns> 
       </TableView> 

      </children> 
     </AnchorPane> 
    </content> 
    <BorderPane.margin> 
     <Insets bottom="12.5" left="10.0" right="10.0" /> 
    </BorderPane.margin> 
    </ScrollPane> 

と宋級

package sample; 
public class Song { 
    private String Title; 


    public String getTitle() { 
     return Title; 
    } 

    public void setTitle(String title) { 
     Title = title; 
    } 

    public String getAlbum() { 

     return Album; 
    } 

    public void setAlbum(String album) { 
     Album = album; 
    } 

    private String Album; 

    public String getAuthor() { 
     return Author; 
    } 

    public void setAuthor(String author) { 
     Author = author; 
    } 

    private String Author; 

} 

答えて

0

どのように私はMP3ファイルからID3メタデータを取得します:mp3agicまたはjaudiotaggerなどのメタデータを抽出するためのJavaライブラリを使用してください。例えばjaudiotaggerは、次の構文を使用します。

ObservableList<Song> songs = FXCollections.observableArrayList(); 
//populate your observable list here 
Musiclist.setItems(songs); 

このsnipetを調整し、テーブルビューがからのデータを知っているように、あなたのFXMLに追加:テーブルビューにデータを挿入する方法

AudioFile f = AudioFileIO.read(testFile); 
Tag tag = f.getTag(); 
AudioHeader = f.getAudioHeader(); 

tag.getFirst(FieldKey.ARTIST); 
tag.getFirst(FieldKey.ALBUM); 
tag.getFirst(FieldKey.TITLE); 
tag.getFirst(FieldKey.COMMENT); 
tag.getFirst(FieldKey.YEAR); 

あなたのソングクラスを表示する必要があります。

<TableColumn fx:id="Title" prefWidth="219.0" text="Title"> 
    <cellValueFactory> 
     <PropertyValueFactory property="Title" /> 
    </cellValueFactory> 
</TableColumn> 
+0

私はどのように観測可能リストを作成しますか? 「setItems」とタイプするたびに「setItems」というシンボルを解決できません –

+0

コード全体とエラー全体を表示してください。そうでないと問題の内容を伝えられません。オブジェクト(Musiclist、Titleなど)を小文字にしてください。そうでなければ、クラスと誤解されます([this(http://stackoverflow.com/questions/414001/variable-naming-conventions-in-java)を参照) 。 – Nash

+0

ObservableList songs = FXCollections.observableArrayList(); MusicList.setItems(曲);これを私のコントローラーに入力すると、「setItems 'のシンボルを解決できません」というメッセージが表示されます。 –

関連する問題