Skip to main content

Posts

Showing posts with the label Swing

Swing : Using HTML in button and Label text

You can decorate the buttons and label using the html text. You no need to call methods to set color, font size, font style etc for the Button and labels. Just specify them through the HTML tag. import  javax.swing. * ; import  java.awt. * ; class  HTMLButton {      public   static   void  main( String  ar[ ] )     {           String  html =          "<html><table border=1>"          + "<tr><td>One</td><td>1</td></tr>"          + "<tr><td>Two</td><td>2</td></tr>"          + "</table>" ;                   JFrame  fram...

String : Tabded Pane

Java has class javax.swing.JTabbedPane that is used to represent the tabbed pane. Tabbed pane is a container that has the more than one Panel attached with the tabbed name, where you can place the components. Each tabbed have their own components properties. You create tabs to JTabbedPane using the addTab( ). Each tab may contain other components. import  javax.swing. * ; import  java.awt. * ; import  java.awt.event. * ; class  TabbedPaneDemo {      public   static   void  main( String  ar[])     {          JFrame  frame = new   JFrame ( "Menus" );                   JTabbedPane  tp = new   JTabbedPane ();                   JPanel  p1 = new   JPanel ();   ...

Swing : DeskTop Pane

 In swing API, JDeskTopPane is the container used to create a display area where multiple-document may be placed. So virtually, it works like a desktop where we can place anther component. It has two properties public static final int LIVE_DRAG_MODE             this indicates that while dragging components, they should be inside the desktop. public static final int OUTLINE_DRAG_MODE  this indicates that component inside the desktop may be dragged out side of desktop pane. Internal Frame You can create graphical interface that has the windows insides another window. This is done in java by using JInternalFrame class. Internal frame can be moved and resized independently. This is used to create the MDI application. According to java specification, it has the following different types of constructors JInternalFrame ()           Creates a non-resizable, non-closabl...

Swing : Toolbars

Tool bar component is a container for button , images , labels that can be added to any Frame. It contains horizontally aligned components and can be dragged and drop in window. You can add more then one tool bar to your window. Often too bars are used to access functionality that is also listed in menus. This facility is provided by the class JToolBar that is in javax.swing packge. There are following methods and constructors use commonly. Constructors JToolBar () it creates the toolbar with HORIZONTAL orientation. JToolBar (int orientation)  it creates the toolbar with specified orientation. JToolBar (String name)  it creates the toolbar with specified name and HORIZONTAL orientation. JToolBar (String name, int orientation) it creates the toolbar with specified name and specified orientation. Methods add(Copoenent) adds component to tool bar addSeparator () adds separator of de...

Swing : Table

Table component in Java swing is created by the javax.swing. JTable class. This class is graphically representation of table. The graphical area is divided in to rows and columns and data are arranged into the cells.  Creating table import  javax.swing. * ; import  javax.swing.event. * ; import  java.awt. * ; import  javax.swing.border. * ; class  SwingDemo14 {      public   static   void  main( String  ar[])     {          JFrame  f = new   JFrame ( "Tool bar demo" );                   Object  colNames[] = { "Name" , "Phone" , "Address" };                   Object  rows[][] = {  { "ABC" , "345345" , "T-&*6,kdjhfkj" },   ...

Tree

To create three component int Java swing we use   JTree class of the package javax.swing used to create the tree. Using tree class you can create the hierarchical view of items. import  javax.swing. * ; import  javax.swing.event. * ; import  java.awt. * ; import  javax.swing.tree. * ; class  SwingDemo {      public   static   void  main( String  ar[])     {          JFrame  f = new   JFrame ( "Tool bar demo" );                   DefaultMutableTreeNode  a = new   DefaultMutableTreeNode ( "Info" );          DefaultMutableTreeNode  name = new   DefaultMutableTreeNode ( "Name" );          DefaultMutableTreeNode  phone = new   ...

JApplet

Like awt’s Applet swing also provides the class JApplet to create the applet that may contains the swing components. JApplet has some enhanced features. The JApplet class is the subclass of Applet. JApplet support for adding menus and menu items. Default layout out of JApplet is border layout while in Applet default layout is FlowLayout. Painting in JApplet is not as like Applet class. Paint code can not be placed directly to paint something. // // An example of the JApplet class.  For use with the applet.html file. // import  javax.swing. * ; import  javax.swing.border. * ; import  java.awt. * ; /*  *<applet code="JAppletDemo" width=300 height=250> </applet>  */ public   class  JAppletDemo  extends   JApplet  {    public   void  init() {      JPanel  p  =   new ...

Swing : Menu

JMenu is the standard menu that may have various JMenuItems. JMunu are placed into JMenuBar. and JMenuBar is added to the JFrame using setMenuBar method. you can add ActionListener to JMenuItem to deal with events.  import  javax.swing. * ; import  java.awt. * ; import  java.awt.event. * ; class  MenuDemo {      public   static   void  main( String  ar[])     {          JFrame  frame = new   JFrame ( "Menus" );                   JMenu  file = new   JMenu ( "File" );         file.setMnemonic( KeyEvent .VK_F);                            JMenu  edit = new   JMenu ( "Edit" );   ...

CardLayout

import  javax.swing. * ; import  java.awt. * ; import  java.awt.event. * ; class  CardLayoutDemo {      static   CardLayout  card;      static   JPanel  desk;      public   static   void  main( String  ar[])     {                   JPanel  p1 = new   JPanel ();p1.setBackground( new   Color (234,234,255));          JPanel  p2 = new   JPanel ();p2.setBackground( new   Color (200,200,200));          JPanel  p3 = new   JPanel ();p3.setBackground( new   Color (200,200,255));          JPanel  p4 = new   JPanel ();p4.setBackground( new   Color (100,200,255));     ...

BoxLayout in Java Swing

Box layout arranges the components either horizontal or vertical.   javax.swing package   has a class BoxLayout    that is used to create the BoxLayout managers and Boxes. You use many classes to arrange the components in more sophisticated way. Several supporting classes are typically used javax.swing.BoxLayout , javax.swing.Box , and javax.swing.Box.Filler .   there is following coonstructor to create the BoxLayout instance. BoxLayout (Container target, int axis) It creates a layout manager that will lay out components along the given axis. Here target is the container’s reference on which this Layout has to be applied and axis is the integer constant specifying the y-axis or x-axis. here Box is the another lightweight container that uses a BoxLayout object as its layout manager. Box provides several class methods that are useful for containers using BoxLayout -- even non-Box containers. To create an instance of this container we simply pass the de...