`
Now7!
  • 浏览: 7786 次
  • 性别: Icon_minigender_1
  • 来自: 济南
文章分类
社区版块
存档分类
最新评论

(转载)Swing中的AppContext使用

阅读更多

来自http://www.javafaq.nu/java-bookpage-13-5.html

2.5    AppContext service

class sun.awt.AppContext [platform specific]

 

Warning: AppContext is not meant to be used by any developer, as it is not part of the Java 2 core API. We are discussing it here only to facilitate a more thorough understanding of how Swing service classes work behind the scenes.

 

AppContext is an application/applet (we'll say "app" for short) service table that is unique to each Java session (applet or application). For applets, a separate AppContext exists for each SecurityContext which corresponds to an applet's codebase. For instance, if we have two applets on the same page, each using code from a different directory, both of those applets would have distinct SecurityContexts associated with them. If, however, they each were loaded from the same codebase, they would necessarily share a SecurityContext. Java applications do not have SecurityContexts. Rather, they run in namespaces which are distinguished by ClassLoaders. We will not go into the details of SecurityContexts or ClassLoaders here, but it suffices to say that they can be used by SecurityManagers to indicate security domains, and the AppContext class is designed to take advantage of this by only allowing one instance of itself to exist per security domain. In this way, applets from different codebases cannot access each other's AppContext. So why is this significant? We're getting there...

 

A shared instance is an instance of a class that is normally retreivable using a static method defined in that class. Each AppContext maintains a Hashtable of shared instances available to the associated security domain, and each instances is referred to as a service. When a service is requested for the first time it registers its shared instance with the associated AppContext. This consists of creating a new instance of itself and adding it to the AppContext key/value mapping.

 

One reason these shared instances are registered with an AppContext instead of being implemented as normal static instances, directly retreivable by the service class, is for security purposes. Services registered with an AppContext can only be accessed by trusted apps, whereas classes directly providing static instances of themselves allow these instances to be used on a global basis (requiring us to implement our own security mechanism if we want to limit access to them). Another reason for this is robustness. The less applets interact with each other in undocumented ways, the more robust they can be.

 

For example, suppose an app tries to access all of the key events on the system EventQueue (where all events get queued for processing in the event-dispatching thread) to try and steal passwords. By using distinct EventQueues in each AppContext, the only key events that the app would have access to are its own. (There is in fact only one EventQueue per AppContext.)

 

So how do we access our AppContext to add, remove, and retrieve services? AppContext is not meant to be accessed by developers. But we can if we really need to, and this would guarantee that our code would never be certified as 100% pure, because AppContext is not part of the core API. Nevertheless, here's what is involved: The static AppContext.getAppContext() method determines the correct AppContext to use depending on whether we are running an applet or application. We can then use the returned AppletContext's put(), get(), and remove() methods to manage shared instances. In order to do this we would need to implement our own methods such as the following:

 

    private static Object appContextGet(Object key) {

 

      return sun.awt.AppContext.getAppContext().get(key);

 

    }

 

    private static void appContextPut(Object key, Object value) {

 

      sun.awt.AppContext.getAppContext().put(key, value);

 

    }

 

    private static void appContextRemove(Object key) {

 

      sun.awt.AppContext.getAppContext().remove(key);

 

    }

 

In Swing, this functionality is implemented as three SwingUtilities static methods (refer to SwingUtilities.java source code):

 

    static void appContextPut(Object key, Object value)

 

    static void appContextRemove(Object key, Object value)

 

    static Object appContextGet(Object key)

 

However, we cannot access these because they are package private. These are the methods used by Swing's service classes. Some of the Swing service classes that register shared instances with AppContext include: EventQueue, TimerQueue, ToolTipManager, RepaintManager, FocusManager and UIManager.LAFState (all of which we will discuss at some point in this book). Interestingly, SwingUtilities secretly provides an invisible Frame instance registered with AppContext to act as the parent to all JDialogs and JWindows with null owners.

分享到:
评论

相关推荐

    JAVA swing中文离线API文档

    这份文档不仅包含了组件的用法,还有异常处理、国际化支持以及无障碍功能等相关信息,对于理解和使用Swing进行桌面应用开发至关重要。 总之,"JAVA Swing中文离线API文档"是一个极其宝贵的资源,它可以帮助Java开发...

    swing电脑app版抽奖、点卯、点到随机.zip点名

    在本项目中,"swing电脑app版抽奖、点卯、点到随机.zip点名" 提供了一个基于 Swing 的应用程序,用于执行一些特定的功能,如抽奖、点卯(签到)以及随机点名。这些功能通常在会议、活动中非常实用,能够增加互动性和...

    java swing 中使用Spring技术

    本项目“Java Swing中使用Spring技术”展示了如何在Swing应用中集成Spring框架,以实现更加模块化和可维护的代码结构。通过这种方式,开发者可以在Swing界面中利用Spring的强大功能,如管理对象的生命周期、处理事务...

    AWT与Swing使用区别

    - **顶级容器**:建议在Swing应用中使用JFrame、JApplet等Swing顶级容器,以减少潜在的问题。 综上所述,选择AWT还是Swing主要取决于项目需求。如果需要与操作系统紧密集成、重视性能,或者只需要基本的GUI功能,...

    Swing基本标签使用

    Swing使用不同的布局管理器,如FlowLayout、GridLayout、BoxLayout和GridBagLayout等,来决定组件在窗口中的位置和大小。标签的位置可以通过选择合适的布局管理器和调整其参数来控制。 8. 组合使用 在实际应用中,...

    Java 实现swing中嵌入html 实例 适合新手

    本实例面向Java新手,将详细讲解如何使用DJ Native Swing库在Java Swing应用中嵌入HTML。 首先,我们需要了解DJ Native Swing库。这是一个开源项目,它扩展了Java Swing,添加了对Web浏览器组件的支持。项目提供了...

    swing中tree使用方法

    swing中 tree的使用方法,详细例子,教程 树的使用 tree demo

    swing容器使用

    * 容器的层次结构:在 Swing 中,容器可以嵌套使用,例如 JFrame 中可以包含 JPanel,而 JPanel 中可以包含其他组件。 * 容器的布局管理:Swing 提供了多种布局管理器,例如 BorderLayout、FlowLayout、GridLayout ...

    Java Swing 中文Api

    Java Swing 是Java编程语言中的一个图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分。在Java 2 Platform Standard Edition 5.0版本中,Swing提供了丰富的组件和功能,使得开发者能够创建美观且...

    swing中JTable的使用

    类中使用JTable 实现了对JTable中行级的增删改 JFrame为自主布局,设置了背景图片以及窗口图标 有详细注释 例如: TableModel tableModel = jTable1.getModel(); DefaultTableModel dtm = (DefaultTableModel) ...

    swing中文帮助文件

    很全面的哟,swing中文帮助文件,让你快速掌握各种java swing控件

    Swing插件下载,swing下载,java-swing下载,swing-plugin

    在Java中,Swing插件则通常是指支持在Web浏览器中运行Swing组件的技术,这使得基于Swing的应用程序可以在网络环境中更加便捷地分发和使用。 "Swing插件下载"是指寻找并安装这个特定的Java插件,以便在浏览器中运行...

    Java的Swing界面皮肤、主题使用(substance.jar)内附使用教程

    在Java中使用Substance库,首先需要将其加入到项目类路径中。这通常通过将`substance.jar`文件放入项目的`lib`目录或者在构建路径中指定该文件来实现。对于Maven或Gradle项目,可以将依赖添加到相应的配置文件中。 ...

    java swing中文帮助文档

    很好的一个中文帮助API,易于学习和开发过程中遇到问题的查看

    swing中使用jfreenchart绘制柱状图,折线图,折线图中可动态添加线

    通过以上步骤,你就可以在Swing应用中使用JFreeChart库创建包含柱状图和折线图的选项卡,并且能够在折线图中动态添加线。这个功能可以用于数据可视化,数据分析等多个场景,为用户提供直观的数据展示。

    java swing 日期datepicker组件使用

    这个压缩包包含了使用DatePicker组件的示例项目以及相关的jar包,帮助我们了解如何在Swing应用中集成和操作这个日期选择器。 DatePicker组件在Swing中并不是标准的JFC/Swing组件,但可以通过第三方库如JDatePicker...

    Eclipse中swing插件

    Swing是Java编程环境中用于构建图形用户界面(GUI)的一个工具包,它是Java Foundation Classes (JFC)的一部分。在Eclipse这样的集成开发环境(IDE)中,Swing插件可以帮助开发者更加高效地设计、测试和调试Swing...

    swing 鼠标事件 loading效果

    在Swing中,可以使用`JLabel`配合`Icon`来实现简单的加载图标。一种常见的方式是创建一个`javax.swing.Timer`,每隔一段时间更新加载图标的状态。此外,可以使用`JOptionPane`或者自定义组件来展示一个带有加载效果...

    Swing中JTABLE中添加JBUTTON控件

    在Java的Swing库中,`JTable`是用于创建数据网格视图的重要组件,它允许用户以表格的形式查看和操作数据。而`JButton`则是一个常用的按钮控件,通常用于触发某些动作或事件。将`JButton`添加到`JTable`中可以为用户...

    Java_swing_api_中文

    5. **可定制性**:Swing组件允许深度定制,例如,可以自定义渲染器(CellRenderer)和编辑器(CellEditor)来改变组件的外观和行为,或者使用LookAndFeel来更改整个应用的视觉样式。 6. **国际化支持**:Swing API...

Global site tag (gtag.js) - Google Analytics