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 6238427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:12:43+00:00 2026-05-24T11:12:43+00:00

A typical way to use JFileChooser includes checking whether user clicked OK, like in

  • 0

A typical way to use JFileChooser includes checking whether user clicked OK, like in this code:

private void addModelButtonMouseClicked(java.awt.event.MouseEvent evt) {                                            
    JFileChooser modelChooser = new JFileChooser();
    if(modelChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ){
        File selectedFile = modelChooser.getSelectedFile();
        if(verifyModelFile(selectedFile)){
            MetModel newModel;
            newModel = parser.parse(selectedFile, editedCollection.getDirectory() );
            this.editedCollection.addModel(newModel);
            this.modelListUpdate();
        }
    }
}

I tried to mimic this behavior in my own window inheriting JFrame. I thought that this way of handling forms is more convenient than passing collection that is to be edited to the new form. But I have realized that if I want to have a method in my JFrame returning something like exit status of it I need to make it wait for user clicking OK or Cancel without freezing the form/dialog window.

So, how does showOpenDialog() work? When I tried to inspect the implementation, I found only one line methods with note “Compiled code”.

  • 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-05-24T11:12:45+00:00Added an answer on May 24, 2026 at 11:12 am

    I tried to mimic this behavior in my own window inheriting JFrame.

    JFrame is not a modal or ‘blocking’ component. Use a modal JDialog or JOptionPane instead.

    E.G.

    Blocking Chooser

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    /**  Typical output:
    [JTree, colors, violet]
    User cancelled
    [JTree, food, bananas]
    Press any key to continue . . .
    */
    class ConfirmDialog extends JDialog {
    
        public static final int OK_OPTION = 0;
        public static final int CANCEL_OPTION = 1;
    
        private int result = -1;
    
        JPanel content;
    
        public ConfirmDialog(Frame parent) {
            super(parent,true);
    
            JPanel gui = new JPanel(new BorderLayout(3,3));
            gui.setBorder(new EmptyBorder(5,5,5,5));
            content = new JPanel(new BorderLayout());
            gui.add(content, BorderLayout.CENTER);
            JPanel buttons = new JPanel(new FlowLayout(4));
            gui.add(buttons, BorderLayout.SOUTH);
    
            JButton ok = new JButton("OK");
            buttons.add(ok);
            ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae) {
                    result = OK_OPTION;
                    setVisible(false);
                }
            });
    
            JButton cancel = new JButton("Cancel");
            buttons.add(cancel);
            cancel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae) {
                    result = CANCEL_OPTION;
                    setVisible(false);
                }
            });
    
            setContentPane(gui);
        }
    
        public int showConfirmDialog(JComponent child, String title) {
            setTitle(title);
            content.removeAll();
            content.add(child, BorderLayout.CENTER);
            pack();
            setLocationRelativeTo(getParent());
    
            setVisible(true);
    
            return result;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame f = new JFrame("Test ConfirmDialog");
                    final ConfirmDialog dialog = new ConfirmDialog(f);
                    final JTree tree = new JTree();
                    tree.setVisibleRowCount(5);
                    final JScrollPane treeScroll = new JScrollPane(tree);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JButton b = new JButton("Choose Tree Item");
                    b.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent ae) {
                            int result = dialog.showConfirmDialog(
                                treeScroll, "Choose an item");
                            if (result==ConfirmDialog.OK_OPTION) {
                                System.out.println(tree.getSelectionPath());
                            } else {
                                System.out.println("User cancelled");
                            }
                        }
                    });
                    JPanel p = new JPanel(new BorderLayout());
                    p.add(b);
                    p.setBorder(new EmptyBorder(50,50,50,50));
                    f.setContentPane(p);
                    f.pack();
                    f.setLocationByPlatform(true);
                    f.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Typical way of creating a CSV string (pseudocode): Create a CSV container object (like
It is typical to have something like this in your cshrc file for setting
The typical way to define an integer constant to use inside a function is:
The typical way of implementing IUnknown::QueryInterface() is the following: use a chain of if-else-if
The typical way of selecting data is: select * from my_table But what if
During a typical day programming, I implement functions in a way that I would
The typical ConfigParser generated file looks like: [Section] bar=foo [Section 2] bar2= baz Now,
I'm learning socket programming (in python) and I was wondering what the best/typical way
I need a way to use the jquery .live() function to act on elements
This is no typical programming question. I am currently developing an app using the

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.