View RSS Feed

Java Swing

Java Swing tutorials.

  1. How to Write a Change Listener

    by , 04-26-2012 at 09:24 PM
    Property change listener and a change listener are similar. On an object, change listener gets register, typically at component. However, it may be some other object and when object is changed notification is sent to the listener. In case of change listener, notification of any sort of change is not sent to the listener but changing of the source object is notified. Hence, change listeners are considered as more useful only when it becomes necessary to be aware that when object gets changed. ...
  2. How to Write a Caret Listener

    by , 04-26-2012 at 09:23 PM
    In text component when selection gets changed or moved by the caret (i.e the cursor which show insertion point), caret events take place. Document of the text component may be initiating the caret event when text is inserted or removed. Caret listener may be attached to any subclass of the JTextComponent instance with method addCaretListener.
  3. How to Write an Action Listener

    by , 04-26-2012 at 09:22 PM
    Action Listeners are the most common and easiest event handlers for implementation. An action listener is implemented to detail that what could be possibly done when cetain operations are performed by user.

    When a user performs an action, an action event takes place. For example, when a button is clicked by a user, menu item is choosen and in text field ENTER is pressed. An actionPerformed message would be sent to all of the action listeners , which on a relevant component are registered. ...
  4. Sending a Description of an Action to a Status Bar

    by , 04-26-2012 at 09:22 PM
    Requested feature that is more common is that architecture which supports the content messages sending to status bar, when mouse is present at toolbar buttons or menu items. Support will be provided by the architecture of current action which behaves elegantly.
    When and addition of action is done to a container like JMenu or JToolBar, component which got added to container, is returned. MouseAdapter’s shared instance is added, to returned components. When at component the mouse is moved, ...
    Tags: status bar Add / Edit Tags
  5. Enabling and Disabling of Components Which Share an Action

    by , 04-26-2012 at 08:50 PM
    When “enabled”, the bound property of Action is enabled/disabled, property change event is sent to property change listeners. The AbstractButton consist of “inner-class property change listener” that disables/enables the correspondent component.

    In case of ActionDemo applications, actions get initialized in initActions() method. Similar Actions set is shared among popup menu, toolbar, menu and also are used for construction of the UI controls in createPopupMenu(),createToolBar() and ...
  6. Interface Action

    by , 04-26-2012 at 08:49 PM
    A useful extension is provided by the Action interface to the Action Listener interface where multiple controls may access similar functionality.
    ActionPerformed method that has been defined by the help of ActionListener interface, this interface would be allowing application to define:

    • One or more than one text strings that provide details regarding the function. Strings could be used for display of the button;s flyover text or setting up of the text, in menu item.
    • One
  7. How to Use Actions

    by , 04-26-2012 at 08:48 PM
    For separation of the state and functionality from component, an action is used. For instance, if there are 2 or more than 2 components which perform similar kind of functions, an Action object shall be considered for implementation of the function. An action object act as action listener which assist action event handling as well as centralized handling for component of action event firing state. For example, text fields, common buttons, menu items and tool bar buttons. The state which can be handled ...
  8. Designing a Swing GUI

    by , 04-26-2012 at 08:47 PM
    For simpler GUI designs, discussed above MVC widget internals could be ignored. Widget placement can be approached in GUI along with Swing similarly as AWT: by widget’s instantiation and their addition to the containers. In addition to this, MVC is used by two techniques for designing powerful and flexible GUIs.

    1. Simpler GUI Design: Widgets are instantiated and added to container. Then selection of Look & Feel is done for whole GUI.
    2. Complex GUI Design: Though Swing directly
  9. Component Architecture and MVC

    by , 04-26-2012 at 08:46 PM
    MVC’s common variation is used to present components by Swing where controller and view get combined to form an object known as delegate. Delegates present model and also translates the input of user into model, just like it is done by a controller. Communication that takes place b/w controller and view is found to be very complex. Two simplifies are combined, for the component design.

    For example, checkbox widget shall be considered. It consists of a state which could be true or ...
  10. Stateless vs. stateful delegates

    by , 04-26-2012 at 08:45 PM
    Methods which are present at ComponentUI use JComponent object, as a parameter. UI delegate’s stateless implementation is enabled by this convention. Implementation of stateless UI delegate permits one instance of UI delegate, to get used for component class’s instances. This would be reducing no. of objects that get instantiated.
    Suck kind of approach is for various simple GUI components. However, in case of more complexed components, it is not considered as “win” as inefficiency is created ...
  11. Distinguishing between UI-set and app-set properties

    by , 04-26-2012 at 08:44 PM
    Swing. Therefore this would be a critical thing to ensure that values don’t clobber when component’s default properties are set up by Look and Feel, for the component.
    This issue doesn’t arise when UI delegate first time is installed, at the component as properties are uninitialized and ultimately would be settled legally by Look&Feel. When individual properties are set after construction of component & subsequently new setup of Look and Feel takes place, such problem arises. Hence, ...
  12. The look-and-feel packages

    by , 04-26-2012 at 08:43 PM
    Swing.plaf, ScrollBarUI, ButtonUI etc which are present in UI delegates defines the accurate API which can be used by a component for interaction with instance of UI delegate. It shall be noted that originally interfaces were used here but got replaced by abstract classes as it was being felt that API was immature to with-stand interface’s concrete casting. Implementation of look and feel’s root is plaf APIs.

    Every implementation of look and feel gives abstract plaf classes’s concrete ...
  13. Managing look-and-feel data

    by , 04-26-2012 at 08:42 PM
    A static class known as UIManager.LookAndFeelInfo is defined by UIManager to store high leveled names, like Metal, and specified classname like "", for LookAndFeel.Internally these classes are used for management of the LookAndFeel objects which are known. UIManager could access this information through these given static methods:
    Java Code:
       public static LookAndFeelInfo[]
       public static
  14. Dynamically Changing the Default Look-and-Feel

    by , 04-26-2012 at 08:41 PM
    When look and feel is being set programmatically by the Swing application as it has been discussed above, the ideal place considered is before instantiation of the Swing components. This happens as the particular LookAndFeel is made current default by UIManager.setLookAndFeel() method, by initializing or loading the LookAndFeel instance, however components are not automatically caused to make their look & feel changed.

    It shall be noticed that UI delegates are initialized ...
  15. The 'default' look and feel

    by , 04-26-2012 at 08:41 PM
    Methods are provided by UIManager to set or get the current, default Look & feel.
    Java Code:
    public static LookAndFeel 
       public static void
          setLookAndFeel(LookAndFeel newLookAndFeel)
       public static void 
          setLookAndFeel(String className)
    Being default look & feel, cross platformed look and feel of Java which is called “Metal” is initialized by the Swing. But if default look ...
  16. Look-and-feel management

    by , 04-26-2012 at 08:40 PM
    An abstract look and feel class is defined by Swing which present all central information to the implementation of look and feel, like description, name, whether look and feel is native or not, a hash table to store default values for different attributes of look and feel, like fonts and colors.

    Every implementation of look and feel defines the LookAndFeel subclass. For instance, swing.plaf.motif.MotifLookAndFeel provides Swing along with important details for management of look & ...
  17. The component hooks

    by , 04-26-2012 at 08:39 PM
    Look and feel behavior of every swing component defines swing.plaf package’s abstract class for representation of UI delegate. These classes naming convention is taking class name for sake of component, append UI and remove prefix

    In constructor of component, UI delegate is made, of which access is given as “JavaBeans” bound property, at the component. For instance, following methods are provided by JScrollBar for access of UI delegate.

    Java Code:
       public ScrollBarUI
  18. Pluggable look-and-feel architecture

    by , 04-26-2012 at 08:38 PM
    Look and feel, plugable architecture of swing provides one component API, with no dictation of look and feel. A default look and feel set is provided by the swing toolkit. API is considered as “open”, a design which allows additionally developers to make new implementations of look and feel, either by extension of present look and feel or by creating a new one. Pluggable look and feel is although extensible but it was designed intentionally at that level lower than basic component API. This is done ...
  19. Model change notification

    by , 04-26-2012 at 08:37 PM
    Any interested parties shall be notified by the modes when value or data gets changed. JavaBeans Event model is used by the Swing models for notification implementation. Two approaches used in Swing for this notification:

    • A lightweighted notification shall be sent which has been changed by the state and listener is required to rsend back the query to model for determining what has changed. This approach has an advantage that one event instance might be used from a particular model
  20. Application-data models

    by , 04-26-2012 at 08:36 PM
    Application data models are those interfaces which represent certain data that is quantifiable and it has meaning in application’s context like a cell present in table or items that have been displayed in some list. Such data models give quite powerful paradigm of programming for the swing programs, which require clear separation b/w GUI and application logic/data. For Swing components which are truly data centric like JTable and JTree, interaction is strongly recommended with data model. Application ...
  21. GUI-state models

    by , 04-26-2012 at 08:36 PM
    GUI state models are considered as those interfaces that are involved in defining the GUI control’s visual status. Like a button is armed or pressed, or which items in a list shall be chosen. Typically, GUI state models are just relevant in GUI context. It is also useful for development of programs by using separation of GUI state mode, specially when multiple controls of GUI have been associated with one common state. Or, when one control’s manipulation will be changing the another’s value automatically. ...
  22. Java Swing Roots in MVC

    by , 04-26-2012 at 08:35 PM
    In MVC i.e. Model view controller design, swing architecture has been rooted which dates back, to SmallTalk. MVC calls visual applications to be separately broken into 3 parts:
    • A Model- to present data, for application.
    • The view which is data’s the representation.
    • A controller – to take user input & translates them into changes present in model.

    MVC was considered as a logical choice earlier for Swing as bas was provided to meet up 1st three design goals, by staying ...
  23. Java Swing Design Goals

    by , 04-26-2012 at 08:34 PM
    Swing project has following goal:

    To build a set of extensible GUI components to enable developers to more rapidly develop powerful Java front ends for commercial applications.

    Design goals were established by the Swing team earlier in project, which resulted into architecture. Guidelines are:
    1. In java, easy maintenance and class platform consistency could be promoted by being implemented.
    2. Single API shall be provided which is able to support various look
  24. How to use JTree in Java

    by , 02-29-2012 at 07:14 AM
    JTree is a Swing component that displays hierarchical data in a tree structure. The screen-shot below depicts a tree component:

    Name:  sample tree.png
