Example usage for java.awt.event ComponentAdapter ComponentAdapter

List of usage examples for java.awt.event ComponentAdapter ComponentAdapter

Introduction

In this page you can find the example usage for java.awt.event ComponentAdapter ComponentAdapter.

Prototype

ComponentAdapter

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setMaximumSize(new Dimension(350, 250));
    frame.addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent evt) {
            Dimension size = frame.getSize();
            Dimension max = frame.getMaximumSize();
            if (size.getWidth() > max.getWidth()) {
                frame.setSize((int) max.getWidth(), (int) size.getHeight());
            }/*from w w w. ja va2  s  .c o  m*/
            if (size.getHeight() > max.getHeight()) {
                frame.setSize((int) size.getWidth(), (int) max.getHeight());
            }
        }
    });
    frame.setSize(200, 100);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setMinimumSize(new Dimension(200, 200));
    frame.addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent evt) {
            Dimension size = frame.getSize();
            Dimension min = frame.getMinimumSize();
            if (size.getWidth() < min.getWidth()) {
                frame.setSize((int) min.getWidth(), (int) size.getHeight());
            }/*ww w  .j a v a2 s .  c o  m*/
            if (size.getHeight() < min.getHeight()) {
                frame.setSize((int) size.getWidth(), (int) min.getHeight());
            }
        }
    });
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDialog dialog = new JDialog(frame, false);
    dialog.setSize(200, 50);/* www.  j  ava 2  s .  c  om*/
    frame.addComponentListener(new ComponentAdapter() {
        Point lastLocation;

        @Override
        public void componentMoved(ComponentEvent e) {
            if (lastLocation == null && frame.isVisible()) {
                lastLocation = frame.getLocation();
            } else {
                Point newLocation = frame.getLocation();
                int dx = newLocation.x - lastLocation.x;
                int dy = newLocation.y - lastLocation.y;
                dialog.setLocation(dialog.getX() + dx, dialog.getY() + dy);
                lastLocation = newLocation;
            }
        }
    });
    frame.setSize(400, 200);
    frame.setVisible(true);
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel() {
        @Override/*w  ww.  j  a va 2s. c o m*/
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    };
    panel.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            System.out.println("Resized to " + e.getComponent().getSize());
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            System.out.println("Moved to " + e.getComponent().getLocation());
        }
    });
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("test", panel);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(tabbedPane);
    frame.pack();

    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
    statusBar.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 4, 4, 4)));
    final JLabel status = new JLabel();
    statusBar.add(status);/*from  w w w  .  j a va  2  s  .c om*/

    JLabel content = new JLabel("Content in the middle");
    content.setHorizontalAlignment(JLabel.CENTER);

    final JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(content);
    frame.add(statusBar, BorderLayout.SOUTH);

    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            status.setText(frame.getWidth() + "x" + frame.getHeight());
        }
    });

    frame.setBounds(20, 20, 200, 200);
    frame.setVisible(true);
}

From source file:edu.gmu.isa681.client.Main.java

public static void main(String[] args) {
    log.info("Setting look and feel...");

    try {//from  w ww .  j a v  a 2s .  c om
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }

    } catch (Exception ex1) {
        log.warn(ex1.getMessage(), ex1);
        log.warn("Nimbus is not available.");
        log.warn("Switching to system look and feel");
        log.warn("Some GUI discrepancies may occur!");

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex2) {
            log.error(ex2.getMessage(), ex2);
            log.error("Could not setup a look and feel.");
            System.exit(1);
        }
    }

    log.info("Initializing GUI...");

    final JFrame frame = new JFrame();
    frame.setTitle("GoForward");
    frame.setBackground(new Color(0, 100, 0));
    UIManager.put("nimbusBase", new Color(0, 100, 0));
    //UIManager.put("nimbusBlueGrey", new Color(0, 100, 0));
    UIManager.put("control", new Color(0, 100, 0));

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            frame.setPreferredSize(frame.getSize());
        }
    });

    Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    if (dim.width < 1366) {
        frame.setPreferredSize(new Dimension(800, 600));
    } else {
        frame.setPreferredSize(new Dimension(1200, 700));
    }

    //frame.setResizable(false);
    frame.setLocationByPlatform(true);
    frame.pack();

    Client client = new Client("localhost", Constants.SERVER_PORT);
    Controller controller = new Controller(client, frame);
    controller.applicationStarted();

    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    log.info("Started");
}

From source file:fll.subjective.SubjectiveFrame.java

