• Post Reply Bookmark Topic Watch Topic
  • New Topic

menu item and jfilechooser

 
mark smith
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

openMenuItem is a JMenuItem
when i click on openMenuItem, i would like to be able to choose a file




The method showOpenDialog(Component) in the type JFileChooser don't seem to be applicable for the arguments (new ActionListener(){})


any idea?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the method signature takes an argument of type Component
public int showOpenDialog(Component parent) throws HeadlessException

Inside an inner class, by using the keyword 'this' as the argument,
you are supplying the inner class type as the argument i.e. ActionListener,
which is not a Component.

if your class is similar to this

public class Something extends JFrame
{

}

this line
int choice = fc.showOpenDialog(this);

should be
int choice = fc.showOpenDialog(Something.this);
 
mark smith
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i select the file, i would like to pass it to another class i created



Can't do that...
Cannot refer to a non-final variable imgPane inside an inner class defined in a different method

what is the best way to resolve the problem
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cannot refer to a non-final variable imgPane inside an inner class defined in a different method
You never can. If you wish to refer to a variable inside an inner class, it must be
  • A final local variable, or
  • a field. I am not sure whether fields of the inner class count.
  • So you need to redeclare your imgPane as a final local variable or as a field.
     
    mark smith
    Ranch Hand
    Posts: 257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    is there a better way to manage the event (menuitem)?
     
    Campbell Ritchie
    Marshal
    Posts: 52574
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just move the declaration of your imgPane variable so it becomes a field. And it is not "Testthis."

    And this format is shorter:-
    [ July 15, 2006: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 52574
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But I can think of improvements.

    If you use J5.0 or higher, you can lose the line about the content pane. Your "pane.add();" statement becomes
    Be sure to use BorderLayout.CENTER rather than "Center" to avoid potential spelling errors. You will have to import it.

    You would do better to have imgPane initialised before you get to the ActionListener, and give it a setAndDisplayFile() method which takes your File as a parameter.
     
    mark smith
    Ranch Hand
    Posts: 257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i could also do something like



    don't know if it's better
     
    Campbell Ritchie
    Marshal
    Posts: 52574
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Learn how to write an anonymous inner class for your ActionListener. I have written many times on these boards about how to do it, and also why, for example here. It saves you all this messing about with "getActionCommand()." If you have several buttons with different ActionListeners, your actionPerformed() method will turn into a right mess.

    Move the JFileChooser. You are going to use it several times. Declare it as a field and initialise it in your constructor.
    You will end up with something like this:-And of course, I have already told you that you need a setAndDisplayFile(File f) method in your ImagePane class.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!