Example usage for javax.swing JFrame addComponentListener

List of usage examples for javax.swing JFrame addComponentListener

Introduction

In this page you can find the example usage for javax.swing JFrame addComponentListener.

Prototype

public synchronized void addComponentListener(ComponentListener l) 

Source Link

Document

Adds the specified component listener to receive component events from this component.

Usage

From source file:Main.java

public static void main() {

    ComponentListener listener = new ComponentAdapter() {
        public void componentShown(ComponentEvent evt) {
            Component c = (Component) evt.getSource();
            System.out.println("Component is now visible");
        }//from  ww  w.ja  v  a  2  s  .c o m

        public void componentHidden(ComponentEvent evt) {
            Component c = (Component) evt.getSource();
            System.out.println("Component is now hidden");
        }

        public void componentMoved(ComponentEvent evt) {
            Component c = (Component) evt.getSource();

            Point newLoc = c.getLocation();
            System.out.println("Get new location");
        }

        public void componentResized(ComponentEvent evt) {
            Component c = (Component) evt.getSource();

            Dimension newSize = c.getSize();
            System.out.println("Get new size");
        }
    };
    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    frame.addComponentListener(listener);
    frame.setVisible(true);
}

From source file:net.schweerelos.parrot.CombinedParrotApp.java

private void initGUI(Properties properties) {
    try {// w  ww.j  a  v a 2  s  . c  o m
        this.setTitle(APP_TITLE);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                for (MainViewComponent mainViewComponent : mainViews) {
                    if (mainViewComponent instanceof ParrotStateListener) {
                        ((ParrotStateListener) mainViewComponent).parrotExiting();
                    }
                }
            }
        });
        setSize(920, 690);

        getContentPane().setLayout(new BorderLayout());

        UserInterfaceManager uiManager = new UserInterfaceManager(properties);
        navigators = new ArrayList<NavigatorComponent>(4);

        // main view
        listView = uiManager.createMainViewComponent(Style.TABLE);
        graphView = uiManager.createMainViewComponent(Style.GRAPH);
        mainViews.add(listView);
        mainViews.add(graphView);

        final JTabbedPane mainPanel = new JTabbedPane();
        mainPanel.add(graphView.getTitle(), graphView.asJComponent());
        mainPanel.add(listView.getTitle(), listView.asJComponent());

        mainPanel.setSelectedIndex(0);
        activeMainView = graphView;
        mainPanel.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (e.getSource() != mainPanel) {
                    return;
                }
                int selectedIndex = mainPanel.getSelectedIndex();
                if (selectedIndex == 0) {
                    activeMainView = graphView;
                } else if (selectedIndex == 1) {
                    activeMainView = listView;
                } else {
                    Logger logger = Logger.getLogger(CombinedParrotApp.class);
                    logger.warn("unknown tab index selected: " + selectedIndex);
                }
            }
        });

        add(mainPanel, BorderLayout.CENTER);

        // navigators
        JToolBar navigatorsBar = new JToolBar(JToolBar.HORIZONTAL);
        navigatorsBar.setMargin(new Insets(0, 11, 0, 0));
        navigatorsBar.setFloatable(false);
        getContentPane().add(navigatorsBar, BorderLayout.PAGE_START);

        // timeline
        NavigatorComponent timelineNavigator = uiManager.createTimelineNavigationComponent();
        navigators.add(timelineNavigator);

        JFrame timelineFrame = new JFrame(timelineNavigator.getNavigatorName() + "  " + APP_TITLE);

        timelineFrame.getContentPane().add(timelineNavigator.asJComponent());
        timelineFrame.pack();
        Point preferredLocation = new Point(0, 0);
        preferredFrameLocations.put(timelineFrame, preferredLocation);

        if (timelineNavigator.hasShowHideListener()) {
            timelineFrame.addComponentListener(timelineNavigator.getShowHideListener());
        }
        timelineFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        JToggleButton timelineButton = setupNavigatorButton(timelineNavigator.getNavigatorName(),
                timelineNavigator.getAcceleratorKey(), timelineNavigator);
        navigatorsBar.add(timelineButton);

        // map      
        NavigatorComponent mapNavigator = uiManager.createMapNavigationComponent();
        navigators.add(mapNavigator);

        JFrame mapFrame = new JFrame(mapNavigator.getNavigatorName() + "  " + APP_TITLE);

        mapFrame.getContentPane().add(mapNavigator.asJComponent());
        mapFrame.pack();
        preferredLocation = new Point(0,
                Toolkit.getDefaultToolkit().getScreenSize().height - mapFrame.getHeight());
        preferredFrameLocations.put(mapFrame, preferredLocation);

        if (mapNavigator.hasShowHideListener()) {
            mapFrame.addComponentListener(mapNavigator.getShowHideListener());
        }
        mapFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        JToggleButton mapButton = setupNavigatorButton(mapNavigator.getNavigatorName(),
                mapNavigator.getAcceleratorKey(), mapNavigator);
        navigatorsBar.add(mapButton);

        // search      
        NavigatorComponent searchNavigator = uiManager.createSearchComponent();
        navigators.add(searchNavigator);

        JFrame searchFrame = new JFrame(searchNavigator.getNavigatorName() + "  " + APP_TITLE);

        searchFrame.getContentPane().add(searchNavigator.asJComponent());
        searchFrame.pack();
        preferredLocation = new Point(
                Toolkit.getDefaultToolkit().getScreenSize().width - searchFrame.getWidth(), 0);
        preferredFrameLocations.put(searchFrame, preferredLocation);

        if (searchNavigator.hasShowHideListener()) {
            searchFrame.addComponentListener(searchNavigator.getShowHideListener());
        }
        searchFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        JToggleButton searchButton = setupNavigatorButton(searchNavigator.getNavigatorName(),
                searchNavigator.getAcceleratorKey(), searchNavigator);
        navigatorsBar.add(searchButton);

        // connections
        NavigatorComponent chainNavigator = uiManager.createChainNavigationComponent();
        navigators.add(chainNavigator);

        if (chainNavigator instanceof PickListener) {
            for (MainViewComponent mainViewComponent : mainViews) {
                mainViewComponent.addPickListener((PickListener) chainNavigator);
            }
        }

        if (chainNavigator.hasShowHideListener()) {
            chainNavigator.asJComponent().addComponentListener(chainNavigator.getShowHideListener());
        }

        JToggleButton connectionsButton = setupNavigatorButton(chainNavigator.getNavigatorName(),
                chainNavigator.getAcceleratorKey(), chainNavigator);
        navigatorsBar.add(connectionsButton);

        add(chainNavigator.asJComponent(), BorderLayout.PAGE_END);
        chainNavigator.asJComponent().setVisible(false);
    } catch (RuntimeException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    } catch (UnknownStyleException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:com.tag.FramePreferences.java

public void install() {
    windowStateListener = new WindowStateListener() {

        public void windowStateChanged(WindowEvent e) {
            Object source = e.getSource();
            if (source instanceof JFrame) {
                JFrame frame = (JFrame) source;
                int extendedState = frame.getExtendedState();
                if (extendedState == JFrame.ICONIFIED)
                    return;

                Preferences prefs = getPreferences();
                prefs.putInt(KEY_EXTENDED_STATE, extendedState);
            }/*  ww  w .j  a va  2  s  .  co  m*/
        }

    };
    frame.addWindowStateListener(windowStateListener);

    componentListener = new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            Preferences prefs = getPreferences();
            Dimension size = frame.getSize();
            prefs.putInt(KEY_WIDTH, size.width);
            prefs.putInt(KEY_HEIGHT, size.height);
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            Preferences prefs = getPreferences();
            Point location = frame.getLocation();
            prefs.putInt(KEY_X, location.x);
            prefs.putInt(KEY_Y, location.y);
        }

    };
    frame.addComponentListener(componentListener);
}

