JMenuItem mntmLoad = new JMenuItem("Load");
        mntmLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser chooser = new JFileChooser();
                chooser.setMultiSelectionEnabled(true);
                int option = chooser.showOpenDialog(testgui.this);
                if (option == JFileChooser.APPROVE_OPTION) {
                  File[] sf = chooser.getSelectedFiles();
                  String filelist = "nothing";
                  if (sf.length > 0) filelist = sf[0].getName();
                  for (int i = 1; i < sf.length; i++) {
                    filelist += ", " + sf[i].getName();
                  }
                  statusbar.setText("You chose " + filelist);
                }
                else {
                  statusbar.setText("You canceled.");
                }
                }
        });
        mnFile.add(mntmLoad);

the compiler show error on this line(The method showOpenDialog(Component) in the type JFileChooser is not applicable for the arguments (testgui))

int option = chooser.showOpenDialog(testgui.this);

any idea?

Dani AI

Generated

Quick takeaway: the file-chooser call needs an actual Swing/AWT component as its "owner" window. Inside an anonymous ActionListener the expression that looks like "OuterClass.this" refers to the enclosing instance — but if that enclosing class does not extend a Component (for example, JFrame or JDialog) the compiler will complain. was pointing at that same root cause, and later confirmed the mistake was found.

How to fix it:

  • Pass a real window instance as the parent so the chooser can be modal and centered on your window (for example, the JFrame that holds your UI).
  • If you do not have a window to attach to, pass null; the chooser will still open but it will not be tied to your application window.
  • If you are inside an inner class and need the outer frame, refer to the outer instance (or keep a final reference to the frame) so you supply the correct object type.

Extra practical notes:

  • When you allow multi-selection, always check the returned array length before processing to avoid index errors. Use a StringBuilder or join logic rather than repeated string concatenation when building a display string for many files.
  • Make sure all Swing GUI work happens on the Event Dispatch Thread so dialogs and updates behave predictably.
  • Prefer meaningful class names and have your window class extend a Swing top-level container (JFrame/JDialog) when it represents an application window.

If the problem persists, include the class declaration line (the testgui class signature or the field that holds your frame) so it’s clear whether that type is actually a Component or not.

Recommended Answers

All 2 Replies

You didn't post the declaration of testgui, but apparently it's not a subclass of Component. The ".this" is even more mysterious.

ok, thx. i found my mistake already

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.