/** * Show the given message in a dialog box or independent window, depending on whether the source * component is contained in a Frame or not. * * @param c The Controller that calls this method, or null if it is not called by a Controller. * (The Controller, if any, will be notified when the error message is cleared.) * @param message The message to display. */ public void setErrorMessage(Controller c, String message) { if (popup != null) clearErrorMessage(); if (message == null) return; errorSource = c; errorMessage = message; Component parent = source; while (parent != null && !(parent instanceof Frame)) parent = parent.getParent(); if (parent != null) popup = new Dialog((Frame) parent, "Error Message", true); // modal dialog else popup = new Frame("Error Message"); // independent window popup.setBackground(Color.white); popup.add(new MC(message), BorderLayout.CENTER); Panel buttonBar = new Panel(); buttonBar.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10)); Button OK = new Button(" OK "); OK.addActionListener(this); buttonBar.add(OK); popup.add(buttonBar, BorderLayout.SOUTH); popup.pack(); if (parent == null) popup.setLocation(100, 80); else popup.setLocation(parent.getLocation().x + 50, parent.getLocation().y + 30); popup.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { popup.dispose(); } }); popup.show(); // make the dialog visible. }
/** * We generally draw lines to/from the <I>center</I> of components; this method finds the center * of the argument's enclosing rectangle * * @return Point at the center of <CODE>c</CODE> * @param c The component whose center point we wish to determine */ protected Point getCenterLocation(Component c) { Point p1 = new Point(); Point p2 = new Point(); // start with the relative location... c.getLocation(p1); // get to the middle of the fractionsLabel Dimension d = c.getSize(); p1.x += d.width / 2; p1.y += d.height / 2; Component parent = c.getParent(); // System.err.println("parent=" + parent); while (parent != null) { parent.getLocation(p2); p1.x += p2.x; p1.y += p2.y; if (STOP.equals(parent.getName())) break; parent = parent.getParent(); } return p1; }
/** * Changes the frame's location to a more central screen location * * @param frame the frame to be moved * @param X how far right to move the frame * @param Y how far down to move the frame */ public static void changeFrameLocation(Component frame, int X, int Y) { Point location = frame.getLocation(); // the window's current location // move the window over and down a certain amount of pixels location.translate(X, Y); // set the location frame.setLocation(location); }
/** * Internal MDI frames have offsets where a popup menu should be shown (in JDK 1.2). This method * sums up iteratively all x and y offsets of all parent compontents until the top parent * component is reached. */ private void adjustOffsets(Component comp, Point offsetPoint) { if (comp != null) { Point compLocation = comp.getLocation(); offsetPoint.translate(compLocation.x, compLocation.y); adjustOffsets(comp.getParent(), offsetPoint); } }
public void componentMoved(ComponentEvent evt) { Component component = evt.getComponent(); Point point = component.getLocation(); if (point.y < 0) { component.setBounds(point.x, 0, component.getWidth(), component.getHeight()); } }
private Point getLocationInTabbedPanel(Component c, TabbedPanel tp) { Point l = SwingUtilities.convertPoint(c.getParent(), c.getLocation(), tp); Insets tpInsets = tp.getInsets(); l.x -= tpInsets.left; l.y -= tpInsets.top; return l; }
public void layoutContainer(Container target) { // System.out.println("Laying out container " + target); super.layoutContainer(target); if (free != null) { // what is free?? Point loc = free.getLocation(); Dimension sz = free.getSize(); // System.out.println("Laying out free component " + free); free.setBounds(loc.x, loc.y, sz.width, sz.height); } }
public static void center(Component c, Component rel) { if (rel != null) { c.setLocation( rel.getLocation().x + rel.getSize().width / 2 - c.getSize().width / 2, rel.getLocation().y + rel.getSize().height / 2 - c.getSize().height / 2); } else { c.setLocation( Toolkit.getDefaultToolkit().getScreenSize().width / 2 - c.getSize().width / 2, +Toolkit.getDefaultToolkit().getScreenSize().height / 2 - c.getSize().height / 2); } }
public void paint(Graphics g1) { Graphics2D g = (Graphics2D) g1; Component c; Point p; paintComponent(g); for (int i = 0; i < getComponentCount(); i++) { AffineTransform save = g.getTransform(); c = getComponent(i); p = c.getLocation(); g.translate((int) p.getX(), (int) p.getY()); c.paint(g); g.setTransform(save); } }
public static void ajustWidth(Component aChild, int aValue) { assert aChild.getParent().getLayout() instanceof MarginLayout; MarginLayout layout = (MarginLayout) aChild.getParent().getLayout(); MarginConstraints anchors = layout.getLayoutConstraints(aChild); int containerWidth = aChild.getParent().getWidth(); int childLeft = aChild.getLocation().x; if (anchors.getWidth() != null) { anchors.getWidth().setPlainValue(aValue, containerWidth); } else if (anchors.getLeft() != null && anchors.getRight() != null) { anchors.getRight().setPlainValue(containerWidth - childLeft - aValue, containerWidth); } aChild.getParent().revalidate(); aChild.getParent().repaint(); }
/** * Called by ScrollPane's internal observer of the scrollpane's adjustables. This is called * whenever a scroll position is changed in one of adjustables, whether it was modified externally * or from the native scrollbars themselves. */ public void setValue(Adjustable adj, int v) { Component c = getScrollChild(); if (c == null) { return; } Point p = c.getLocation(); switch (adj.getOrientation()) { case Adjustable.VERTICAL: setScrollPosition(-(p.x), v); break; case Adjustable.HORIZONTAL: setScrollPosition(v, -(p.y)); break; } }
public void show(Event e) { Component origin = (Component) e.target; WComponentPeer peer = (WComponentPeer) WToolkit.targetToPeer(origin); if (peer == null) { // A failure to map the peer should only happen for a // lightweight component, then find the actual native parent from // that component. The event coorinates are going to have to be // remapped as well. Component nativeOrigin = WToolkit.getNativeContainer(origin); e.target = nativeOrigin; // remove the event coordinates for (Component c = origin; c != nativeOrigin; c = c.getParent()) { Point p = c.getLocation(); e.x += p.x; e.y += p.y; } } _show(e); }
/** * Calculates the preferred size dimensions for the specified panel given the components in the * specified parent container. * * @see #minimumLayoutSize * @param target The component to be laid out. * @return A size deemed suitable for laying out the container. */ public Dimension preferredLayoutSize(Container target) { int count; Container parent; Component component; Point point; Dimension dimension; Insets insets; Dimension ret; synchronized (target.getTreeLock()) { count = target.getComponentCount(); if (0 == count) { // be the same size unless we have a parent ret = target.getSize(); parent = target.getParent(); if (null != parent) { insets = parent.getInsets(); ret = parent.getSize(); ret.setSize( ret.width - insets.left - insets.right, ret.height - insets.top - insets.bottom); } } else { ret = new Dimension(0, 0); for (int i = 0; i < count; i++) { component = target.getComponent(i); if (component.isVisible()) { point = component.getLocation(); dimension = component.getPreferredSize(); ret.width = Math.max(ret.width, point.x + dimension.width); ret.height = Math.max(ret.height, point.y + dimension.height); } } insets = target.getInsets(); ret.width += insets.left + insets.right; ret.height += insets.top + insets.bottom; } } return (ret); }