public static void main(final String[] args) {
    LogUtils.initializeLogging();// w  ww  .  j  ava  2s  .c om

    Thread.setDefaultUncaughtExceptionHandler(new GuiExceptionHandler());

    // Use cross platform look and feel so that things look right all of the
    // time
    try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (final ClassNotFoundException e) {
        LOGGER.warn("Could not find cross platform look and feel class", e);
    } catch (final InstantiationException e) {
        LOGGER.warn("Could not instantiate cross platform look and feel class", e);
    } catch (final IllegalAccessException e) {
        LOGGER.warn("Error loading cross platform look and feel", e);
    } catch (final UnsupportedLookAndFeelException e) {
        LOGGER.warn("Cross platform look and feel unsupported?", e);
    }

    try {
        final SubjectiveFrame frame = new SubjectiveFrame();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            @SuppressFBWarnings(value = { "DM_EXIT" }, justification = "Exiting from main is OK")
            public void windowClosing(final WindowEvent e) {
                System.exit(0);
            }

            @Override
            @SuppressFBWarnings(value = { "DM_EXIT" }, justification = "Exiting from main is OK")
            public void windowClosed(final WindowEvent e) {
                System.exit(0);
            }
        });
        // should be able to watch for window closing, but hidden works
        frame.addComponentListener(new ComponentAdapter() {
            @Override
            @SuppressFBWarnings(value = { "DM_EXIT" }, justification = "Exiting from main is OK")
            public void componentHidden(final ComponentEvent e) {
                System.exit(0);
            }
        });
        GraphicsUtils.centerWindow(frame);
        frame.setVisible(true);
        frame.promptForFile();

    } catch (final Throwable e) {
        JOptionPane.showMessageDialog(null, "Unexpected error: " + e.getMessage(), "Error",
                JOptionPane.ERROR_MESSAGE);
        LOGGER.fatal("Unexpected error", e);
        System.exit(1);
    }
}

From source file:fll.scheduler.SchedulerUI.java

public static void main(final String[] args) {
    LogUtils.initializeLogging();/*from  ww  w. ja  v  a 2 s  . com*/

    Thread.setDefaultUncaughtExceptionHandler(new GuiExceptionHandler());

    // Use cross platform look and feel so that things look right all of the
    // time
    try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (final ClassNotFoundException e) {
        LOGGER.warn("Could not find cross platform look and feel class", e);
    } catch (final InstantiationException e) {
        LOGGER.warn("Could not instantiate cross platform look and feel class", e);
    } catch (final IllegalAccessException e) {
        LOGGER.warn("Error loading cross platform look and feel", e);
    } catch (final UnsupportedLookAndFeelException e) {
        LOGGER.warn("Cross platform look and feel unsupported?", e);
    }

    try {
        final SchedulerUI frame = new SchedulerUI();

        frame.addWindowListener(new WindowAdapter() {
            @Override
            @SuppressFBWarnings(value = { "DM_EXIT" }, justification = "Exiting from main is OK")
            public void windowClosing(final WindowEvent e) {
                System.exit(0);
            }

            @Override
            @SuppressFBWarnings(value = { "DM_EXIT" }, justification = "Exiting from main is OK")
            public void windowClosed(final WindowEvent e) {
                System.exit(0);
            }
        });
        // should be able to watch for window closing, but hidden works
        frame.addComponentListener(new ComponentAdapter() {
            @Override
            @SuppressFBWarnings(value = { "DM_EXIT" }, justification = "Exiting from main is OK")
            public void componentHidden(final ComponentEvent e) {
                System.exit(0);
            }
        });

        GraphicsUtils.centerWindow(frame);

        frame.setVisible(true);
    } catch (final Exception e) {
        LOGGER.fatal("Unexpected error", e);
        JOptionPane.showMessageDialog(null,
                "An unexpected error occurred. Please send the log file and a description of what you were doing to the developer. Error message: "
                        + e.getMessage(),
                "Error", JOptionPane.ERROR_MESSAGE);
    }
}

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");
        }/* www  . j av  a  2  s .co 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:Main.java

public static JSplitPane setDividerLocation(final JSplitPane splitter, final double proportion) {
    if (splitter.isShowing()) {
        if (splitter.getWidth() > 0 && splitter.getHeight() > 0) {
            splitter.setDividerLocation(proportion);
        } else {//w w w .  j ava  2  s  .  com
            splitter.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentResized(ComponentEvent ce) {
                    splitter.removeComponentListener(this);
                    setDividerLocation(splitter, proportion);
                }
            });
        }
    } else {
        splitter.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && splitter.isShowing()) {
                    splitter.removeHierarchyListener(this);
                    setDividerLocation(splitter, proportion);
                }
            }
        });
    }
    return splitter;
}