From source file:net.sourceforge.pmd.util.designer.Designer.java

private void createRuleXML() {
    CreateXMLRulePanel rulePanel = new CreateXMLRulePanel(xpathQueryArea, codeEditorPane);
    JFrame xmlframe = new JFrame("Create XML Rule");
    xmlframe.setContentPane(rulePanel);//w w w . ja  v  a2  s  .c  o  m
    xmlframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    xmlframe.setSize(new Dimension(600, 700));
    xmlframe.addComponentListener(new java.awt.event.ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            JFrame tmp = (JFrame) e.getSource();
            if (tmp.getWidth() < 600 || tmp.getHeight() < 700) {
                tmp.setSize(600, 700);
            }
        }
    });
    int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
    int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    xmlframe.pack();
    xmlframe.setLocation((screenWidth - xmlframe.getWidth()) / 2, (screenHeight - xmlframe.getHeight()) / 2);
    xmlframe.setVisible(true);
}

From source file:org.fuin.kickstart4j.ErrorDialog.java

/**
 * {@inheritDoc}/*from  ww w.ja v a  2 s.  c  om*/
 */
public void run() {
    final ErrorPanel panel = new ErrorPanel();
    panel.setMessage(message);
    panel.setPreferredSize(new Dimension(600, 400));
    panel.setCloseListener(new ErrorPanel.CloseListener() {
        public void onClose(final ErrorPanel panel) {
            System.exit(exitCode);
        }
    });
    final JFrame frame = Utils4Swing.createShowAndPosition(
            new MessagesWrapper(Locale.getDefault()).getErrorMessageDialogTitle(), panel, false,
            new ScreenCenterPositioner());
    frame.addComponentListener(new ComponentAdapter() {
        public void componentHidden(final ComponentEvent e) {
            System.exit(exitCode);
        }
    });

}

From source file:org.objectstyle.cayenne.modeler.pref.ComponentGeometry.java

/**
 * Binds this preference object to synchronize its state with a given component,
 * allowing to specify an initial offset compared to the stored position.
 *//*w  ww .j  av  a  2 s . c o m*/
public void bind(final JFrame frame, int initialWidth, int initialHeight, int maxOffset) {

    updateSize(frame, initialWidth, initialHeight);
    updateLocation(frame, maxOffset);

    frame.addComponentListener(new ComponentAdapter() {

        public void componentResized(ComponentEvent e) {
            setWidth(new Integer(frame.getWidth()));
            setHeight(new Integer(frame.getHeight()));
        }

        public void componentMoved(ComponentEvent e) {
            setX(new Integer(frame.getX()));
            setY(new Integer(frame.getY()));
        }
    });
}