• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to select different blocks of code when file selected in JFileChooser ?

 
Tom Storm
Ranch Hand
Posts: 31
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program where I have to open a file and then run a piece of code to do something with it. However since there are different files I want to run different pieces of code depending on the file. I have a JFileChooser setup and it works but I would like to have a something like an if else depending on the file extension.
My apologies is there is a simple answer as I haven't been able to find it.
Thank you.
 
Piet Souris
Rancher
Posts: 1638
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tom,

can you be a little bit more specific about your problem?
Since you are given a File from the JFileChooser, it is easy
to get the file extension, and so call every method that you
like based on this extension.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

To elaborate on what PS said: you can get the file name, then you can get the last index of "." and the substring following.
 
Tom Storm
Ranch Hand
Posts: 31
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your help. I was able to create a small class to find the extension and then I was able to set up an if else depending on what was returned. BTW how do you remove the very large greenhorn badge!?
Thanks again.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
About greenhorns, try here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!