java invalidate_Java Component.invalidate方法代码示例

该段代码展示了在Java中如何使用Component.invalidate方法来更新组件布局。当需要替换内嵌组件时,首先保存当前焦点组件,清除全局焦点以防止组件移除时焦点丢失。然后获取新的内嵌组件并设置其启用状态、前景色和背景色。如果新的内嵌组件与旧的不同,则移除旧组件,添加新组件,并调用invalidate方法强制重新布局。在显示状态下,还会调用validate方法立即重新布局。此外,代码还处理了焦点恢复和焦点转移的情况。
摘要由CSDN通过智能技术生成

import java.awt.Component; //导入方法依赖的package包/类

/** Installs the component we will embed to display the property */

private void installDisplayerComponent() {

//Fetch or instantiate the component we will embed to display the

//property. Depending on the prefs, it may be a RendererPropertyDisplayer,

//an EditablePropertyDisplayer or a CustomPropertyDisplayer.

PropertyDisplayer displayer = getPropertyDisplayer();

//Find who has focus now, so if we have focus, focus won't end up set

//to null

Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();

boolean hadFocus = (focusOwner == this) || isAncestorOf(focusOwner);

if (hadFocus) {

//If we had focus, clear the global focus owner for now, so that

//when the existing component is removed, it does not cause

//focus to get briefly set to a random component

KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();

}

//Fetch the new inner component (the custom editor or Inplace editor)

Component newInner = displayer.getComponent();

//Set the enabled state appropriately. For implementations of

//PropertyDisplayer_Editable, this will already be handled; for render-

//only cases, it should be handled explicitly

if (!(displayer instanceof PropertyDisplayer_Editable)) {

//only for renderers

newInner.setEnabled(isEnabled() && getProperty().canWrite());

}

newInner.setForeground(getForeground());

newInner.setBackground(getBackground());

//Make sure the inner component has really changed

if (newInner != inner) {

synchronized (getTreeLock()) {

//remove the odl component

if (inner != null) {

remove(inner);

}

//and add the new one (if any)

if (newInner != null) {

add(newInner);

//invalidate its layout so it will be re-laid out

newInner.invalidate();

inner = newInner;

}

}

}

//Force a re-layout immediately if visible

if (isShowing() && !(getParent() instanceof javax.swing.CellRendererPane)) {

validate();

}

//Restore focus if necessary

if (hadFocus && isEnabled() && ((preferences & PREF_READ_ONLY) == 0)) {

requestFocus();

}

//Simply adding a component to a container can sometimes cause it to be

//given focus even though it's not focusable. If this has happened,

//find the next component in the focus cycle root and force focus to that.

//Mainly a problem with JFileChooser, but we also have a few property

//editors that force focus on addNotify which should be fixed

if (!isEnabled() || ((preferences & PREF_READ_ONLY) != 0)) {

Component focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

if ((focus == inner) || ((inner instanceof Container) && ((Container) inner).isAncestorOf(focus))) {

this.transferFocusUpCycle();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值