/** 开始跟随元素 */ public void following() { if (!listening) { listening = true; followTo.addComponentListener( new ComponentAdapter() { public void componentResized(ComponentEvent evt) { followTo(); } @Override public void componentMoved(ComponentEvent e) { followTo(); } private void followTo() { if (leftPad >= 0) { self.setLocation(followTo.getX() + followTo.getWidth() + leftPad, self.getY()); } if (topPad >= 0) { self.setLocation(self.getX(), followTo.getY() + followTo.getHeight() + topPad); } if (rightPad >= 0) { self.setLocation(followTo.getX() - self.getWidth() - rightPad, self.getY()); } if (bottomPad >= 0) { self.setLocation(self.getX(), followTo.getY() - self.getHeight() - bottomPad); } } }); } }
/** * Handles this components ancestor being added to a container. Registers this component as a * listener for size changes on the ancestor so that we may un-cache the prefereed size and force * a recalculation. * * @param event The heirarchy event. */ public void hierarchyChanged(final HierarchyEvent event) { if (0 != (event.getChangeFlags() & HierarchyEvent.PARENT_CHANGED)) { Component dad = event.getChanged(); Component parent = getParent(); if ((null != parent) && (parent.getParent() == dad)) dad.addComponentListener(this); } }
public GradientBackground(Component canvas) { super("Background"); this.canvas = canvas; canvas.addComponentListener( new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { super.componentResized(e); update(); } }); int width = Math.max(canvas.getWidth(), 10); int height = Math.max(canvas.getHeight(), 10); backgroundQuad = new Quad("BackgroundQuad", width, height); backgroundQuad.getSceneHints().setLightCombineMode(LightCombineMode.Off); backgroundQuad.getSceneHints().setOrthoOrder(1); update(); attachChild(backgroundQuad); final ZBufferState zstate = new ZBufferState(); zstate.setWritable(false); zstate.setEnabled(false); setRenderState(zstate); getSceneHints().setRenderBucketType(RenderBucketType.Skip); }
public void setTarget(Component paramComponent, Insets paramInsets) { if (this.Target != null) { this.Target.removeComponentListener(this); if (this.TargetParent != null) { this.TargetParent.removeContainerListener(this); this.TargetParent = null; } } if (paramInsets == null) this.Space = DEFAULTINSETS; else this.Space = paramInsets; this.Target = paramComponent; setLocation( paramComponent.getLocation().x - this.Space.left, paramComponent.getLocation().y - this.Space.top); setSize( paramComponent.getSize().width + this.Space.left + this.Space.right, paramComponent.getSize().height + this.Space.top + this.Space.bottom); paramComponent.addComponentListener(this); if (this.Target.getParent() != null) { this.TargetParent = this.Target.getParent(); this.TargetParent.addContainerListener(this); } }
/** Checks conditions and adds resize handler if they are met. */ private void checkAndAddResizeHandler() { Component parent = getParent(); if (parent != null && resizable && resizeHandler == null) { resizeHandler = new ResizeHandler(); parent.addComponentListener(resizeHandler); } }
private void wireLabelToComponent(final Component comp, final JLabel label) { comp.addComponentListener( new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { label.setText(comp.getWidth() + ":" + comp.getHeight()); } }); }
/** * Initializes this window. * * @param window the component which represents the window. This component will be used when * calling methods like {@link #setWindowBounds(Rectangle, boolean)}. It is the root of this * whole window. * @param contentParent the container which will be used as parent for the contents of this * window. This method will change the {@link LayoutManager} and add a child to <code> * contentParent</code>. This component can be the same as <code>window</code>. * @param borderAllowed If <code>true</code> and if {@link WindowConfiguration#isResizeable()}, * then a new border is installed for the {@link #getDisplayerParent() displayer parent}, and * some {@link MouseListener}s are installed. When the mouse is over the border it will change * the cursor and the user can resize or move the window. If <code>false</code> nothing * happens and the resizing system has to be implemented by the subclass. */ protected void init( Component window, Container contentParent, WindowConfiguration configuration, boolean borderAllowed) { if (window == null) throw new IllegalArgumentException("window must not be null"); if (contentParent == null) throw new IllegalArgumentException("contentParent must not be null"); this.window = window; content = createContent(configuration); content.setController(getController()); if (configuration.isResizeable()) { contentParent.setLayout(new GridLayout(1, 1)); } else { contentParent.setLayout(new ResizingLayoutManager(this, window)); } contentParent.add(content); Container parent = getDisplayerParent(); parent.setLayout(new GridLayout(1, 1)); if (configuration.isResizeable() && borderAllowed) { if (parent instanceof JComponent && configuration.getBorderFactory() != null) { border = configuration.getBorderFactory().create(this, (JComponent) parent); border.setController(getController()); borderModifier = new WindowBorder((JComponent) parent); borderModifier.setBorder(border); borderModifier.setController(getController()); ((JComponent) parent).setBorder(border); } Listener listener = new Listener(); parent.addMouseListener(listener); parent.addMouseMotionListener(listener); parent.addComponentListener(listener); } window.addComponentListener( new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { fireShapeChanged(); } @Override public void componentMoved(ComponentEvent e) { fireShapeChanged(); } }); addScreenDockWindowListener(windowListener); }
@Override public Component getListCellRendererComponent( final JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (padre == null) { padre = list.getParent(); padre.addComponentListener( new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { list.setCellRenderer(new CeldaListaRendererEntropy()); list.repaint(); } }); } if (isSelected) { txaCelda.setBackground(LookAndFeelEntropy.COLOR_SELECCION_ITEM); } else { txaCelda.setBackground(Color.WHITE); } String strMostrar = value == null ? "" : value.toString(); int intAnchoActual = list.getWidth(); if (intAnchoActual != 0) { int intMaxCaracteres = 100; strMostrar = strMostrar.substring( 0, strMostrar.length() <= intMaxCaracteres ? strMostrar.length() : intMaxCaracteres); if (strMostrar.length() == intMaxCaracteres) { strMostrar += "..."; } int intAnchoDeseado = 0; int intCantidadLineas = 1; for (char c : strMostrar.toCharArray()) { intAnchoDeseado += txaCelda.getFontMetrics(txaCelda.getFont()).charWidth(c); if (intAnchoDeseado > intAnchoActual * intCantidadLineas) { intCantidadLineas++; } } txaCelda.setRows(intCantidadLineas); } txaCelda.setText(strMostrar); return txaCelda; }
void installListeners(Component component) { for (ComponentListener listener : componentListeners) component.addComponentListener(listener); for (KeyListener listener : keyListeners) component.addKeyListener(listener); for (MouseListener listener : mouseListeners) component.addMouseListener(listener); for (MouseMotionListener listener : mouseMotionListeners) component.addMouseMotionListener(listener); if (component instanceof Container) { Container container = (Container) component; container.addContainerListener(containerListener); for (int iComp = container.getComponentCount(); iComp-- != 0; ) { installListeners(container.getComponent(iComp)); } } }
public OverlayPopupPanel(JLayeredPane layeredPane, Component childPanel) { this.layeredPane = layeredPane; this.childPanel = childPanel; if (childPanel != null) { setLayout(new BorderLayout()); add(childPanel, BorderLayout.CENTER); // Match visiblity of the child childPanel.addComponentListener( new ComponentListener() { @Override public void componentHidden(ComponentEvent e) { setVisible(false); } @Override public void componentMoved(ComponentEvent e) {} @Override public void componentResized(ComponentEvent e) {} @Override public void componentShown(ComponentEvent e) { setVisible(true); } }); // Should hide if the child is hidden if (!childPanel.isVisible()) { setVisible(false); } } layeredPane.add(this, JLayeredPane.MODAL_LAYER); layeredPane.addComponentListener(this); resize(); }
@Override protected void listeningStarted() { handler = new Handler(); comp.addComponentListener(handler); }
/** * Subclasses should call this after having constructed their GUI. Then this method will attach a * copy of the main menu from <code>root.menuFactory</code> and restore bounds from preferences. * * @param root application root * @see MenuFactory#gimmeSomethingReal( AppWindow ) */ public void init() { if (initialized) throw new IllegalStateException("Window was already initialized."); // System.out.println( "init " + getClass().getName() ); if (borrowMenuBar) { borrowMenuBar(wh.getMenuBarBorrower()); wh.addBorrowListener(this); } else if (ownMenuBar) { setJMenuBar(wh.getMenuBarRoot().createBar(this)); } // AbstractApplication.getApplication().addComponent( getClass().getName(), this ); winListener = new AbstractWindow.Adapter() { public void windowOpened(AbstractWindow.Event e) { // System.err.println( "shown" ); if (classPrefs != null) classPrefs.putBoolean(KEY_VISIBLE, true); if (!initialized) System.err.println("WARNING: window not initialized (" + e.getWindow() + ")"); } // public void windowClosing( WindowEvent e ) // { // classPrefs.putBoolean( PrefsUtil.KEY_VISIBLE, false ); // } public void windowClosed(AbstractWindow.Event e) { // System.err.println( "hidden" ); if (classPrefs != null) classPrefs.putBoolean(KEY_VISIBLE, false); } public void windowActivated(AbstractWindow.Event e) { try { active = true; if (wh.usesInternalFrames() && ownMenuBar) { wh.getMasterFrame().setJMenuBar(bar); } else if (borrowMenuBar && (barBorrower != null)) { barBorrower.setJMenuBar(null); if (jf != null) { jf.setJMenuBar(bar); } else if (jif != null) { wh.getMasterFrame().setJMenuBar(bar); } else { throw new IllegalStateException(); } } if (tempFloating) { if (jif == null) { // System.out.println( "activ " + enc_getClass().getName() ); //// wh.removeWindow( AbstractWindow.this, null ); tempFloatingTimer.restart(); // GUIUtil.setAlwaysOnTop( getWindow(), true ); //// floating = true; //// wh.addWindow( AbstractWindow.this, null ); } else { jif.setLayer(JLayeredPane.MODAL_LAYER); } // } else if( wh.usesFloating() ) { // // tricky... // // we need to do this because if the opposite's // // window is tempFloating, it will reset // // alwaysOnTop to false too late for the OS, // // so this one is not jumping to the front // // automatically upon activation... // toFront(); } } // seems to be a bug ... ! catch (NullPointerException e1) { e1.printStackTrace(); } } public void windowDeactivated(AbstractWindow.Event e) { // System.out.println( "deac2 " + enc_getClass().getName() ); try { active = false; if (wh.usesInternalFrames() && ownMenuBar) { if (wh.getMasterFrame().getJMenuBar() == bar) wh.getMasterFrame().setJMenuBar(null); } else if (borrowMenuBar && (barBorrower != null)) { if (jf != null) { jf.setJMenuBar(null); } barBorrower.setJMenuBar(bar); } if (tempFloating) { if (jif == null) { // System.out.println( "deact " + enc_getClass().getName() ); // wh.removeWindow( AbstractWindow.this, null ); GUIUtil.setAlwaysOnTop(getWindow(), false); tempFloatingTimer.stop(); // floating = false; // wh.addWindow( AbstractWindow.this, null ); // find the new active window (is valid only after // the next event cycle) and re-put it in the front\ // coz setAlwaysOnTop came "too late" EventQueue.invokeLater( new Runnable() { public void run() { final AbstractWindow fw = FloatingPaletteHandler.getInstance().getFocussedWindow(); if (fw != null) fw.toFront(); } }); } else { jif.setLayer(JLayeredPane.DEFAULT_LAYER); } } } // seems to be a bug ... ! catch (NullPointerException e1) { e1.printStackTrace(); } } }; addListener(winListener); if (autoUpdatePrefs()) { getClassPrefs(); // this creates the prefs restoreFromPrefs(); cmpListener = new ComponentAdapter() { public void componentResized(ComponentEvent e) { classPrefs.put(KEY_SIZE, dimensionToString(e.getComponent().getSize())); } public void componentMoved(ComponentEvent e) { classPrefs.put(KEY_LOCATION, pointToString(e.getComponent().getLocation())); } public void componentShown(ComponentEvent e) { classPrefs.putBoolean(KEY_VISIBLE, true); } public void componentHidden(ComponentEvent e) { classPrefs.putBoolean(KEY_VISIBLE, false); // System.err.println( "hidden" ); } }; c.addComponentListener(cmpListener); } else { if (alwaysPackSize()) { pack(); } } wh.addWindow(this, null); initialized = true; }
/** * @see JPanel#addComponentListener(ComponentListener) * @param componentListener component listener */ public void addComponentListener(ComponentListener componentListener) { canvas.addComponentListener(componentListener); }
/** Creates new form EditContactPanel */ public EditContactPanel() { initComponents(); // if not Substance LaF, add clipboard popup menu to text components if (!config.getLookAndFeel().equals(ThemeManager.LAF.SUBSTANCE)) { ClipboardPopupMenu.register(nameTextField); ClipboardPopupMenu.register(numberTextField); } // listen for changes in number and guess gateway numberTextField .getDocument() .addDocumentListener( new AbstractDocumentListener() { @Override public void onUpdate(DocumentEvent e) { boolean usrSet = userSet; if (!userSet) { gatewayComboBox.selectSuggestedGateway(numberTextField.getText()); } gatewayComboBox.setFilter(numberTextField.getText()); userSet = usrSet; updateCountryInfoLabel(); updateSuggestGatewayButton(); EditContactPanel.this.revalidate(); } }); // on keyring update update credentialsInfoLabel keyring.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateCredentialsInfoLabel(); EditContactPanel.this.revalidate(); } }); // when some info label is shown or hidden, update the frame size ComponentListener resizeListener = new ComponentAdapter() { @Override public void componentHidden(ComponentEvent e) { askForResize(); } @Override public void componentShown(ComponentEvent e) { askForResize(); } private void askForResize() { actionSupport.fireActionPerformed(ActionEventSupport.ACTION_NEED_RESIZE, null); } }; for (Component comp : infoPanel.getComponents()) { comp.addComponentListener(resizeListener); } // update components gatewayComboBoxActionPerformed(null); }