Example usage for java.awt Component getSize

List of usage examples for java.awt Component getSize

Introduction

In this page you can find the example usage for java.awt Component getSize.

Prototype

public Dimension getSize() 

Source Link

Document

Returns the size of this component in the form of a Dimension object.

Usage

From source file:edu.ku.brc.af.core.NavBox.java

/**
 * Refreshes - meaning it makes sure it is resized (layed out) and drawn.
 * @param nbi the box to refresh/*  ww  w .  j a  v a2 s . c  o m*/
 */
public static void refresh(final NavBoxItemIFace nbi) {
    if (nbi != null) {
        Component comp = nbi.getUIComponent();
        comp.invalidate();
        comp.doLayout();
        comp.setSize(comp.getPreferredSize());
        comp.repaint();
        log.debug("comp " + comp.getPreferredSize() + " " + comp.getSize()); //$NON-NLS-1$ //$NON-NLS-2$

        Container parentComp = nbi.getUIComponent().getParent();
        if (parentComp instanceof NavBox) {
            refresh((NavBox) parentComp);
        } else if (parentComp instanceof JScrollPane) {
            // this must be a scrollable NavBox;
            // let's get the actual NavBox
            // container heirarchy is NavBox -> JScrollPane -> NavBoxItem
            parentComp = parentComp.getParent().getParent();
            refresh((NavBox) parentComp);
        }
    }
}

From source file:Main.java

public static Window showCentered(Component parent, Window window) {
    if (null == window)
        throw new IllegalArgumentException("window null");

    if (window instanceof Dialog) {
        Dialog dialog = (Dialog) window;

        boolean isResizable = dialog.isResizable();

        dialog.setResizable(true);/*from  w  w  w  .j a va2 s  . c om*/
        dialog.pack();
        dialog.setResizable(isResizable);
    } else {
        window.pack();
    }

    Dimension windowSize = window.getPreferredSize();

    int newX;
    int newY;

    if (null == parent) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        newX = (screen.width - windowSize.width) / 2;
        newY = (screen.height - windowSize.height) / 2;
    } else {
        Dimension parentSize = parent.getSize();
        Point loc = parent.getLocation();

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
        newY = (parentSize.height - windowSize.height) / 2 + loc.y;

        if (0 > newX)
            newX = 0;
        if (0 > newY)
            newY = 0;
    }

    window.setLocation(newX, newY);
    window.setVisible(true);

    return window;
}

From source file:Main.java

public static Window showBelow(Component parent, Window window, int horizontalAlignment) {
    final int DISTANCE = 2;

    if (null == parent)
        throw new IllegalArgumentException("parent null");
    if (null == window)
        throw new IllegalArgumentException("parent null");
    if (!((SwingConstants.LEADING == horizontalAlignment) || (SwingConstants.TRAILING == horizontalAlignment)
            || (SwingConstants.CENTER == horizontalAlignment)
            || (SwingConstants.SOUTH == horizontalAlignment))) {

        throw new IllegalArgumentException("Illegal horizontal alignment " + horizontalAlignment
                + " should be either SwingConstants.LEADING or "
                + "SwingConstants.TRAILING or SwingConstants.CENTER");
    }//www .  j  a  v  a  2  s . c om

    window.pack();

    Dimension windowSize = window.getPreferredSize();
    Dimension parentSize = parent.getSize();

    Point loc = parent.getLocationOnScreen();

    int newX;

    if ((SwingConstants.CENTER == horizontalAlignment) || (SwingConstants.SOUTH == horizontalAlignment)) {

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
    } else if (SwingConstants.TRAILING == horizontalAlignment) {
        newX = loc.x + parentSize.width - windowSize.width;
    } else {
        newX = loc.x;
    }

    window.setLocation(newX, (loc.y + parentSize.height + DISTANCE));

    window.setVisible(true);

    return window;
}

From source file:ComponentEventDemo.java

public void componentResized(ComponentEvent e) {
    Component c = e.getComponent();
    displayMessage("componentResized event from " + c.getClass().getName() + "; new size: " + c.getSize().width
            + ", " + c.getSize().height);
}

From source file:PopupDemo.java

