Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jorphan.gui » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jorphan.gui;
   20   
   21   import java.awt.Component;
   22   import java.awt.Dimension;
   23   
   24   /**
   25    * This class is a Util for awt Component and could be used to place them in
   26    * center of an other.
   27    *
   28    * @version $Revision: 674365 $
   29    */
   30   public final class ComponentUtil {
   31       /**
   32        * Use this static method if you want to center and set its position
   33        * compared to the size of the current users screen size. Valid percent is
   34        * between +-(0-100) minus is treated as plus, bigger than 100 is always set
   35        * to 100.
   36        *
   37        * @param component
   38        *            the component you want to center and set size on
   39        * @param percentOfScreen
   40        *            the percent of the current screensize you want the component
   41        *            to be
   42        */
   43       public static void centerComponentInWindow(Component component, int percentOfScreen) {
   44           if (percentOfScreen < 0) {
   45               centerComponentInWindow(component, -percentOfScreen);
   46               return;
   47           }
   48           if (percentOfScreen > 100) {
   49               centerComponentInWindow(component, 100);
   50               return;
   51           }
   52           double percent = percentOfScreen / 100.d;
   53           Dimension dimension = component.getToolkit().getScreenSize();
   54           component.setSize((int) (dimension.getWidth() * percent), (int) (dimension.getHeight() * percent));
   55           centerComponentInWindow(component);
   56       }
   57   
   58       /**
   59        * Use this static method if you want to center a component in Window.
   60        *
   61        * @param component
   62        *            the component you want to center in window
   63        */
   64       public static void centerComponentInWindow(Component component) {
   65           Dimension dimension = component.getToolkit().getScreenSize();
   66   
   67           component.setLocation((int) ((dimension.getWidth() - component.getWidth()) / 2),
   68                   (int) ((dimension.getHeight() - component.getHeight()) / 2));
   69           component.validate();
   70           component.repaint();
   71       }
   72   
   73       /**
   74        * Use this static method if you want to center a component over another
   75        * component.
   76        *
   77        * @param parent
   78        *            the component you want to use to place it on
   79        * @param toBeCentered
   80        *            the component you want to center
   81        */
   82       public static void centerComponentInComponent(Component parent, Component toBeCentered) {
   83           toBeCentered.setLocation(parent.getX() + (parent.getWidth() - toBeCentered.getWidth()) / 2, parent.getY()
   84                   + (parent.getHeight() - toBeCentered.getHeight()) / 2);
   85   
   86           toBeCentered.validate();
   87           toBeCentered.repaint();
   88       }
   89   
   90       /**
   91        * Private constructor to prevent instantiation.
   92        */
   93       private ComponentUtil() {
   94       }
   95   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jorphan.gui » [javadoc | source]