Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Norm

Thread: I'm trying to open a "FileOpenDialog"

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default I'm trying to open a "FileOpenDialog"

    I'm trying to open a "FileOpenDialog" but am getting two err msgs:

    1) openFileButton cannot be resolved or is not a field RTTCPSortSimMain.java

    2) RTTCPSortSimMain cannot be resolved to a variable

    public class OpenFileButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.openFileButton) { // 1
    final JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(RTTCPSortSimMain); //2
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    // This is where a real application would open the file.
    // log.append("Opening: " + file.getName() + "." + newline);
    } else {
    // log.append("Open command cancelled by user." + newline);
    }
    }
    }
    }

    openFileButton is the name of the JButton
    RTTCPSortSimMain is the name of the class this all lives in

    What should I be using in these places instead?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    Java Code:
    == this.openFileButton)
    Where is openFileButton defined? Is it in scope where you are trying to use it? The this. reference changes the scope to the current class. What class is that code in? It looks like an inner class. Try removing the this.
    Java Code:
    showOpenDialog(RTTCPSortSimMain)
    What data type is RTTCPSortSimMain?
    What does the API doc for the showOpenDialog() method say is the type of its argument?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Thanks Norm. I won't waste my time here then...

    db

  5. #5
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Java Code:
    == this.openFileButton)
    Where is openFileButton defined? Is it in scope where you are trying to use it? The this. reference changes the scope to the current class. What class is that code in? It looks like an inner class. Try removing the this.
    Removing the "this" alone didn't change the err msg, but moving its declaration "up" did.

    I did have it being declared and instantiated (by copying the template code for the other button) inside the "go()" method:

    Java Code:
    JButton openFileButton = new JButton("Open File");
    	openFileButton.addActionListener(new OpenFileButtonListener());
    ...but now I declare it in the outer class, and then instantiate it in the inner class:

    Java Code:
    public class RTTCPSortSimMain {
    	. . .
    	JButton openFileButton;
    
    +
    
    public void go() {
    	. . .
    	JButton sendButton = new JButton("Send and Receive");
    	sendButton.addActionListener(new SendButtonListener());
    	// above is the legacy (copied from a book) code; below is "my" button
    	openFileButton = new JButton("Open File");
    	openFileButton.addActionListener(new OpenFileButtonListener());

  6. #6
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Java Code:
    showOpenDialog(RTTCPSortSimMain)
    What data type is RTTCPSortSimMain?
    What does the API doc for the showOpenDialog() method say is the type of its argument?
    It's the parent, but can be null; I used null and it compiles and seems to work fine. I'm sure you already knew this, but am responding here for future searche[r]s.

  7. #7
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sorry about that; I didn't know I had done that, unless you mean on the javaprogramming forums (as opposed to this one, java-forums.org). If so, I didn't figure both forums would be comprised of the same denizens (Javazens?)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    What data type is RTTCPSortSimMain?
    It's the parent,
    It looks like the name of your class. What data type does the showOpenDialog() method take as an argument?

    In some locations of your code, "this" could have a data type of RTTCPSortSimMain. But how is that any use when calling the showOpenDialog() method???

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    Cross posting without notification that you are doing it means that we're wasting time answering a question that is already answered on another forum. Many of us don't like that.

  10. #10
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What data type is RTTCPSortSimMain?

    It looks like the name of your class. What data type does the showOpenDialog() method take as an argument?

    In some locations of your code, "this" could have a data type of RTTCPSortSimMain. But how is that any use when calling the showOpenDialog() method???
    Sorry for the confusion - what I meant by "It's the parent" is that's what should be pased to JFileChooser. You're right, I originally had the class name there (I was just guessing what should go there based on an example I had, rather than really thinking it through - I know, that was my first mistake. Actually, my Nth mistake, but we won't talk about that.

  11. #11
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Cross posting without notification that you are doing it means that we're wasting time answering a question that is already answered on another forum. Many of us don't like that.
    OK, got it, and I apologize. I'll just stick with this forum (java-forums.org) from now on, as it provides email notification of responses.

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Open "Save Page As" Dialog Box
    By Anubha in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-12-2007, 09:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •