Java Code Examples for javax.swing.JComponent#revalidate()

The following examples show how to use javax.swing.JComponent#revalidate() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: gcs   File: FeatureEditor.java    License: Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent event) {
    String     command = event.getActionCommand();
    JComponent parent  = (JComponent) getParent();
    if (LeveledAmount.ATTRIBUTE_PER_LEVEL.equals(command)) {
        ((Bonus) mFeature).getAmount().setPerLevel(mLeveledAmountCombo.getSelectedIndex() == 1);
    } else if (CHANGE_BASE_TYPE.equals(command)) {
        LAST_FEATURE_TYPE = (FeatureType) mBaseTypeCombo.getSelectedItem();
        if (LAST_FEATURE_TYPE != null && !LAST_FEATURE_TYPE.matches(mFeature)) {
            Commitable.sendCommitToFocusOwner();
            try {
                parent.add(create(mRow, LAST_FEATURE_TYPE.createFeature()), UIUtilities.getIndexOf(parent, this));
            } catch (Exception exception) {
                // Shouldn't have a failure...
                exception.printStackTrace(System.err);
            }
            parent.remove(this);
            parent.revalidate();
            parent.repaint();
        }
    } else {
        super.actionPerformed(event);
    }
}
 
Example 2
private static void checkVisibility(JComponent comp) {
    if (comp == null) return;

    comp.invalidate();
    comp.revalidate();
    comp.doLayout();
    comp.repaint();

    for (Component c : comp.getComponents())
        if (c.isVisible()) {
            comp.setVisible(true);

            return;
        }

    comp.setVisible(false);
}
 
Example 3
Source Project: sc2gears   File: GuiUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates and returns a scroll panel which wraps the specified view component.<br>
 * The returned scroll panel disables vertical scroll bar, and only displays the horizontal scroll bar when the view does not fit
 * into the size of the view port. When the view fits into the view port, the scroll pane will not claim the space of the scroll bar.
 * 
 * @param view               view to wrap in the scroll pane
 * @param parentToRevalidate parent to revalidate when the scroll pane decides to change its size
 * 
 * @return the created self managed scroll pane
 */
public static JScrollPane createSelfManagedScrollPane( final Component view, final JComponent parentToRevalidate ) {
	final JScrollPane scrollPane = new JScrollPane( view );
	
	scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_NEVER );
	scrollPane.getHorizontalScrollBar().setPreferredSize( new Dimension( 0, 12 ) ); // Only want to restrict the height, width doesn't matter (it takes up whole width)
	scrollPane.getHorizontalScrollBar().setUnitIncrement( 10 );
	
	final ComponentListener scrollPaneComponentListener = new ComponentAdapter() {
		@Override
		public void componentResized( final ComponentEvent event ) {
			scrollPane.setHorizontalScrollBarPolicy( view.getWidth() < scrollPane.getWidth() ? JScrollPane.HORIZONTAL_SCROLLBAR_NEVER : JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
			scrollPane.setPreferredSize( null );
			scrollPane.setPreferredSize( new Dimension( 10, scrollPane.getPreferredSize().height ) );
			parentToRevalidate.revalidate();
		}
	};
	scrollPane.addComponentListener( scrollPaneComponentListener );
	
	return scrollPane;
}
 
Example 4
Source Project: pumpernickel   File: SplayedLayout.java    License: MIT License 6 votes vote down vote up
private void process(MouseEvent e) {
	Component c = e.getComponent();
	JComponent splayedAncestor = (JComponent) getSplayedAncestor(c);
	if (splayedAncestor == null) {
		c.removeMouseListener(this);
	} else if (prioritizeRollover) {
		if (e.getID() == MouseEvent.MOUSE_ENTERED
				&& splayedAncestor
						.getClientProperty(PROPERTY_ROLLOVER_CHILD) != c) {
			splayedAncestor.putClientProperty(PROPERTY_ROLLOVER_CHILD,
					c);
			splayedAncestor.revalidate();
		} else if (e.getID() == MouseEvent.MOUSE_EXITED
				&& splayedAncestor
						.getClientProperty(PROPERTY_ROLLOVER_CHILD) == c) {
			splayedAncestor.putClientProperty(PROPERTY_ROLLOVER_CHILD,
					null);
			splayedAncestor.revalidate();
		}
	}
}
 