Views: 7633
Size:  12.3 KB
    Figure: A tree component in Java

    This article will focus on how to implement such a tree component in Java, by walking through basic concepts to develop an interesting sample application.

    Fundamental concepts about Tree component

    • Node: A tree displays data vertically in rows,
    Tags: java, jtree, swing Add / Edit Tags
  25. Swing delegation event model

    by , 01-08-2012 at 01:38 PM
    Registered event listeners are notified for the event received by objects in swing delegation event model. Event receiver is a component in most cases.

    Event Types: ActionEvent, KeyEvent, MouseEvent, WindowEvent etc.
    Event Processors: JButton, JList etc.
    EventListeners: ActionListener, ComponentListener, KeyListener etc.

    Name:  62.JPG
Views: 3083
Size:  15.8 KB

    Swing delegation event model
  26. Layout Managers

    by , 01-08-2012 at 01:35 PM
    FlowLayout: Applet and Panel has FlowLayout as their default layout. It lays components from left to right.

    BorderLayout: Frame and Dialog has BorderLayout as their default layout. It lays components in north, south, east, west and center.

    CardLayout: stack of same size components arranged inside each other.

    GridLayout: Makes a bunch of components equal in size and displays them in the requested number of rows and columns.

    GridBagLayout: ...
  27. JTable MVC

    by , 01-08-2012 at 01:34 PM
    Swing uses a modified version of Model View Controller design pattern. Model, view and controller layers are separated by MVC design pattern.

    Name:  62.JPG
Views: 1785
Size:  19.7 KB

    Swing MVC Architecture

    • Component (JTable, JTree, and JList): These components coordinate.
    • Model (TableModel): charged with storing the data.
    • UIDelegate: It is responsible for getting the data from model and rendering it to screen.
    Tags: jtable, mvc Add / Edit Tags
    Architecture , JTable
  28. Swing Hierarchy

    by , 01-08-2012 at 01:31 PM
    All the Swing components start with ‘J’. Following diagram shows the swing hierarchy. JComponent is the base class for all components.

    Name:  62.JPG
Views: 5065
Size:  42.4 KB

    Swing Hierarchy
  29. Swing Action architecture

    by , 01-08-2012 at 01:28 PM
    Shared behavior between different user interfaces components are implemented by “Swing Action Architecture”. Like tool bar buttons and menu items perform same actions. All the components are disabled when an action is disabled.

    Java Code: Swing Action Architecture
    class FileAction extends AbstractAction {
    FileAction(String name) {
    public void actionPerformed(ActionEvent ae){
    //add action logic here
    JMenu fileMenu

    Updated 01-14-2012 at 07:56 PM by Java Swing

    Architecture , Actions
  30. AWT vs. Swing

    by , 01-08-2012 at 01:23 PM
    Swing contains a rich set of components as compared to AWT. They are purely build in Java and has following benefits as compared to AWT.

    • Swing contains extra components like JTree, JTable etc.
    • They can change skin based on current look and feel.
    • They follow the MVC design pattern and are more flexible.
    • Swing provided light weight components.
    • Swing provides double buffering.
    • Swing provides paint debugging.

    Swing has following ...
    Tags: awt, swing Add / Edit Tags
Page 2 of 3 FirstFirst 123 LastLast