java - how to insert into a table the file name loaded with filechooser in javaFX? -
i'm trying populate table file name taken filechooser, in filename column, how can include filename in column of tableview ?
this interface 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> this class main
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); } } this controller
/** * sample skeleton '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 given fxmlloader private resourcebundle resources; @fxml // url location of fxml file given fxmlloader private url location; @fxml // fx:id="help" private menu help; // value injected fxmlloader @fxml // fx:id="btnspettro" private button btnspettro; // value injected fxmlloader @fxml // fx:id="lblanalisicom" private label lblanalisicom; // value injected fxmlloader @fxml // fx:id="tabregistra" private tab tabregistra; // value injected fxmlloader @fxml // fx:id="split1" private splitpane split1; // value injected fxmlloader @fxml // fx:id="menubar" private menubar menubar; // value injected fxmlloader @fxml // fx:id="lblanalisising" private label lblanalisising; // value injected fxmlloader @fxml // fx:id="btnspettrogram" private button btnspettrogram; // value injected fxmlloader @fxml // fx:id="btnanalizza" private button btnanalizza; // value injected fxmlloader @fxml // fx:id="coldata" private tablecolumn<populatetable, string> coldate; // value injected fxmlloader @fxml // fx:id="guidaonline" private menuitem guidaonline; // value injected fxmlloader @fxml // fx:id="colnome" private tablecolumn<populatetable, string> colname; // value injected fxmlloader @fxml // fx:id="table1" private tableview<populatetable> table1; // value injected fxmlloader @fxml // fx:id="hbox" private hbox hbox; // value injected fxmlloader @fxml // fx:id="btnpulsazioni" private button btnpulsazioni; // value injected fxmlloader @fxml // fx:id="btnrimuovi" private button btnrimuovi; // value injected fxmlloader @fxml // fx:id="colnum" private tablecolumn<populatetable,integer> colcount; // value injected fxmlloader @fxml // fx:id="tabelenco" private tab tabelenco; // value injected fxmlloader @fxml // fx:id="rightstatus" private label rightstatus; // value injected fxmlloader @fxml // fx:id="scroll2" private scrollpane scroll2; // value injected fxmlloader @fxml // fx:id="scroll1" private scrollpane scroll1; // value injected fxmlloader @fxml // fx:id="pane" private pane pane; // value injected fxmlloader @fxml // fx:id="about" private menuitem about; // value injected fxmlloader @fxml // fx:id="anchor1" private anchorpane anchor1; // value injected fxmlloader @fxml // fx:id="anchor2" private anchorpane anchor2; // value injected fxmlloader @fxml // fx:id="leftstatus" private label leftstatus; // value injected fxmlloader @fxml // fx:id="btnaggiungi" private button btnaggiungi; // value injected fxmlloader @fxml // fx:id="btnpicchi" private button btnpicchi; // value injected fxmlloader @fxml // fx:id="manuale" private menuitem manuale; // value injected fxmlloader @fxml // fx:id="btnformanti" private button btnformanti; // value injected fxmlloader @fxml // fx:id="btnintensita" private button btnintensita; // value injected fxmlloader @fxml // fx:id="tab1" private tabpane tab1; // value injected fxmlloader @fxml // fx:id="x3" private font x3; // value injected fxmlloader @fxml // fx:id="x4" private color x4; // value injected fxmlloader @fxml // fx:id="vbox" private vbox vbox; // value injected fxmlloader @fxml // fx:id="riconoscimenti" private menuitem riconoscimenti; // value injected fxmlloader @fxml // fx:id="separator" private separatormenuitem separator; // value injected 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 != null : "fx:id=\"help\" not injected: check fxml file 'startgui.fxml'."; assert btnspettro != null : "fx:id=\"btnspettro\" not injected: check fxml file 'startgui.fxml'."; assert lblanalisicom != null : "fx:id=\"lblanalisicom\" not injected: check fxml file 'startgui.fxml'."; assert tabregistra != null : "fx:id=\"tabregistra\" not injected: check fxml file 'startgui.fxml'."; assert split1 != null : "fx:id=\"split1\" not injected: check fxml file 'startgui.fxml'."; assert menubar != null : "fx:id=\"menubar\" not injected: check fxml file 'startgui.fxml'."; assert lblanalisising != null : "fx:id=\"lblanalisising\" not injected: check fxml file 'startgui.fxml'."; assert btnspettrogram != null : "fx:id=\"btnspettrogram\" not injected: check fxml file 'startgui.fxml'."; assert btnanalizza != null : "fx:id=\"btnanalizza\" not injected: check fxml file 'startgui.fxml'."; assert coldate != null : "fx:id=\"coldata\" not injected: check fxml file 'startgui.fxml'."; assert guidaonline != null : "fx:id=\"guidaonline\" not injected: check fxml file 'startgui.fxml'."; assert colname != null : "fx:id=\"colnome\" not injected: check fxml file 'startgui.fxml'."; assert table1 != null : "fx:id=\"table1\" not injected: check fxml file 'startgui.fxml'."; assert hbox != null : "fx:id=\"hbox\" not injected: check fxml file 'startgui.fxml'."; assert btnpulsazioni != null : "fx:id=\"btnpulsazioni\" not injected: check fxml file 'startgui.fxml'."; assert btnrimuovi != null : "fx:id=\"btnrimuovi\" not injected: check fxml file 'startgui.fxml'."; assert colcount != null : "fx:id=\"colnum\" not injected: check fxml file 'startgui.fxml'."; assert tabelenco != null : "fx:id=\"tabelenco\" not injected: check fxml file 'startgui.fxml'."; assert rightstatus != null : "fx:id=\"rightstatus\" not injected: check fxml file 'startgui.fxml'."; assert scroll2 != null : "fx:id=\"scroll2\" not injected: check fxml file 'startgui.fxml'."; assert scroll1 != null : "fx:id=\"scroll1\" not injected: check fxml file 'startgui.fxml'."; assert pane != null : "fx:id=\"pane\" not injected: check fxml file 'startgui.fxml'."; assert != null : "fx:id=\"about\" not injected: check fxml file 'startgui.fxml'."; assert anchor1 != null : "fx:id=\"anchor1\" not injected: check fxml file 'startgui.fxml'."; assert anchor2 != null : "fx:id=\"anchor2\" not injected: check fxml file 'startgui.fxml'."; assert leftstatus != null : "fx:id=\"leftstatus\" not injected: check fxml file 'startgui.fxml'."; assert btnaggiungi != null : "fx:id=\"btnaggiungi\" not injected: check fxml file 'startgui.fxml'."; assert btnpicchi != null : "fx:id=\"btnpicchi\" not injected: check fxml file 'startgui.fxml'."; assert manuale != null : "fx:id=\"manuale\" not injected: check fxml file 'startgui.fxml'."; assert btnformanti != null : "fx:id=\"btnformanti\" not injected: check fxml file 'startgui.fxml'."; assert btnintensita != null : "fx:id=\"btnintensita\" not injected: check fxml file 'startgui.fxml'."; assert tab1 != null : "fx:id=\"tab1\" not injected: check fxml file 'startgui.fxml'."; assert x3 != null : "fx:id=\"x3\" not injected: check fxml file 'startgui.fxml'."; assert x4 != null : "fx:id=\"x4\" not injected: check fxml file 'startgui.fxml'."; assert vbox != null : "fx:id=\"vbox\" not injected: check fxml file 'startgui.fxml'."; assert riconoscimenti != null : "fx:id=\"riconoscimenti\" not injected: check fxml file 'startgui.fxml'."; assert separator != null : "fx:id=\"separator\" not injected: check fxml file 'startgui.fxml'."; } } this utility class 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(); } }
just do
table1.getitems().add(new populatetable(0, filename, "")); i.e.
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, "")); } } (you might need change first , third parameter values appropriate.)
Comments
Post a Comment