Java select a file 🔎

Поделиться
HTML-код
  • Опубликовано: 29 дек 2024

Комментарии • 79

  • @BroCodez
    @BroCodez  4 года назад +36

    public class Main{
    public static void main(String[] args) {

    // JFileChooser = A GUI mechanism that let's a user choose a file (helpful for opening or saving files)

    new MyFrame();
    }
    }
    import java.awt.FlowLayout;
    import java.awt.event.*;
    import java.io.File;
    import javax.swing.*;
    public class MyFrame extends JFrame implements ActionListener{
    JButton button;

    MyFrame(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    button = new JButton("Select File");
    button.addActionListener(this);

    this.add(button);
    this.pack();
    this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    if(e.getSource()==button) {

    JFileChooser fileChooser = new JFileChooser();

    fileChooser.setCurrentDirectory(new File(".")); //sets current directory

    int response = fileChooser.showOpenDialog(null); //select file to open
    //int response = fileChooser.showSaveDialog(null); //select file to save

    if(response == JFileChooser.APPROVE_OPTION) {
    File file = new File(fileChooser.getSelectedFile().getAbsolutePath());
    System.out.println(file);
    }
    }
    }
    }

    • @joyceasante8292
      @joyceasante8292 Год назад

      Practicing...
      public class Main{
      public static void main(String[]args){
      new MyFrame();
      }
      }
      *****************************
      import java.awt.FlowLayout;
      import java.awt.event.*;
      import java.io.File;
      import javax.swing.*;
      public class MyFrame extends JFrame implements ActionListener{
      JButton button;
      MyFrame(){
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLayout(new FlowLayout());
      button = new JButton("Choose a file");
      button.addActionListener(this);
      this.add(button);
      this.pack();
      this.setVisible(true);
      }
      @Override
      public void actionPerformed(ActionEvent e){
      if(e.getSource()==button){
      JFileChooser fileChooser = new JFileChooser();
      // fileChooser.setCurrentDirectory(new File(""));
      fileChooser.setCurrentDirectory(new File("C:\\Users\\Yaa\\Desktop"));

      //System.out.println(fileChooser.showOpenDialog(null));
      //int response = fileChooser.showOpenDialog(null);
      int response = fileChooser.showSaveDialog(null);
      if(response ==JFileChooser.APPROVE_OPTION){
      File file = new File(fileChooser.getSelectedFile().getAbsolutePath());
      System.out.println(file);
      }

      }
      }
      }

  • @revolutionaryfrog
    @revolutionaryfrog 2 года назад +8

    this channel has single handedly gotten me through all my JavaFX learning. Insane value.

  • @alexsimone5265
    @alexsimone5265 4 года назад +6

    bro this channel is so awesome. You have the most helpful vids, get straight to the point, and explain everything well. 10/10 major props to you

    • @BroCodez
      @BroCodez  4 года назад

      thanks for watching Alex

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 Год назад

    This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro

  • @eugeneh5401
    @eugeneh5401 2 года назад

    Love your content, keep up the good work! Really appreciate your tutorials. You have helped me out countless of times!

  • @hisokuh2709
    @hisokuh2709 5 месяцев назад

    Big ups BroCodez u dun know, i was bare scared my code aint gon work but you solved it u mad lad!

  • @annpadilla5638
    @annpadilla5638 11 месяцев назад

    Excellent content

  • @bethgee910
    @bethgee910 3 года назад +2

    Great vid! Please could you link me to the videos you have on saving and opening the files?

  • @eugenezuev7349
    @eugenezuev7349 7 месяцев назад

    it's a really helpful tutorial, thanks

  • @guramee
    @guramee Год назад

    4:26 "Nuclear launch codes" xD

  • @Mirtguitar
    @Mirtguitar Год назад

    Good tutorial

  • @speklex779
    @speklex779 Год назад

    Nice video

  • @indrajithhettiarachchi5700
    @indrajithhettiarachchi5700 2 года назад

    Keep it up bro

  • @attalaw36
    @attalaw36 3 года назад

    You're the best teacher.

  • @ZayidAwcabdi
    @ZayidAwcabdi Год назад

    Thank you very much bro❤ you helped us a lot🎉

  • @mediadimah22
    @mediadimah22 3 года назад

    Thanks for your great work! Please, maybe more explanation for each assignment.

  • @loaykoshak
    @loaykoshak 3 года назад

    thank you bro keep forward

  • @tristisludos
    @tristisludos 3 года назад

    Thanks for the video mate, you're a real help

  • @nhanthanh4493
    @nhanthanh4493 2 года назад

    nice

  • @daniellima9370
    @daniellima9370 2 года назад

    Thanks. Your video helped me a lot.

  • @usamanadeem145
    @usamanadeem145 3 года назад

    Help ful

  • @NatyTor
    @NatyTor 2 года назад

    hi bro, i so love your video man!!! keep going like this!!🤟🤟🤟

  • @angelcastineira2561
    @angelcastineira2561 2 года назад

    Great video!

  • @sovxui3705
    @sovxui3705 3 года назад +1

    Is their a way to print it on a JTextArea when you open a file?

  • @augischadiegils.5109
    @augischadiegils.5109 4 года назад +1

    Very helpful :) thanks bro

  • @mohamedoutlouhou8866
    @mohamedoutlouhou8866 3 года назад

    good job Bro

  • @ABC-zv1sd
    @ABC-zv1sd 2 года назад

    I am developing an android Filemanager app. I used checkbox to select files and folders(files+subfolders) . I want to add feature to copy paste the selected files and folders(files+subfolders) at same time like google Filemanager . How can i add this copy paste feature in my java android file manager??? Advance Thanks..

  • @fernandoribeiro4384
    @fernandoribeiro4384 2 года назад

    Love you bro! Thanks a lot for your vids, very helpful ! Comment done ehehehe enjoy and keep up! Best regards

  • @karimshalabi6878
    @karimshalabi6878 2 года назад

    how can you set this to open on action in a gui, for example i press a load file button and this comes up to choose a file etc...

  • @rabbit_alt
    @rabbit_alt 2 года назад

    nice video, thanks.

  • @رند-س5ح
    @رند-س5ح 2 года назад

    Thanks dude that helps a lot

  • @yazilimportali6431
    @yazilimportali6431 2 года назад

    thanks that helped me

  • @mdnadeemazam4382
    @mdnadeemazam4382 2 года назад

    Instead of printing the path I want to store it in a String to use for another function, how can I get a from this function?

  • @MrSadeeq67
    @MrSadeeq67 3 года назад

    You are my Hero Bro.!

  • @monwil3296
    @monwil3296 4 года назад

    Bro, 👍👌

  • @shaynazoedeguzman7418
    @shaynazoedeguzman7418 2 года назад

    Excellent!

  • @henriquefelinimena3693
    @henriquefelinimena3693 3 года назад

    thank you for the content

  • @olajuwon4224
    @olajuwon4224 2 года назад

    Thank you brother. You earned a subscription from me.

  • @MrVegetox
    @MrVegetox 2 года назад

    Thanks, Brah!

  • @muhammedkabbani6434
    @muhammedkabbani6434 2 года назад

    perfect

  • @shawonmajid
    @shawonmajid 3 года назад

    8:34 Nuclear launch codes! :3

  • @masterofkaarsvet
    @masterofkaarsvet 2 года назад

    Is there a way to make a drag/drop interface for files?

  • @javazantech
    @javazantech Год назад

    Thanks from Brazil , how to save image path in mysql database

  • @maaahhdi
    @maaahhdi 3 года назад

    Very helpful.

  • @davidmonsalve8525
    @davidmonsalve8525 3 года назад

    bro, you're the best

  • @MageHunter
    @MageHunter 2 года назад

    Thank you so mutch!!!!!!

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 года назад

    thanks bro

  • @paveludovichenko9547
    @paveludovichenko9547 2 года назад

    could you add the implementation of DnD facilities for this button?

  • @MZ-tz1bt
    @MZ-tz1bt 3 года назад

    super

  • @lequangnghi194
    @lequangnghi194 3 года назад

    Hi, I've been wondering, instead of "this", what else that I can put in the addActionListener(this) that will match the parameter?

    • @programmer5834
      @programmer5834 2 года назад +1

      You can given parent component where you implement ActionListener . actually he using same class that why he use (this)
      I hope you understand

    • @spenzr6920
      @spenzr6920 2 года назад

      im assuming u asked this cz u did that in the main method
      in that case,
      you need to construct an object of your class (idk how to say it sry my eng isnt good)
      Here is my code, i hope u will get it
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      public class abc implements ActionListener {
      public static void main(String[] args) {
      JFrame frame = new JFrame("Title");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(420,420);
      frame.setLayout(null);
      //Creating the Button
      JButton button = new JButton("I am a button :D");
      button.setBounds(0, 0, 150,69);
      //creating an object of the class (idk how to say it lol sry but im sure yk of it)
      abc abcObject = new abc();
      //Using the object instead of this keyword
      button.addActionListener(abcObject);

      //There is also another way, i guess its called lambda expression
      //This has no relation to the previous button we created earlier
      JButton button2 = new JButton("2nd button");
      button2.setBounds(69,69,100,69);
      button2.addActionListener(e -> System.out.println("This action was performed using lambda expression"));


      //adding button and making frame visible
      frame.add(button);
      frame.add(button2);
      frame.setVisible(true);

      }
      @Override
      public void actionPerformed(ActionEvent e) {

      System.out.println("69420 ");

      }
      }

    • @spenzr6920
      @spenzr6920 2 года назад

      but i would recommend u doing the way he did in the tutorial. I think its simpler
      there are probably more ways to do it, this one is simplest, u just have to use 'this' and its done

  • @alexshepel5599
    @alexshepel5599 4 года назад

    Awesome, bro :)

  • @ROTEN31
    @ROTEN31 2 года назад

    I have a question, how can I copy the selected file?

  • @robertofranciscoriosmorale4158
    @robertofranciscoriosmorale4158 2 года назад

    thnx dude, helped a lot

  • @LISAtheNINJAA
    @LISAtheNINJAA 3 года назад

    Amazing!! :D

  • @longkesh1971
    @longkesh1971 Год назад

    Thanks bro.

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Год назад

    Thanks!

  • @comandanteshran4123
    @comandanteshran4123 2 года назад

    thanks! it's what i need...

  • @ishmaelbrentuo
    @ishmaelbrentuo 3 года назад

    Pls how to u add file to read in Jframe ?
    Need help.

  • @kemann3815
    @kemann3815 3 года назад

    Perfecto

  • @dancing_frank_lee
    @dancing_frank_lee 3 года назад

    thank you so much

  • @gemma_sll
    @gemma_sll 2 года назад

    thanks man

  • @andreasm6636
    @andreasm6636 3 года назад

    my uni project: stonks

  • @abbasalaa2935
    @abbasalaa2935 6 месяцев назад

    عاشت ايدك

  • @nawfalnjm5699
    @nawfalnjm5699 3 года назад

    thank you

  • @zari_723
    @zari_723 2 года назад

    thanks

  • @CptKaracho66
    @CptKaracho66 2 года назад

    A comment down below

  • @Liveafghan79
    @Liveafghan79 9 месяцев назад

  • @amr_badran2948
    @amr_badran2948 Год назад

    hello

  • @greeneggsandmushrooms9855
    @greeneggsandmushrooms9855 3 года назад

    file selects you

  • @akashsahu5716
    @akashsahu5716 Год назад

    bow down in front of bro

  • @zstar8397
    @zstar8397 Год назад

    Hey hope you are doing alright just I wanna say that
    GOD loved the world so much he sent his only begotten
    son Jesus to die a brutal death for us so that we can have eternal life and we can all accept this amazing gift this by simply trusting in Jesus, confessing that GOD raised him from the dead, turning away from your sins and forming a relationship with GOD.

  • @kagailawrence
    @kagailawrence 3 года назад

    super

  • @nitindixit676
    @nitindixit676 Год назад

    thank you