Example usage for javax.swing JInternalFrame addVetoableChangeListener

List of usage examples for javax.swing JInternalFrame addVetoableChangeListener

Introduction

In this page you can find the example usage for javax.swing JInternalFrame addVetoableChangeListener.

Prototype

public synchronized void addVetoableChangeListener(VetoableChangeListener listener) 

Source Link

Document

Adds a VetoableChangeListener to the listener list.

Usage

From source file:VetoableChangeListenerDemo.java

public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JDesktopPane desktop = new JDesktopPane();

    JInternalFrame palette = new JInternalFrame("Palette", true, true, true, true);

    palette.addVetoableChangeListener(new IconPolice());

    palette.setBounds(350, 150, 100, 100);
    desktop.add(palette, JDesktopPane.PALETTE_LAYER);
    palette.setVisible(true);// w  ww. j  a v a  2s.co m

    frame.add(desktop, BorderLayout.CENTER);
    frame.setSize(500, 300);
    frame.setVisible(true);
}

From source file:InternalFrameTest.java

/**
 * Creates an internal frame on the desktop.
 * @param c the component to display in the internal frame
 * @param t the title of the internal frame.
 *//* w  w  w  .j  a va2 s.co m*/
public void createInternalFrame(Component c, String t) {
    final JInternalFrame iframe = new JInternalFrame(t, true, // resizable
            true, // closable
            true, // maximizable
            true); // iconifiable

    iframe.add(c, BorderLayout.CENTER);
    desktop.add(iframe);

    iframe.setFrameIcon(new ImageIcon("document.gif"));

    // add listener to confirm frame closing
    iframe.addVetoableChangeListener(new VetoableChangeListener() {
        public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
            String name = event.getPropertyName();
            Object value = event.getNewValue();

            // we only want to check attempts to close a frame
            if (name.equals("closed") && value.equals(true)) {
                // ask user if it is ok to close
                int result = JOptionPane.showInternalConfirmDialog(iframe, "OK to close?", "Select an Option",
                        JOptionPane.YES_NO_OPTION);

                // if the user doesn't agree, veto the close
                if (result != JOptionPane.YES_OPTION)
                    throw new PropertyVetoException("User canceled close", event);
            }
        }
    });

    // position frame
    int width = desktop.getWidth() / 2;
    int height = desktop.getHeight() / 2;
    iframe.reshape(nextFrameX, nextFrameY, width, height);

    iframe.show();

    // select the frame--might be vetoed
    try {
        iframe.setSelected(true);
    } catch (PropertyVetoException e) {
    }

    frameDistance = iframe.getHeight() - iframe.getContentPane().getHeight();

    // compute placement for next frame

    nextFrameX += frameDistance;
    nextFrameY += frameDistance;
    if (nextFrameX + width > desktop.getWidth())
        nextFrameX = 0;
    if (nextFrameY + height > desktop.getHeight())
        nextFrameY = 0;
}