wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to open a file using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to open a file using Java" Watch "How to open a file using Java" New topic
Author

How to open a file using Java

Sujai Kaarthik
Ranch Hand

Joined: Dec 29, 2008
Posts: 162
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


The greater danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it
Ashutosh Chauhan
Greenhorn

Joined: Feb 26, 2009
Posts: 14
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

Joined: Dec 29, 2008
Posts: 162
thanks for the quick reply. I'll try and get back here
Ashutosh Chauhan
Greenhorn

Joined: Feb 26, 2009
Posts: 14
Welcome!
Sujai Kaarthik
Ranch Hand

Joined: Dec 29, 2008
Posts: 162
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Sujai Kaarthik
Ranch Hand

Joined: Dec 29, 2008
Posts: 162
will that affect the actual code?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
Will *what* effect the actual code?
Sujai Kaarthik
Ranch Hand

Joined: Dec 29, 2008
Posts: 162
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to open a file using Java
 
Similar Threads
ActionListener problem
JPanel
TEXTFIELDS TO INT
JFrame
Sample maintenance disply