Search code examples
javalistviewjavafxfilechooser

ListView and FileChooser


I am having a trouble with ListView accepting content. here is the code so if someone could give me an advice:

  public void button1Action(){
    Main main=new Main();
    FileChooser fc=new FileChooser();
    fc.setInitialDirectory(new File("C:\\Users"));
    List<File>selectedFiles=fc.showOpenMultipleDialog(null);
    if(selectedFiles!=null){
       for(int i=0;i<selectedFiles.size();i++){
           main.lview.getItems().add(selectedFiles.get(i).getAbsolutePath());
       } 
    }else{
        System.out.println("File is not Valid");
    }
}

After i finish choosing files it the paths do not show on listview.

Edit: Even if i manually through the code add some string it doesn;t show anything this is my Main class:

public class Main extends Application {
BorderPane root=new BorderPane();
ListView<String> lview=new ListView<String>();

 Button btn_load = new Button("Load");
Button btn_play = new Button("Play");
Button btn_stop = new Button("Stop");
Button btn_next = new Button("Next");
Button btn_previous = new Button("Previous");
Button btn_rewind = new Button("Rewind");
TextField tf_pesma=new TextField();
Slider slider=new Slider();
HBox hbox=new HBox();
HBox hbox1=new HBox();

@Override
public void start(Stage primaryStage) {
    Logic logic=new Logic();
    slider.setValue(50);
    slider.setMin(0);
    slider.setMax(100);
    slider.setMaxWidth(100);
    lview.setDisable(false);
    lview.setVisible(true);
    lview.setPrefWidth(800);

 btn_load.setOnAction(e->{
     logic.button1Action();
    });
 lview.setOnMouseClicked(new EventHandler<MouseEvent>(){
     @Override
     public void handle(MouseEvent event){

     }
 });


    VBox floor = new VBox();
    floor.setPadding(new Insets(10,10,10,10)); 
    floor.setSpacing(10);
    floor.getChildren().addAll(addInHBox(),addInHBox1());
    VBox right=new VBox();
    right.setPadding(new Insets(10,10,10,10));
    right.setSpacing(10);
    HBox button=new HBox();
    button.setAlignment(Pos.CENTER);
    button.getChildren().add(btn_load);
    right.getChildren().addAll(lview,button);
    root.setBottom(floor);
    root.setRight(right);
    Scene scene = new Scene(root, 900, 580);

    primaryStage.setTitle("Music Player");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}
public HBox addInHBox(){
    hbox.setPadding(new Insets(10,10,10,10));
    hbox.setSpacing(10);
    hbox.getChildren().addAll(btn_play,btn_stop,btn_next,btn_previous,btn_rewind);
    return hbox;
}
public HBox addInHBox1(){
    hbox1.setPadding(new Insets(10,10,10,10));
    hbox1.setSpacing(10);
    tf_pesma.setPrefWidth(700);
    hbox1.getChildren().addAll(tf_pesma,slider);
    return hbox1;
}
}

Solution

  • You are creating new Main instance in Logic class. The ListView displayed on the screen is not owned by the new Main instance you created.

    To get rid of this problem, access lview of Main that already exists.

    // Add this in Main class
    private void button1Action() {
        FileChooser fc = new FileChooser();
        fc.setInitialDirectory(new File("C:\\Users"));
        List<File> selectedFiles = fc.showOpenMultipleDialog(null);
        if (selectedFiles != null) {
            for (int i = 0; i < selectedFiles.size(); i++) {
                lview.getItems().add(selectedFiles.get(i).getAbsolutePath());
            }
        } else {
            System.out.println("File is not Valid");
        }
    }
    

    And change the handler.

    btn_load.setOnAction(e->{
        button1Action();
    });