hakuna matata
hakuna matata

Reputation: 3327

JDialog to get input, instead get error

I wrote a class that extends JDialog, to be shown when I press save (save is a JMenuItem object). However, when I press save, I get the dialog with an error of having file name as null, and I didn't get the chance to type anything.

What am I doing wrong here? Any help would be appreciated.

Thanks.

Here is the code for my JDialog extended class:

package ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class SaveImageView extends JDialog {
    private final JPanel contentPanel = new JPanel ();
    private JTextField txtFilename;

    public int type; // 0 -> png, 1 -> jpg, 2 -> gif
    public String filename;

    public SaveImageView () {
        setTitle("Save Image");
        setBounds(100, 100, 450, 230);
        getContentPane().setLayout(null);
        contentPanel.setBounds(0, 0, 434, 229);
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel);
        contentPanel.setLayout(null);

        {
            JLabel lblFilename = new JLabel("Filename: ");
            lblFilename.setBounds(10, 95, 58, 20);
            contentPanel.add(lblFilename);
        }

        {
            txtFilename = new JTextField();
            txtFilename.setBounds(78, 95, 123, 20);
            contentPanel.add(txtFilename);
            txtFilename.setColumns(10);
        }

        {
            JRadioButton rdbtnGif = new JRadioButton("GIF");
            rdbtnGif.setBounds(123, 11, 43, 23);
            contentPanel.add(rdbtnGif);
            rdbtnGif.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    type = 2;
                }
            });
        }

        {
            JRadioButton rdbtnJpg = new JRadioButton("JPG");
            rdbtnJpg.setBounds(123, 37, 43, 23);
            contentPanel.add(rdbtnJpg);
            rdbtnJpg.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    type = 1;
                }
            });
        }

        {
            JRadioButton rdbtnPng = new JRadioButton("PNG");
            rdbtnPng.setBounds(123, 63, 45, 23);
            contentPanel.add(rdbtnPng);
            rdbtnPng.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    type = 0;
                }
            });
        }

        JLabel lblImageTypeTo = new JLabel("Image type to save: ");
        lblImageTypeTo.setBounds(10, 11, 144, 23);
        contentPanel.add(lblImageTypeTo);

        JLabel lblNote = new JLabel("NOTE: Images will be saved in \"output\" folder.");
        lblNote.setBounds(10, 132, 273, 22);
        contentPanel.add(lblNote);

        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBounds(0, 160, 434, 33);
            contentPanel.add(buttonPane);
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));

            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        filename = txtFilename.getText();
                    }
                });
            }

            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }
}

And this is in my main GUI class, this is when I add the ActionListener to the item:

//////////////////////////////////////////////////////////
//                  Save - Menu Item                    //
//////////////////////////////////////////////////////////
JMenuItem saveItem = new JMenuItem("Save");
saveItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        SaveImageView siv = new SaveImageView();
        siv.setVisible(true);
        String typeStr = (siv.type == 0) ? "png" : ((siv.type == 1) ? "jpg" : "gif");
        try {
            ImageIO.write(img, typeStr, new File("output/" + siv.filename + "." + typeStr));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
});
menu.add(saveItem);

This is the error I'm getting:

java.io.FileNotFoundException: output\null.png (The system cannot find the path specified)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
    at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
    at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at ui.PPMViewer$1.actionPerformed(PPMViewer.java:40)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.imageio.ImageIO.write(Unknown Source)
    at ui.PPMViewer$1.actionPerformed(PPMViewer.java:40)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Upvotes: 0

Views: 304

Answers (2)

eboix
eboix

Reputation: 5133

Looks like the code: filename = txtFilename.getText(); doesn't ever get called because you're not clicking the OK button before saving.

This is because your main GUI class does not pause after you have opened the JDialog, but keeps on running.

A way to stop this behavior is to set the window modality to true, or, if you want to avoid magic values, Dialog.ModalityType.DEFAULT_MODAL.

So in the constructor, add the line setModal(true).


Once the OK and Cancel buttons have been pressed, you will need to exit the JDialog. You can do this by adding the line:

dispose();

to your ActionListener code.

Upvotes: 1

makasprzak
makasprzak

Reputation: 5220

When you invoke the setVisible() method on your SaveImageView class, the dialog is shown, but the current thread goes further not waiting for your actions. The easiest way is to use JOptionPane.showInputDialog. It will block the thread execution.

Upvotes: 1

Related Questions