Example 5
Source Project: arcusipcd   File: EnumControl.java    License: Apache License 2.0 5 votes vote down vote up
public EnumControl(JComponent parent, 
		final String title, List<String> values, String current, final CommandQueue commandQueue) {
	JLabel label = new JLabel(title);
	parent.add(label);
	parent.add(Box.createVerticalGlue());
	
	comboBox = new JComboBox<String>();
	for (String value : values) {
		comboBox.addItem(value);
	}
	comboBox.setSelectedItem(current);
	comboBox.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (!isUpdating) {
				String newValue = (String)comboBox.getSelectedItem();
				Command setCommand = new SetParameterValue();
				setCommand.putAttribute(title, newValue);
				try {
					commandQueue.insertCommand(setCommand);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		
	});
	
	parent.add(comboBox);
	parent.add(Box.createVerticalStrut(40));
	parent.revalidate();
}
 
Example 6
Source Project: ChatGameFontificator   File: ComboMenuBar.java    License: The Unlicense 5 votes vote down vote up
public void updateScroll()
{
    List<JMenuItem> menuKeyList = getKeyList();

    // Keep the first index less than the total number available in the list (unfiltered) minus the length of the scroll count, and no lower than zero
    firstIndex = Math.min(firstIndex, menuKeyList.size() - SCROLL_COUNT);
    firstIndex = Math.max(firstIndex, 0);
    int lastIndex = firstIndex + Math.min(menuKeyList.size(), SCROLL_COUNT);

    int runningUnfilteredCount = 0;
    for (int i = 0; i < menuKeyList.size(); i++)
    {
        JMenuItem jmi = menuKeyList.get(i);
        MenuVisibilityStatus status = allMenuItems.get(jmi);
        final boolean inScrollBounds = !status.isFiltered() && runningUnfilteredCount >= firstIndex && runningUnfilteredCount < lastIndex;
        status.setOutOfScrollBounds(!inScrollBounds);
        if (!status.isFiltered())
        {
            runningUnfilteredCount++;
        }
    }

    mainMenu.getPopupMenu().removeAll();
    setMenuItemFilterVisibility();

    upItem.setEnabled(firstIndex > 0);
    mainMenu.getPopupMenu().add(upItem);
    List<JMenuItem> inBoundsScrollMenuFolders = getInBoundScrollMenuFolders();

    for (JMenuItem menuFolder : inBoundsScrollMenuFolders)
    {
        mainMenu.getPopupMenu().add(menuFolder);
    }
    downItem.setEnabled(lastIndex < runningUnfilteredCount);
    mainMenu.getPopupMenu().add(downItem);

    JComponent parent = (JComponent) upItem.getParent();
    parent.revalidate();
    parent.repaint();
}
 
Example 7
private void installInLayeredPane(JComponent component) {
    JLayeredPane layeredPane = getRootPane().getLayeredPane();
    layeredPane.add(component, JLayeredPane.PALETTE_LAYER, 20);
    Dimension size = component.getPreferredSize();
    component.setSize(size);
    component.setLocation((getWidth() - size.width) / 2,
            (getHeight() - size.height) / 2);
    component.revalidate();
    component.setVisible(true);
}
 
Example 8
private void refreshMenu() {
    if (menuItems != null && menuItems.length > 0) {
        firstIndex = Math.max(topFixedCount, firstIndex);
        firstIndex = Math.min(menuItems.length - bottomFixedCount - scrollCount, firstIndex);

        upItem.setEnabled(firstIndex > topFixedCount);
        downItem.setEnabled(firstIndex + scrollCount < menuItems.length - bottomFixedCount);

        menu.removeAll();
        for (int i = 0; i < topFixedCount; i++) {
            menu.add(menuItems[i]);
        }
        if (topFixedCount > 0) {
            menu.addSeparator();
        }

        menu.add(upItem);
        for (int i = firstIndex; i < scrollCount + firstIndex; i++) {
            menu.add(menuItems[i]);
        }
        menu.add(downItem);

        if (bottomFixedCount > 0) {
            menu.addSeparator();
        }
        for (int i = menuItems.length - bottomFixedCount; i < menuItems.length; i++) {
            menu.add(menuItems[i]);
        }

        JComponent parent = (JComponent) upItem.getParent();
        parent.revalidate();
        parent.repaint();
    }
}
 
Example 9
Source Project: gcs   File: FeatureEditor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private void addFeature() {
    JComponent parent = (JComponent) getParent();
    try {
        parent.add(create(mRow, LAST_FEATURE_TYPE.createFeature()));
    } catch (Exception exception) {
        // Shouldn't have a failure...
        exception.printStackTrace(System.err);
    }
    if (mFeature == null) {
        parent.remove(this);
    }
    parent.revalidate();
}
 
Example 10
Source Project: gcs   File: FeatureEditor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private void removeFeature() {
    JComponent parent = (JComponent) getParent();
    parent.remove(this);
    if (parent.getComponentCount() == 0) {
        parent.add(new NoFeature(mRow));
    }
    parent.revalidate();
    parent.repaint();
}
 
Example 11
Source Project: gcs   File: SkillDefaultEditor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private void addDefault() {
    SkillDefault skillDefault = new SkillDefault(LAST_ITEM_TYPE, LAST_ITEM_TYPE.isSkillBased() ? "" : null, null, 0); //$NON-NLS-1$
    JComponent   parent       = (JComponent) getParent();
    parent.add(new SkillDefaultEditor(skillDefault));
    if (mDefault == null) {
        parent.remove(this);
    }
    parent.revalidate();
    parent.repaint();
    notifyActionListeners();
}
 
Example 12
Source Project: gcs   File: SkillDefaultEditor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private void removeDefault() {
    JComponent parent = (JComponent) getParent();
    parent.remove(this);
    if (parent.getComponentCount() == 0) {
        parent.add(new SkillDefaultEditor());
    }
    parent.revalidate();
    parent.repaint();
    notifyActionListeners();
}
 
Example 13
Source Project: gcs   File: ListPrereqEditor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private void addItem(Prereq prereq) {
    JComponent parent = (JComponent) getParent();
    int        index  = UIUtilities.getIndexOf(parent, this);
    ((PrereqList) mPrereq).add(0, prereq);
    parent.add(create(mRow, prereq, getDepth() + 1), index + 1);
    parent.revalidate();
}
 
Example 14
Source Project: gcs   File: PrereqEditor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private void remove() {
    JComponent parent = (JComponent) getParent();
    int        index  = UIUtilities.getIndexOf(parent, this);
    int        count  = countSelfAndDescendents(mPrereq);
    for (int i = 0; i < count; i++) {
        parent.remove(index);
    }
    mPrereq.removeFromParent();
    parent.revalidate();
    parent.repaint();
}
 
Example 15
Source Project: gcs   File: PrereqEditor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (CHANGE_BASE_TYPE.equals(command)) {
        Class<?> type = BASE_TYPES[mBaseTypeCombo.getSelectedIndex()];
        if (!mPrereq.getClass().equals(type)) {
            JComponent parent    = (JComponent) getParent();
            PrereqList list      = mPrereq.getParent();
            int        listIndex = list.getIndexOf(mPrereq);
            try {
                Prereq prereq;
                if (type == ContainedWeightPrereq.class) {
                    prereq = new ContainedWeightPrereq(list, mRow.getDataFile().defaultWeightUnits());
                } else {
                    prereq = (Prereq) type.getConstructor(PrereqList.class).newInstance(list);
                }
                if (prereq instanceof HasPrereq && mPrereq instanceof HasPrereq) {
                    ((HasPrereq) prereq).has(((HasPrereq) mPrereq).has());
                }
                list.add(listIndex, prereq);
                list.remove(mPrereq);
                parent.add(create(mRow, prereq, mDepth), UIUtilities.getIndexOf(parent, this));
            } catch (Exception exception) {
                // Shouldn't have a failure...
                exception.printStackTrace(System.err);
            }
            parent.remove(this);
            parent.revalidate();
            parent.repaint();
            ListPrereqEditor.setLastItemType(type);
        }
    } else if (CHANGE_HAS.equals(command)) {
        ((HasPrereq) mPrereq).has(((JComboBox<?>) event.getSource()).getSelectedIndex() == 0);
    } else {
        super.actionPerformed(event);
    }
}
 