void mouseAction(String which, MouseEvent e) {
    Component c = e.getComponent();
    System.out.println(which + "e=" + e + ", mods=" + getMods(e) + ", component=" + c);
    if (e.isPopupTrigger()) {
        System.out.println("isPopup");
        PopupMenu pm = getHash(c);
        pm.show(c, c.getSize().width / 2, c.getSize().height / 2);
    }/*from  w ww  . java  2  s  . c  o m*/
}

From source file:it.unibo.alchemist.boundary.gui.Perspective.java

private void setMainDisplay(final GraphicalOutputMonitor<T> gom) {
    if (!(gom instanceof Component)) {
        throw new IllegalArgumentException("Java Swing can not load a " + gom);
    }//from   w  w w . ja va  2s  .  c o  m
    final Component gomComp = (Component) gom;
    if (main != null) {
        sim.removeOutputMonitor(main);
        gom.setStep(main.getStep());
        gom.setRealTime(main.isRealTime());
        final Component mainComp = (Component) main;
        gomComp.setSize(mainComp.getSize());
        remove(mainComp);
    } else {
        gomComp.setSize(getSize());
    }
    main = gom;
    add(gomComp, BorderLayout.CENTER);
    revalidate();
    makeEffects();
    if (sim != null) {
        new Thread(() -> sim.addOutputMonitor(main)).start();
    }
}

From source file:edu.ku.brc.ui.UIRegistry.java

/**
 * Writes a string message into the BufferedImage on GlassPane and sets the main component's visibility to false and
 * shows the GlassPane.// w w  w .j  a  v a 2s  . co m
 * @param msg the message
 * @param pointSize the Font point size for the message to be writen in
 */
public static GhostGlassPane writeGlassPaneMsg(final String msg, final int pointSize) {
    GhostGlassPane glassPane = getGlassPane();
    if (glassPane != null) {
        glassPane.finishDnD();
    }

    glassPane.setMaskingEvents(true);

    Component mainComp = get(MAINPANE);
    if (mainComp != null && glassPane != null) {
        JFrame frame = (JFrame) get(FRAME);
        frameRect = frame.getBounds();

        int y = 0;
        JMenuBar menuBar = null;
        Dimension size = mainComp.getSize();
        if (UIHelper.getOSType() != UIHelper.OSTYPE.MacOSX) {
            menuBar = frame.getJMenuBar();
            size.height += menuBar.getSize().height;
            y += menuBar.getSize().height;
        }
        BufferedImage buffer = getGlassPaneBufferedImage(size.width, size.height);
        Graphics2D g2 = buffer.createGraphics();
        if (menuBar != null) {
            menuBar.paint(g2);
        }
        g2.translate(0, y);
        mainComp.paint(g2);
        g2.translate(0, -y);

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(new Color(255, 255, 255, 128));
        g2.fillRect(0, 0, size.width, size.height);

        g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize));
        FontMetrics fm = g2.getFontMetrics();

        int tw = fm.stringWidth(msg);
        int th = fm.getHeight();
        int tx = (size.width - tw) / 2;
        int ty = (size.height - th) / 2;

        int expand = 20;
        int arc = expand * 2;
        g2.setColor(Color.WHITE);
        g2.fillRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.DARK_GRAY);
        g2.drawRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.BLACK);
        g2.drawString(msg, tx, ty);
        g2.dispose();

        glassPane.setImage(buffer);
        glassPane.setPoint(new Point(0, 0), GhostGlassPane.ImagePaintMode.ABSOLUTE);
        glassPane.setOffset(new Point(0, 0));

        glassPane.setVisible(true);
        mainComp.setVisible(false);

        //Using paintImmediately fixes problems with glass pane not showing, such as for workbench saves initialed
        //during workbench or app shutdown. Don't know if there is a better way to fix it.
        //glassPane.repaint();
        glassPane.paintImmediately(glassPane.getBounds());
        showingGlassPane = true;
    }

    return glassPane;
}

From source file:com.biosis.biosislite.vistas.AsignarPermiso.java

private void imprimirBoletaViaje(AsignacionPermiso asignacion) {
    File reporte = new File("reportes/permiso_comision-servicios.jasper");

    List<AsignacionPermiso> listadoReporteAsignacion = new ArrayList();
    listadoReporteAsignacion.add(asignacion);

    Map<String, Object> parametros = new HashMap<>();
    parametros.put("reporte_ususario", UsuarioActivo.getUsuario().getLogin());

    Departamento area = asignacion.getEmpleado().getAreaEmpleadoList().get(0).getDepartamento();
    if (area != null) {
        parametros.put("area", area.getNombre());
    }//from   www .j a  v  a  2s .  co  m

    //        AsignarVacacion vac = new AsignarVacacion();
    long dias = this.fechasAlong(asignacion.getPermiso().getFechaInicio(),
            asignacion.getPermiso().getFechaFin());
    parametros.put("dias", dias);

    //Parametros del viaje
    Viaje viaje = vsc.buscarPorPermiso(asignacion.getPermiso());

    if (viaje != null) {
        parametros.put("alimentacion", viaje.getAlimentacion());
        parametros.put("alojamiento", viaje.getAlojamiento());
        parametros.put("movilidad", viaje.getMovilidad());
        parametros.put("combustible", viaje.getCombustible());
        parametros.put("dias_alimentacion", viaje.getDiasAlimentacion());
        parametros.put("dias_alojamiento", viaje.getDiasAlojamiento());
        parametros.put("dias_movilidad", viaje.getDiasMovLocal());
        //            if (asignacion.getEmpleado().getAreaEmpleadoList().get(0).getDepartamento().getDepartamento().getNombre() != null) {
        //                parametros.put("gerencia", asignacion.getEmpleado().getAreaEmpleadoList().get(0).getDepartamento().getDepartamento().getNombre());
        //            } else {
        parametros.put("gerencia",
                asignacion.getEmpleado().getAreaEmpleadoList().get(0).getGerencia().getNombre());
        //            }
        parametros.put("centro_costo", viaje.getCentroCosto());
        parametros.put("ubigeo", viaje.getDestino());
        parametros.put("cargo", asignacion.getEmpleado().getPuestoList().get(0).getPuesto().getNombre());

        double total = (viaje.getAlimentacion() * viaje.getDiasAlimentacion())
                + (viaje.getAlojamiento() * viaje.getDiasAlojamiento())
                + (viaje.getMovilidad() * viaje.getDiasMovLocal()) + viaje.getCombustible();

        parametros.put("total", total);
    }

    //Fin de parametros del viaje
    Component contentPane = reporteador.obtenerReporte(listadoReporteAsignacion, reporte, parametros);
    Frame principal = JOptionPane.getRootFrame();
    JDialog visor = new JDialog(principal, "Reporte", true);
    visor.setSize(contentPane.getSize());
    //        visor.setSize(ventana.getSize().width, ventana.getSize().height);
    visor.getContentPane().add(contentPane);
    visor.setLocationRelativeTo(this);

    //            visor.setUndecorated(true);
    visor.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
    visor.setVisible(true);
    visor.setAlwaysOnTop(true);
    //        reporteador.getReporte(reporte, parametros, listado) //        reporteador.setConn(controlador.getDao().getConexion());
    //        reporteador.generarReporte(reporte, parametros, JOptionPane.getFrameForComponent(this));
}

From source file:eu.crisis_economics.abm.dashboard.Page_Parameters.java

