Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:17:22+00:00 2026-06-01T16:17:22+00:00

I need to choose image with file open dialog and then show it in

  • 0

I need to choose image with file open dialog and then show it in window. But When I choose image it is not shown in the window.

I’ve created class which create window with jmenubar and 1 jmenuitem. When I click on menuitem JfileChooser appears and then I choose some file. But then happens nothing.
I think the problem is in actionListener for JFileChooser(ImageFilter is a filter from docs java)

public Frame(){
    //create bars and window
    mainframe = new JFrame("Window");
    mainframe.setVisible(true);
    mainframe.setSize(300, 300);
    menubar = new JMenuBar();
    mainer = new JMenu("Menu");
    menubar.add(mainer);
    //create items
    item = new JMenuItem("Open",KeyEvent.VK_T);
    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    item.getAccessibleContext().setAccessibleDescription("open image");
    //action listener
    item.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //open file dialog
                    choser = new JFileChooser();
                    choser.addChoosableFileFilter(new ImageFilter());
                    final int returnval = choser.showOpenDialog(menubar);
                    //action listener for JFileChooser
                    choser.addActionListener(
                            new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    if (returnval == JFileChooser.APPROVE_OPTION){
                                        fc = choser.getSelectedFile();
                                        try{
                                            Panel panel = new ShowImage(fc.getName());
                                            mainframe.getContentPane().add(panel);
                                        }catch(Exception exc){};
                                    }
                                }
                            }
                    );
                }
            }
    );
    mainer.add(item);
    mainframe.setJMenuBar(menubar);
}

ShowImage class

class ShowImage extends Panel{
    BufferedImage image;
    public ShowImage(String imagename) throws IOException {
        File input = new File(imagename);
        image = ImageIO.read(input);
    }
    public void paint(Graphics g){
        g.drawImage(image,0,0,image.getWidth(),image.getHeight(),null);
    }
}

P.S another problem is that it shows nothing until I change size of the window.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:17:24+00:00Added an answer on June 1, 2026 at 4:17 pm

    Extend JPanel instead of Panel, and override paintComponent method, ie:

    class ShowImage extends JPanel{
        public void paintComponent(Graphics g){
            ...
        }
    }
    

    Also, there is no need to addActionListener on JFileChooser, just check the return value and act accordingly, ie:

    final int returnval = choser.showOpenDialog(menubar);
    if (returnval == JFileChooser.APPROVE_OPTION){
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have xpath page.search(//table[@class='campaign']//table) which returns two tables. I need to choose only first
In our COM project, we need to choose between best string class implementation so
I need some advice on which Swing Components to choose in order to achieve
I need to generate a report where the user can choose All Issues, Open
I'm trying to show an ajax preview of an image from a file input.
I'm developing a MVC application and I need to show an image in the
I start working on pdf render.I need to open the pdf file inside the
I have an xml file which I need to visualize in a pdf file.
I need to choose between either ImageMagick or GD library for the following image
In an Activity, I can choose an image from the Gallery, and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.