什么时候Swing组件可以显示? [英] When is a Swing component 'displayable'?

查看:92
本文介绍了什么时候Swing组件可以显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法(例如,通过一个事件?)来确定Swing组件何时变成'可显示的' - 按照 Component .getGraphics

Is there a way (e.g., via an event?) to determine when a Swing component becomes 'displayable' -- as per the Javadocs for Component.getGraphics?

我试图这样做的原因是我可以调用 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.

我试过添加一个 ComponentListener ,但是 componentShown 似乎没有被调用。还有什么我可以尝试的?

I've tried adding a ComponentListener, but componentShown doesn't seem to get called. Is there anything else I can try?

谢谢。

另外,保持我收到的 Graphics 对象?或者是否有可能在 Component 的生命周期的后期创建新的? (例如,在调整大小/隐藏后)

And additionally, is it OK to keep hold of the Graphics object I receive? Or is there potential for a new one to be created later in the lifetime of the Component? (e.g., after it is resized/hidden?)

推荐答案

添加HierarchyListener

Add a 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));

这篇关于什么时候Swing组件可以显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