private Container initContainer() {
    JPanel containerPanel = new JPanel(new CardLayout());
    JLabel loadingModelLabel = new JLabel("Loading model...");
    loadingModelLabel.setHorizontalAlignment(SwingConstants.CENTER);
    containerPanel.add(loadingModelLabel, LOADING_MODEL_ID);
    tabbedPane = new JTabbedPane();
    containerPanel.add(tabbedPane, PARAMETERS_PANEL_ID);

    // create two number of turns field
    numberOfTurnsField = new JFormattedTextField();
    numberOfTurnsField.setFocusLostBehavior(JFormattedTextField.COMMIT);
    numberOfTurnsField.setInputVerifier(new InputVerifier() {

        @Override/* w  w w  . jav  a2s  . c o m*/
        public boolean verify(final JComponent input) {
            if (!checkNumberOfTurnsField(true)) {
                final PopupFactory popupFactory = PopupFactory.getSharedInstance();
                final Point locationOnScreen = numberOfTurnsField.getLocationOnScreen();
                final JLabel message = new JLabel("Please specify a (possibly floating point) number!");
                message.setBorder(new LineBorder(Color.RED, 2, true));
                if (errorPopup != null)
                    errorPopup.hide();
                errorPopup = popupFactory.getPopup(numberOfTurnsField, message, locationOnScreen.x - 10,
                        locationOnScreen.y - 30);

                errorPopup.show();

                return false;
            } else {
                if (errorPopup != null) {
                    errorPopup.hide();
                    errorPopup = null;
                }
                return true;
            }
        }
    });
    numberOfTurnsField.setText(String.valueOf(Dashboard.NUMBER_OF_TURNS));
    numberOfTurnsField
            .setToolTipText("The turn when the simulation should stop specified as an integer value.");
    numberOfTurnsField.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            wizard.clickDefaultButton();
        }
    });

    numberOfTurnsFieldPSW = new JFormattedTextField();
    numberOfTurnsFieldPSW.setFocusLostBehavior(JFormattedTextField.COMMIT);
    numberOfTurnsFieldPSW.setInputVerifier(new InputVerifier() {

        @Override
        public boolean verify(final JComponent input) {
            if (!checkNumberOfTurnsField(false)) {
                final PopupFactory popupFactory = PopupFactory.getSharedInstance();
                final Point locationOnScreen = numberOfTurnsFieldPSW.getLocationOnScreen();
                final JLabel message = new JLabel("Please specify a (possibly floating point) number!");
                message.setBorder(new LineBorder(Color.RED, 2, true));
                if (errorPopup != null)
                    errorPopup.hide();
                errorPopup = popupFactory.getPopup(numberOfTurnsFieldPSW, message, locationOnScreen.x - 10,
                        locationOnScreen.y - 30);

                errorPopup.show();

                return false;
            } else {
                if (errorPopup != null) {
                    errorPopup.hide();
                    errorPopup = null;
                }
                return true;
            }
        }
    });
    numberOfTurnsFieldPSW.setText(String.valueOf(Dashboard.NUMBER_OF_TURNS));
    numberOfTurnsFieldPSW
            .setToolTipText("The turn when the simulation should stop specified as an integer value.");
    numberOfTurnsFieldPSW.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            wizard.clickDefaultButton();
        }
    });

    // create two number of time-steps to ignore field
    numberTimestepsIgnored = new JFormattedTextField();
    numberTimestepsIgnored.setFocusLostBehavior(JFormattedTextField.COMMIT);
    numberTimestepsIgnored.setInputVerifier(new InputVerifier() {

        @Override
        public boolean verify(final JComponent input) {
            if (!checkNumberTimestepsIgnored(true)) {
                final PopupFactory popupFactory = PopupFactory.getSharedInstance();
                final Point locationOnScreen = numberTimestepsIgnored.getLocationOnScreen();
                final JLabel message = new JLabel("Please specify an integer number!");
                message.setBorder(new LineBorder(Color.RED, 2, true));
                if (errorPopup != null)
                    errorPopup.hide();
                errorPopup = popupFactory.getPopup(numberTimestepsIgnored, message, locationOnScreen.x - 10,
                        locationOnScreen.y - 30);

                errorPopup.show();

                return false;
            } else {
                if (errorPopup != null) {
                    errorPopup.hide();
                    errorPopup = null;
                }
                return true;
            }
        }
    });
    numberTimestepsIgnored.setText(String.valueOf(Dashboard.NUMBER_OF_TIMESTEPS_TO_IGNORE));
    numberTimestepsIgnored.setToolTipText(
            "The turn when the simulation should start charting specified as an integer value.");
    numberTimestepsIgnored.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            wizard.clickDefaultButton();
        }
    });

    numberTimestepsIgnoredPSW = new JFormattedTextField();
    numberTimestepsIgnoredPSW.setFocusLostBehavior(JFormattedTextField.COMMIT);
    numberTimestepsIgnoredPSW.setInputVerifier(new InputVerifier() {

        @Override
        public boolean verify(final JComponent input) {
            if (!checkNumberTimestepsIgnored(false)) {
                final PopupFactory popupFactory = PopupFactory.getSharedInstance();
                final Point locationOnScreen = numberTimestepsIgnoredPSW.getLocationOnScreen();
                final JLabel message = new JLabel("Please specify an integer number!");
                message.setBorder(new LineBorder(Color.RED, 2, true));
                if (errorPopup != null)
                    errorPopup.hide();
                errorPopup = popupFactory.getPopup(numberTimestepsIgnoredPSW, message, locationOnScreen.x - 10,
                        locationOnScreen.y - 30);

                errorPopup.show();

                return false;
            } else {
                if (errorPopup != null) {
                    errorPopup.hide();
                    errorPopup = null;
                }
                return true;
            }
        }
    });
    numberTimestepsIgnoredPSW.setText(String.valueOf(Dashboard.NUMBER_OF_TIMESTEPS_TO_IGNORE));
    numberTimestepsIgnoredPSW.setToolTipText(
            "The turn when the simulation should start charting specified as an integer value.");
    numberTimestepsIgnoredPSW.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            wizard.clickDefaultButton();
        }
    });

    onLineChartsCheckBox = new JCheckBox();
    //      onLineChartsCheckBoxPSW = new JCheckBox();
    advancedChartsCheckBox = new JCheckBox();
    advancedChartsCheckBox.setSelected(true);

    // create the scroll pane for the simple parameter setting page
    JLabel label = null;
    label = new JLabel(new ImageIcon(new ImageIcon(getClass().getResource(DECORATION_IMAGE)).getImage()
            .getScaledInstance(DECORATION_IMAGE_WIDTH, -1, Image.SCALE_SMOOTH)));
    Style.registerCssClasses(label, Dashboard.CSS_CLASS_COMMON_PANEL);
    parametersScrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    parametersScrollPane.setBorder(null);
    parametersScrollPane.setViewportBorder(null);

    breadcrumb = new Breadcrumb();
    Style.registerCssClasses(breadcrumb, CSS_ID_BREADCRUMB);

    singleRunParametersPanel = new ScrollableJPanel(new SizeProvider() {

        @Override
        public int getHeight() {
            Component component = tabbedPane.getSelectedComponent();
            if (component == null) {
                return 0;
            }
            JScrollBar scrollBar = parametersScrollPane.getHorizontalScrollBar();
            return component.getSize().height - breadcrumb.getHeight()
                    - (scrollBar.isVisible() ? scrollBar.getPreferredSize().height : 0);
        }

        @Override
        public int getWidth() {
            final int hScrollBarWidth = parametersScrollPane.getHorizontalScrollBar().getPreferredSize().width;
            final int width = dashboard.getSize().width - Page_Parameters.DECORATION_IMAGE_WIDTH
                    - hScrollBarWidth;
            return width;
        }
    });
    BoxLayout boxLayout = new BoxLayout(singleRunParametersPanel, BoxLayout.X_AXIS);
    singleRunParametersPanel.setLayout(boxLayout);
    parametersScrollPane.setViewportView(singleRunParametersPanel);

    JPanel breadcrumbPanel = new JPanel(new BorderLayout());
    breadcrumbPanel.add(breadcrumb, BorderLayout.NORTH);
    breadcrumbPanel.add(parametersScrollPane, BorderLayout.CENTER);

    final JPanel simpleForm = FormsUtils.build("p ~ p:g", "01 t:p", label, breadcrumbPanel).getPanel();
    Style.registerCssClasses(simpleForm, Dashboard.CSS_CLASS_COMMON_PANEL);

    tabbedPane.add("Single run", simpleForm);

    // create the form for the parameter sweep setting page
    tabbedPane.add("Parameter sweep", createParamsweepGUI());

    gaSearchHandler = new GASearchHandler(currentModelHandler);
    gaSearchPanel = new GASearchPanel(gaSearchHandler, this);
    tabbedPane.add("Genetic Algorithm Search", gaSearchPanel);

    return containerPanel;
}

From source file:op.tools.SYSTools.java

public static void centerOnParent(Component parent, Component child) {

    Dimension dimParent = parent.getSize();
    Dimension dimChild = child.getSize();

    //Dimension them = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    int newX = (dimParent.width - dimChild.width) / 2;
    int newY = (dimParent.height - dimChild.height) / 2;
    newX += parent.getX();/*from   www. j a v  a 2s  . c  o  m*/
    newY += parent.getY();
    child.setLocation(newX, newY);
}