How does JFileChooser return an output value?

A typical way to use JFileChooser involves checking if the user clicked OK, as 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 reproduce this behavior in my own window, inheriting a JFrame . I thought this way of processing forms is more convenient than submitting a collection that needs to be edited in a new form. But I realized that if I want the method in my JFrame return something like the exit status from it, I need to make it wait for the "OK" or "Cancel" button to be pressed without freezing the form / dialog box.

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

+2
source share
2 answers

I tried to reproduce this behavior in my own window, inheriting a JFrame.

JFrame not a modal or "blocking" component. Use a modal JDialog or JOptionPane JDialog .

eg.

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); } }); } } 
+2
source

I think you are waiting for the user to press the button, constantly checking which button is pressed.

"I need to make him wait for the OK or Cancel button to click without freezing the form / dialog."

Maybe you should use evens to get a notification when a user clicks on something without waiting for him to click a button - maybe there is some OnWindowExit event?

Or maybe something like this:

  MyPanel panel = new MyPanel(...); int answer = JOptionPane.showConfirmDialog( parentComponent, panel, title, JOptionPane.YES_NO_CANCEL, JOptionPane.PLAIN_MESSAGE ); if (answer == JOptionPane.YES_OPTION) { // do stuff with the panel } 

Otherwise, you can see how to handle window events, especially windowClosing(WindowEvent) here

+1
source

Source: https://habr.com/ru/post/894756/


All Articles