在Swing中何时确切实现组件 [英] When exactly are components realized in Swing

查看:64
本文介绍了在Swing中何时确切实现组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中所有UI组件都是通过XML文件定义的.我阅读了xml并完成了UI的构建.用户进行一些更改后,将提供新的XML,并相应地刷新UI.现在,我遇到了一些非常烦人的渲染问题,尤其是在几个滚动区域中,UI的一部分被切掉了(只有在调整窗口大小时才可见),或者滚动条已经滚动到某个点,但是它应该只是显示内容的顶部.

I'm developing an application, in which all UI components are defined via an XML file. I read the xml and depeding on it the UI is composed. After the user made some changes a new XML is provided and the UI is refreshed accordingly. Now I get some really annoying rendering issues, especially with several scroll areas, where either parts of the UI are cut of (and only get visible, when I resize the window) or the scrollbar is alread scrolled to some point, but it should just show the top of the content.

我认为这是一个事件调度线程问题,并在这里找到了一些非常有用的信息:

I assume this is an Event Dispatch Thread issue and found some really useful info about it here: http://www.javapractices.com/topic/TopicAction.do?Id=153

您可以在此处阅读:该线程(EDT)在实现组件后变为活动状态:已调用pack,show或setVisible(true)".

You can read there:"This thread (the EDT) becomes active after a component becomes realized : either pack, show, or setVisible(true) has been called"

EDT中没有解析XML,也没有实例化组件并将其添加到其父面板.只有在创建所有组件之后,它们才最终通过EDT添加到MainPane中. 但是,似乎在某些情况下创建组件并将其添加到面板中已经启动了EDT.所以事情变得一团糟.

Parsing the XML is not done in the EDT and also instantiating the components and adding them to their parent panels. Only after all components are created they are finally added to the MainPane via the EDT. However is seems in some cases creating components and adding them to panels, already starts the EDT. So things get messed up.

有人详细了解哪些方法会调用pack,show或setVisible(true)并因此启动EDT吗?

Does someone have detailed knowledge which methods call pack, show, or setVisible(true) and therefore start the EDT?

非常感谢

推荐答案

我在滚动窗格上遇到了相同的问题,请尝试在其上设置以下属性:

I had the same issues with scroll panes, try setting the following property on them:

scrollpane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);

正如评论中所述,所有Swing组件必须在EDT上创建,否则您将发生奇怪的事情.

As in the comments, all Swing components must be created on the EDT or your going to get weird stuff happening.

这篇关于在Swing中何时确切实现组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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