Example 16
private void displayError(JComponent view) {
    view.removeAll();
    view.add(new JLabel("Failed to load probes, check the logfile",
             SwingConstants.CENTER), BorderLayout.CENTER);
    
    view.revalidate();
    view.repaint();
}
 
Example 17
private void refreshMenu() {
  if (menuItems != null && menuItems.length > 0) {
    firstIndex = Math.max(topFixedCount, firstIndex);
    firstIndex = Math.min(menuItems.length - bottomFixedCount - scrollCount, firstIndex);

    upItem.setEnabled(firstIndex > topFixedCount);
    downItem.setEnabled(firstIndex + scrollCount < menuItems.length - bottomFixedCount);

    menu.removeAll();
    for (int i = 0; i < topFixedCount; i++) {
      menu.add(menuItems[i]);
    }
    if (topFixedCount > 0) {
      menu.add(new JSeparator());
    }

    menu.add(upItem);
    for (int i = firstIndex; i < scrollCount + firstIndex; i++) {
      menu.add(menuItems[i]);
    }
    menu.add(downItem);

    if (bottomFixedCount > 0) {
      menu.add(new JSeparator());
    }
    for (int i = menuItems.length - bottomFixedCount; i < menuItems.length; i++) {
      menu.add(menuItems[i]);
    }

    JComponent parent = (JComponent) upItem.getParent();
    parent.revalidate();
    parent.repaint();
  }
}
 
Example 18
Source Project: netbeans   File: PlatformsCustomizer.java    License: Apache License 2.0 4 votes vote down vote up
private void selectPlatform (Node pNode) {
    Component active = null;
    for (Component c : cards.getComponents()) {
        if (c.isVisible() &&
            (c == jPanel1 || c == messageArea)) {
                active = c;
                break;
        }
    }
    final Dimension lastSize = active == null ?
        null :
        active.getSize();
    this.clientArea.removeAll();
    this.messageArea.removeAll();
    this.removeButton.setEnabled (false);
    if (pNode == null) {
        ((CardLayout)cards.getLayout()).last(cards);
        return;
    }
    JComponent target = messageArea;
    JComponent owner = messageArea;
    JavaPlatform platform = pNode.getLookup().lookup(JavaPlatform.class);
    if (pNode != getExplorerManager().getRootContext()) {
        if (platform != null) {
            this.removeButton.setEnabled (canRemove(platform, pNode.getLookup().lookup(DataObject.class)));
            if (!platform.getInstallFolders().isEmpty()) {
                this.platformName.setText(pNode.getDisplayName());
                for (FileObject installFolder : platform.getInstallFolders()) {
                    File file = FileUtil.toFile(installFolder);
                    if (file != null) {
                        this.platformHome.setText (file.getAbsolutePath());
                    }
                }
                target = clientArea;
                owner = jPanel1;
            }
        }
        Component component = null;
        if (pNode.hasCustomizer()) {
            component = pNode.getCustomizer();
        }
        if (component == null) {
            final PropertySheet sp = new PropertySheet();
            sp.setNodes(new Node[] {pNode});
            component = sp;
        }
        addComponent(target, component);
    }
    if (lastSize != null) {
        final Dimension newSize = owner.getPreferredSize();
        final Dimension updatedSize = new Dimension(
            Math.max(lastSize.width, newSize.width),
            Math.max(lastSize.height, newSize.height));
        if (!newSize.equals(updatedSize)) {
            owner.setPreferredSize(updatedSize);
        }
    }
    target.revalidate();
    CardLayout cl = (CardLayout) cards.getLayout();
    if (target == clientArea) {
        cl.first (cards);
    }
    else {
        cl.last (cards);
    }
}
 
Example 19
Source Project: netbeans   File: ZoomManager.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Set the zoom factor for the Scene mananged by this ZoomManager
 * instance. The value represents a percentage (e.g. 100%) and
 * must be a positive number. Any value outside of the defined
 * range (<tt>MIN_ZOOM_PERCENT</tt> and <tt>MAX_ZOOM_PERCENT</tt>)
 * will be forced into that range.
 *
 * @param  percent  the percent value (e.g. 50 for half-size,
 *                  200 for double-size).
 * @param  center   the point at which to zoom in and keep centered.
 */
public void setZoom(int percent, Point center) {
    if (percent < MIN_ZOOM_PERCENT) {
        percent = MIN_ZOOM_PERCENT;
    } else if (percent > MAX_ZOOM_PERCENT) {
        percent = MAX_ZOOM_PERCENT;
    }

    // Find the current center point prior to zooming.
    Point sceneCenter = scene.convertViewToScene(center);
    zoomPercentage = percent;
    // Convert the percent value to the zoom factor Scene is expecting
    // (a double that acts as the multiplier to the component sizes and
    // locations, such that 0.5 is 50%, 1.0 is 100%, and 2.0 is 200%.
    double factor = ((double) percent) / 100.0d;
    scene.setZoomFactor(factor);
    // Setting the zoom factor alone is not enough, must force
    // validation and repainting of the scene for it to work.
    scene.validate();
    scene.repaint();

    // Find the new center point and scroll the view after zooming.
    Point newViewCenter = scene.convertSceneToView(sceneCenter);
    JComponent view = scene.getView();
    Rectangle visRect = view.getVisibleRect();
    visRect.x = newViewCenter.x - (center.x - visRect.x);
    visRect.y = newViewCenter.y - (center.y - visRect.y);
    Dimension viewSize = view.getSize();
    if (visRect.x + visRect.width > viewSize.width) {
        visRect.x = viewSize.width - visRect.width;
    }
    if (visRect.y + visRect.height > viewSize.height) {
        visRect.y = viewSize.height - visRect.height;
    }
    if (visRect.x < 0) {
        visRect.x = 0;
    }
    if (visRect.y < 0) {
        visRect.y = 0;
    }
    view.scrollRectToVisible(visRect);
    view.revalidate();
    view.repaint();

    // Notify registered listeners so they may update their state.
    fireZoomEvent(percent);
}
 
Example 20
Source Project: NBANDROID-V2   File: SdksCustomizer.java    License: Apache License 2.0 4 votes vote down vote up
private void selectPlatform(Node pNode) {
    Component active = null;
    for (Component c : cards.getComponents()) {
        if (c.isVisible()
                && (c == jPanel1 || c == messageArea)) {
            active = c;
            break;
        }
    }
    final Dimension lastSize = active == null
            ? null
            : active.getSize();
    this.clientArea.removeAll();
    this.messageArea.removeAll();
    this.removeButton.setEnabled(false);
    if (pNode == null) {
        ((CardLayout) cards.getLayout()).last(cards);
        return;
    }
    JComponent target = messageArea;
    JComponent owner = messageArea;
    selectedPlatform = pNode.getLookup().lookup(AndroidSdk.class);
    if (pNode != getExplorerManager().getRootContext()) {
        if (selectedPlatform != null) {
            mkDefault.setEnabled(!selectedPlatform.isDefaultSdk());
            this.removeButton.setEnabled(!selectedPlatform.isDefaultSdk());
            if (!selectedPlatform.getInstallFolders().isEmpty()) {
                this.platformName.setText(pNode.getDisplayName());
                for (FileObject installFolder : selectedPlatform.getInstallFolders()) {
                    File file = FileUtil.toFile(installFolder);
                    if (file != null) {
                        this.platformHome.setText(file.getAbsolutePath());
                    }
                }
                target = clientArea;
                owner = jPanel1;
            }
        } else {
            removeButton.setEnabled(false);
            mkDefault.setEnabled(false);
        }
        Component component = null;
        if (pNode.hasCustomizer()) {
            component = pNode.getCustomizer();
        }
        if (component == null) {
            final PropertySheet sp = new PropertySheet();
            sp.setNodes(new Node[]{pNode});
            component = sp;
        }
        addComponent(target, component);
    }
    if (lastSize != null) {
        final Dimension newSize = owner.getPreferredSize();
        final Dimension updatedSize = new Dimension(
                Math.max(lastSize.width, newSize.width),
                Math.max(lastSize.height, newSize.height));
        if (!newSize.equals(updatedSize)) {
            owner.setPreferredSize(updatedSize);
        }
    }
    target.revalidate();
    CardLayout cl = (CardLayout) cards.getLayout();
    if (target == clientArea) {
        cl.first(cards);
    } else {
        cl.last(cards);
    }
}