简体   繁体   English

什么是Swing组件'可显示'?

[英]When is a Swing component 'displayable'?

Is there a way (eg, via an event?) to determine when a Swing component becomes 'displayable' -- as per the Javadocs for Component .getGraphics ? 有没有办法(例如,通过事件?)来确定Swing组件何时变得“可显示” - 按照Component的Javadocs .getGraphics

The reason I'm trying to do this is so that I can then call getGraphics() , and pass that to my 'rendering strategy' for the component. 我试图这样做的原因是我可以调用getGraphics() ,并将其传递给组件的'渲染策略'。

I've tried adding a ComponentListener , but componentShown doesn't seem to get called. 我已经尝试添加ComponentListener ,但似乎没有调用componentShown Is there anything else I can try? 还有什么我可以尝试的吗?

Thanks. 谢谢。

And additionally, is it OK to keep hold of the Graphics object I receive? 另外,保持我收到的Graphics对象是否可以? Or is there potential for a new one to be created later in the lifetime of the Component ? 或者是否有可能在Component的生命周期中稍后创建新的? (eg, after it is resized/hidden?) (例如,在调整大小/隐藏后?)

Add a HierarchyListener 添加HierarchyListener

public class MyShowingListener {
private JComponent component;
public MyShowingListener(JComponent jc) { component=jc; }

public void hierarchyChanged(HierarchyEvent e) {
    if((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED)>0 && component.isShowing()) {
        System.out.println("Showing");
    }
}
}

JTable t = new JTable(...);
t.addHierarchyListener(new MyShowingListener(t));

You can listen for a resize event. 您可以收听调整大小事件。 When a component is first displayed, it is resized from 0,0 to whatever the layout manager determines (if it has one). 首次显示组件时,会将其从0,0调整为布局管理器确定的任何大小(如果有的话)。

You need to check up the component hierarchy. 您需要检查组件层次结构。 Check after AncestorListener.ancestorAdded is called. 在调用AncestorListener.ancestorAdded之后检查。

I've always used Coomponent.addNotify to know when the component is ready to be rendered.Not sure if is the the best way, but it works for me. 我总是使用Coomponent.addNotify知道组件何时可以渲染。不确定是否是最好的方法,但它对我有用。 Of course you must subclass the component. 当然,您必须对组件进行子类化。

Component.isDisplayable should be the right answer but I know it didn't worked for me as I thought it will(I don't remember why, but there was something and I switched to addNotify). Component.isDisplayable应该是正确的答案,但我知道它不适合我,因为我认为它会(我不记得为什么,但有一些东西,我切换到addNotify)。

Looking in the SUN's source code, I can see addNotify fires a HierarchyEvent.SHOWING_CHANGED so this is the best way to be notified. 查看SUN的源代码,我可以看到addNotify触发了HierarchyEvent.SHOWING_CHANGED,因此这是获得通知的最佳方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java / Swing - >创建通知JFrame,错误“框架可显示” - Java / Swing -> Creating a notification JFrame, and the error “The frame is displayable” Swing 组件在大量更新时闪烁 - Swing component flickering when updated a lot 隐藏组件时停止Swing计时器 - Stopping a Swing timer when a component is hidden 在 swing 中调整组件大小时如何防止闪烁? - How to prevent flickering when resizing a component in swing? Java:在Eclipse调试模式下,使用“显示”时,增加可显示文本? - Java: In Eclipse Debug Mode, When using 'Display', Increase Displayable Text? 在游戏循环之前或之中运行时未显示Swing组件 - Not showing Swing component when running before or in the game loop Java Swing:在组件*完成*大小调整时执行某些操作 - Java Swing: Do something when a component has *finished* resizing Java / Swing:鼠标悬停在组件上方时不会发生变化 - Java/Swing: Mouse cursor doesn't change when over component Java(Swing):在调整窗口大小时查找组件的* screen *大小 - Java (Swing): finding a component's *screen* size when the window is resized 为什么创建Swing组件时会出现NullPointerException? - Why am I getting a NullPointerException when creating a Swing component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM