View RSS Feed

Java Swing

Java Swing tutorials.

  1. Internal Frames Performance tip

    by , 04-26-2012 at 08:55 PM
    When various internal frames are present in a desktop, it might be noticed by the user that they move slowly. This problem could be avoided by using outline dragging. With help of outline dragging, just internal frame’s outline is painted at the current position of mouse while internal frame dragging. Innards of internal frames couldn’t be repainted, at new positions, unless dragging is stopped. Default behavior known as live dragging is to repaint or reposition few or all internal frames in a continuous ...
  2. Rules of Using Internal Frames

    by , 04-26-2012 at 08:54 PM
    If any programs have been built by the help of JFrame or other components of the Swing, then already you are having enough knowledge that how internal frames could be used. Rules to use the internal frames have been summarized in the given list:

    Internal frame’s size shall be set:
    If internal frame’s size is not set, then it will never become visible due to its zero size. Size can be obtained by one of the given methods: setBounds, pack or setSize.

    Internal frame’s ...
    Categories
    JFrame
  3. Internal Frames vs. Regular Frames

    by , 04-26-2012 at 08:53 PM
    Code that is used for internal frames and regular swing frames is very similar, in variety of ways. Root panes are present in internal frames which are used to setup GUIs, for JInternalFrame that is quite same as the GUI setup, for JFrame. Other APIs are also provided by the JInternalFrames which makes it just like JFrame.

    Internal frames are not top level or windows container, but this makes them to differ from frames. Let us say for example, an internal frame shall be added to a ...
  4. How to Use Internal Frames

    by , 04-26-2012 at 08:52 PM
    A JFrame could be displayed by the JInternalFrame. Usually internal frames are added to the the desktop. Desktop pan might be used for the JFrame’s content pane. Desktop is JDesktopPane’s instance that is basically the JLayeredPane subclass which made API addition for management of various overlapped internal frames.

    Carefully it shall be considered that either GUI of your program could be based around internal frames or frames. To make a switch from internal frames towards frames, ...
    Categories
    JFrame
  5. Responding to Window-Closing Events

    by , 04-26-2012 at 08:51 PM
    When a frame onscreen is closed by a user, frame gets hidden by defauly. Though it is visible, still the frame is present and it might be made visible by the program once again. If different behavior is needed on your partm then either get registered the window listener which controls the window closings or default close behavior could be specified by the help of method setDefaultCloseOperation. Both of them could also be done.

    Given values shall be there for setDefaultCloseOperation ...
    Categories
    JFrame
  6. Creating and Showing Frames

    by , 04-26-2012 at 08:50 PM
    Extremely plain window’s picture is given below that is made by the demonstration application FrameDemo. Source code could be found in FrameDemo.java. FrameDemo could be run (downloading JDK6, or later.)

    How to setup a frame has been shown in the given code of FrameDemo.

    Java Code:
    //1. Create the frame.
    JFrame frame = new JFrame("FrameDemo");
    
    //2. Optional: What happens when the frame closes?
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ...
    Categories
    JFrame
  7. How to Make Frames (Main Windows)

    by , 04-26-2012 at 08:50 PM
    Frame is considered to be a top leveled window that has a border and a title. Frame size consist of any area which has been designated for sake of border. Border area dimensions might be obtained by method getInsets. Border area is also present in frame’s overall size so frame’s portion is obscured effectively, which constraint the area present to render or display the subcomponents, to that rectangle which consists of location of upper left corner (insets.left, insets.top), and width size (insets.left ...
    Tags: main windows Add / Edit Tags
    Categories
    JFrame
  8. Wide JTables

    by , 04-26-2012 at 08:49 PM
    What could be done in case there are too wider JTables present?

    • They could be placed in JScrollPane so complete table gets scrolled.
    • Column’s width could be changed by the user so that the date could be squeezed into the space present, which would widen column, as per requirements.
    • Shortcuts could be given to the users for configuration of width constellations of the multiple columns.
    • If 2 or 3 rows are attempted to be used per data item, it gets awkward. A perfect grid would
    ...
    Tags: wide jtables Add / Edit Tags
    Categories
    JTable
  9. How JTables Work

    by , 04-26-2012 at 08:48 PM
    One of the most important things that shall be made understood is that data gets live just in DataModel. Rendered data is that one which is visible currently. No giant sort of bit map scrolling of the screen is present. Correspondent grid of the JComponents is also not present. Not a single JComponent’s row is even present. All of the cells are rendered by the TableCellRenderer which recycles the similar JComponent. New rows are brought forward from the DataModel when it is scrolled by the user. ...
    Categories
    JTable
  10. JTable Classes and Interfaces

    by , 04-26-2012 at 08:47 PM
    JTable specified functions could be present in JTable class or other classes which are present in package javax.swing.table. Majority of the programs present shall be importing both classes present, as following:
    Java Code:
    import javax.swing.*;
     import javax.swing.table.*;
    It shall be noted that scheme of package naming that is present in Swing change when JFC version1.1 came. Before this, com.sun.java.swing was used to start the swing packages. For this JFC VERSION1.1, ...
    Categories
    JTable
  11. JTable Printing

    by , 04-26-2012 at 08:47 PM
    Simple API is provided by the JTable to print the tables. Priting of the table could easily be done by invoking JTable.print, with no arguments:
    Java Code:
    try {
        if (! table.print()) {
            System.err.println("User cancelled printing");
        }
    } catch (java.awt.print.PrinterException e) {
        System.err.format("Cannot print %s%n", e.getMessage());
    }
    At normal swing, print is invoked that will bring the ...
    Categories
    JTable
  12. Specifying Tool Tips for Cells

    by , 04-26-2012 at 08:46 PM
    Tool tip text that has been displayed is found out for a table cell by the renderer of cell, by default. But it might sometimes much simple thing to make specification of the tool tip text, by overriding the implementation of JTable of the method getToolTipText. How to use these 2 techniques have been shown in this section.

    For addition of the tool tip in cell by renderer, first thing that needs to be done is to create or get the cell renderer. After this make it sure that JComponent ...
    Categories
    JTable
  13. Using Custom Renderers

    by , 04-26-2012 at 08:45 PM
    This section shows how cell renderer could be specified or created. “Type specific” cell renderer could be set by the help of JTable setDefaultRenderer method. Cells could be specified in a specific column by using a renderer. However, TableColumn method setCellRenderer is used usually by people. A “Cell specific” renderer could be specified by creation of subclass JTable.
    Image or text could easily be customized that are rendered by DefaultTableCellRenderer. Just one subclass is created ...
    Categories
    JTable
  14. Creating a Table Model

    by , 04-26-2012 at 08:45 PM
    A table model object is used by every table object for management of the actual data of the table. Interface TableModel shall be implemented by the table model object. If table model object is not provided by the programmer them DefaultTableModel instance is automatically created by the the JTable. This kind of relationship is given below:

    SimpleTableDemo uses the JTable constructor to create table model, along with code:
    Java Code:
    new AbstractTableModel() {
        public
    ...
    Categories
    JTable
  15. Setting and Changing Column Widths of JTable

    by , 04-26-2012 at 08:44 PM
    All tables which are there in a table by default start with equivalent width and columns are automatically filling up the whole table’s width. When table has become narrow or wide, all widths of column would be changed appropriately.


    When a column is resized by dragging the right border then size of the table or the size of the column must change. Size of the table remains similar by default & all columns which are right to the drag point would be resized so that space ...
    Categories
    JTable
  16. Adding a Table to a Container

    by , 04-26-2012 at 08:43 PM
    Scroll pane creation has a typical code which servers to be a container, for a table.

    Java Code:
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    In this snippet, 2 lines would be doing the given:

    • Along with an argument, constructor JScrollPane would be invoked which will be referring to the table objects. A scroll pane would be created as a container for sake of table; automatically addition of the table ...
    Categories
    JTable
  17. JTable Introduction

    by , 04-26-2012 at 08:42 PM
    Many facilities are present in a JTable which makes the rendering and editing possible. But defaults are also provided for such features hence to setup the tables very easily. For instance, a table could be setup with 10 columns and 10 rows of numbers.
    Java Code:
          TableModel dataModel = new AbstractTableModel() {
              public int getColumnCount() { return 10; }
              public int getRowCount() { return 10;}
              public Object getValueAt(int row, int col)
    ...
    Categories
    JTable
  18. Closer Look at Swing. Where is MVC?

    by , 04-26-2012 at 08:42 PM
    Model delegate which a common MVC flavor is being implemented by the internally swing, which coalesces controller and view to UI delegate. Rarely, developers deal with controller or view part while applications are being written. Instead they deal with components.Swing components are not at all the part of MVC but they are considered as mediators present b/w view, model and controller.

    Custom coding is involved in building of the components and model creations to communicate to the ...
    Categories
    MVC
  19. Interaction Between MVC Components

    by , 04-26-2012 at 08:41 PM
    In this section, a closer look will be taken to do the implementation of Fig. 1, in context of the application which is present in Java SE 6. Once view, model and controller objects get instantiated, this will happen:

    1. At the model, view is being registered as listener. Changes being done to the model’s data would be instantly resulting into a change notification that will be received by the view. This is the most common example of “push model”. It shall be noted that model doesn’t ...
    Categories
    MVC
  20. What Is Model-View-Controller (MVC)?

    by , 04-26-2012 at 08:40 PM
    In past years if you have done programming with the libraries of GUI then you must would have come across the MVC design i.e model view controller design. It was firstly introduced by the Trygve Reenskaug who was a developer at XPAR (Xerox Palo Alto Research Center ) in 1979. He used to decouple the business logic and data access , in a manner which then is displayable to the user.MVC consists of 3 basic elements, which are given below:

    • Model - Rules and the data are presented ...
    Categories
    MVC
  21. How to Write a Mouse-Wheel Listener

    by , 04-26-2012 at 08:39 PM
    When mouse’s wheel get rotated, it is been notified by the mouse wheel events. All mice don’t have wheels and in those cases generation of mouse wheel events is never done. No other way is present to detect programmatically that whether mouse wheel is present in a mouse or not.

    MouseAdapter AWT class shall be used alternatively that would be implementing the interface MouseWheelListener, for creation of the MouseWheelEvent and overriding the specific event methods.
    It is not ...
    Categories
    Actions
  22. How to Write a Mouse Listener

    by , 04-26-2012 at 08:38 PM
    To do interaction with a component, mouse is used by the user and this is notified by the mouse events. Usually mouse events take place when cursor gets entered or exit from onscreen area of the component and when button of mouse is pressed or released.

    To track down the motion of cursor usually involve more system;s overheads as compared to tracking of the other events. This is the reason why mouse motion events have been placed separately in Mouse motion, listener type.
    ...
    Categories
    Actions
  23. How to Write a List Selection Listener

    by , 04-26-2012 at 08:37 PM
    When selection of table or list is changed or is changing, then list selection event takes place. From an object, these list selection events get fired which result in implementation of interface ListSelectionModel. To get the list selection model of the table, either use getColumnModel().getSelectionModel() or getSelectionModel method.

    For detection of the list selection events, register listener at the list selection model which is appropriate. An option to register the listener ...
    Categories
    Actions
  24. How to Write a Key Listener

    by , 04-26-2012 at 08:33 PM
    Key events are being indicated when user starts typing keyboard. Key events are specifically fired with keyboard focus by the component when keyboard keys are pressed or releaserd. “How Focus Subsytem could be used” shall be read for more information regarding focus.

    Regarding 2 key events, the notifications are being sent.

    • Unicode character typing
    • To pressor release a key present at the keyboard

    First event is known as key typed event. Second ...
    Tags: key listener Add / Edit Tags
    Categories
    Actions
  25. How to Write an Item Listener

    by , 04-26-2012 at 08:32 PM
    Components fire the item events which are involved in implementation of the interface ItemSelectable. ItemSelectable components usually maintain off and on state for the items. Swing components involved in firing items include certain buttons, for example, check menu items, combo boxes, check boxes and toggle buttons etc.
    Code for the item event handling that has been taken by ComponentEventDemo.java.

    Java Code:
    //where initialization occurs
    checkbox.addItemListener(this);
    ...
    Categories
    Actions
  26. How to Write an Internal Frame Listener

    by , 04-26-2012 at 08:31 PM
    WindowListener and InternalFrameListener are similar. Just like the window listener, events are listened by the internal frame listeners which take place as window is being shown for very 1st time or are iconified, disposed of, activated, deiconified or deactivated.

    Following figure shows the application in which internal frame events have been demonstrated. Internal frame events are being listened by the application by the Event Generator frame, which display messages describing ...
    Categories
    Actions
  27. How to Write a Focus Listener

    by , 04-26-2012 at 08:30 PM
    When keyboard focus is being gained or lost by the component, focus events usually get fired. This is applicable no matter change in focus would be occurring via keyboard, mouse or programmatically.

    Here is will be explained that how focus events could be obtained for a component by making a FocusListener instance registered at it. Only for window, focus can be obtained by implementation of the WindowFocusListener instance. For many components implementation of the PropertyChangeListener ...
    Categories
    Actions
  28. How to Write a Document Listener

    by , 04-26-2012 at 08:29 PM
    A document is used by the swing text component for representation of its components. When document’s content changes, document events take place. A document listener is attached to the text of document of the component instead of the text component, itself.

    Document events are demonstrated by the given example, at 2 text components.

    Name:  1.jpg
Views: 585
Size:  16.1 KB
    Document Listener
    Categories
    Actions
  29. How to Write a Container Listener

    by , 04-26-2012 at 08:27 PM
    A container is involved in firing the Container events; right after the component gets added/removed. Such kinds of events are just for sake of notifications. Container listeners are not needed to be there for adding or removing the components successfully.

    Container events are demonstrated by the given example. Click at ADD A BUTTON /Remove Button to add or remove buttons from the panel which is present at the window’s bottom. Everytime button is added/removed, container event is ...
    Categories
    Actions
  30. How to Write a Component Listener

    by , 04-26-2012 at 08:26 PM
    Component listener is considered as a listener interface to receive the component events. Component is that object which has a graphical presentation which might be displayed at screen or may also interact with some user. Few component examples are scrollbars, checkboxes and buttons of graphical interface.

    Class involved in component event processing will be implementing this interface along with all of the contained methods or will be extending the abstract ComponentAdapter. Listener ...
    Categories
    Actions
Page 1 of 3 123 LastLast