• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to open a file using Java

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
I created a GUI which asks the user to select a file using JFileChooser. To be precise, the user should select a *.class file. After selecting the file, the user needs to click a button which will display all the methods in the selected class file. I have no clue or whatsoever to get the file which has been chosen using JFileChooser. I know we should use reflection API to accomplish this task but my doubt is where to insert the necessary reflection api code or should we add more code to get the things done?What type of events should be created to achieve my goal?

I also post the code which I have created. It will be helpful for me, if I get some guidance from you guys...




I have deleted the menu operations to lessen the LOC here
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
you can visit here to know the details please do some research ,
for quick reference see this
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the quick reply. I'll try and get back here
 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome!
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still I cant get my problem done.

Let me point out my problem again..

* The user needs to select a *.class file using JFileChooser (it has been done)
* After the user has selected a file, he needs to press the Display All Methods button.
* All the methods in the selected file will be displayed in the table which has been created
* I know we should use Reflection API to find the methods.
* I need to know how we can point to the selected file, before using reflection? I am clueless in this thing. (I don't know how to put it in better words)
* What should I write in the actionPerformed(ActionEvent e) for the Display All Methods button in order to get my things done.
* I am also not sure about whether I have did all other code in the right way (I mean in the lines between 236 and 252)

Please make me clear on this issue.

Awaiting correct responses
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that reflection only works with *objects*, not with *classes*. So if you have some random class file you'll need to instantiate it first to use reflection. That's simple only if the class has a no-argument constructor.

If you can't make that assumption about the class you're better off using a library that works with classes, like BCEL.

This is not beginner's stuff, so I'll move it to the Intermediate forum.
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will that affect the actual code?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will *what* effect the actual code?
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant 'Does the program will alter(may be a wrong term) the actual byte code when we use BCEL and things like that?'. I read that we should be careful when we use Reflection API, because it deals with bytecodes..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic