Showing posts with label Java - Applet JLabel. Show all posts
Showing posts with label Java - Applet JLabel. Show all posts

Friday, November 11, 2011

Align Icon in JLabel

    package org.best.example;
    /*
            Align Icon in JLabel Example
            This java example shows how to align icon displayed in JLabel
            using Java Swing JLabel class.
    */
    
    
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="JLabelWithIconTextExample" width=200 height=200>
    </applet>
    */
    
    public class JLabelIconAlignmentExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * Create an icon from an image using
                     * ImageIcon(String imagePath, String description)
                     * constructor.
                     */
                  
                    ImageIcon icon = new ImageIcon("images/copy.gif");
                  
                    /*
                     * To create an Image label and set horizontal alignment, use
                     * JLabel(Icon icon, int horizontalAlignment) or
                     * JLabel(String text, Icon icon, int horizontalAlignment)
                     *
                     * where horizontalAlignment can be one of the following SwingConstants,
                     * LEFT, CENTER, RIGHT, LEADING or TRAILING.
                     */
                  
                    //center aligned JLabel
                    JLabel imageLabel = new JLabel("Copy",icon, JLabel.CENTER);
                  
                    /*
                     * Add JLabel to an Applet
                     */
                  
                    add(imageLabel);
                  
                    /*
                     * Please note that the label is centered vertically in its display area.
                     */
            }
    }
 

Thursday, November 10, 2011

Check whether JLabel is Visible or Enabled

    package org.best.example;
    /*
            Check whether JLabel is Visible or Enabled Example
            This java example shows how to create check whether the JLabel is visible or
            enabled using Java Swing JLabel class.
    */
    
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="JLabelCheckVisibleOrEnableExample" width=200 height=200>
    </applet>
    */
    public class JLabelCheckVisibleOrEnabledExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel(String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("This is JLabel Example.");
                          
                    //add label to applet
                    add(label1);
                  
                    /*
                     * To check whether the JLabel is visible use,
                     * void isVisible(boolean visible)
                     * method.
                     */
                    boolean visible = label1.isVisible();
                  
                    /*
                     * To check whether the JLabel is enabled use,
                     * void isEnabled(boolean enable)
                     * method.
                     */
                    boolean enabled = label1.isEnabled();
                  
            }
          
    }

Wednesday, November 9, 2011

Create JLabel With Image Icon

    package org.best.example;
    /*
            Create JLabel With Image Icon Example
            This java example shows how to create a label with image icon using
            Java Swing JLabel class.
    */
         
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="JLabelWithIconExample" width=200 height=200>
    </applet>
    */
    
    public class JLabelWithIconExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * Create an icon from an image using
                     * ImageIcon(String imagePath, String description)
                     * constructor.
                     */
                  
                    ImageIcon icon = new ImageIcon("images/copy.gif");
                  
                    /*
                     * To create a JLabel with image icon use,
                     * JLabel(Icon icon)
                     * constructor of JLabel class.
                     */
                  
                    JLabel copyLabel = new JLabel(icon);
                  
                    /*
                     * Add label to an applet
                     */
                  
                    add(copyLabel);
                  
            }
    }

Tuesday, November 8, 2011

Create JLabel With Image Icon and Text

    package org.best.example;
    /*
            Create JLabel With Image Icon and Text Example
            This java example shows how to create a label with image icon
            and text using Java Swing JLabel class.
    */
    
    
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="JLabelWithIconTextExample" width=200 height=200>
    </applet>
    */
    
    public class JLabelWithIconTextExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * Create an icon from an image using
                     * ImageIcon(String imagePath, String description)
                     * constructor.
                     */
                  
                    ImageIcon icon = new ImageIcon("images/copy.gif");
                  
                    /*
                     * To create a JLabel with image icon and text use,
                     * JLabel(String text, Icon icon, int horizontalAlignment)
                     * constructor of JLabel class.
                     */
                  
                    JLabel copyLabel = new JLabel("Copy", icon, JLabel.CENTER);
                  
                    /*
                     * Add label to an applet
                     */
                  
                    add(copyLabel);
            }
    }

Monday, November 7, 2011

Create JLabel With Text

    package org.best.example;
    /*
            Create JLabel With Text Example
            This java example shows how to create a label with text using
            Java Swing JLabel class.
    */
    
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="JLabelWithTextExample" width=200 height=200>
    </applet>
    */
    
    public class JLabelWithTextExample extends JApplet{
    
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel(String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("This is JLabel Example.");
                          
                    //add label to applet
                    add(label1);
            }
    }

Sunday, November 6, 2011

Disable JLabel

    package org.best.example;
    /*
            Disable JLabel Example
            This java example shows how to disable JLabel using
            Java Swing JLabel class.
    */
    
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="DisableJLabelExample" width=200 height=200>
    </applet>
    */
    
    public class DisableJLabelExample extends JApplet{
    
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Disable Example.");
                          
                    //add label to applet
                    add(label1);
                  
                    /*
                     * To disable JLabel use,
                     * void setEnabled(boolean enabled)
                     * method.
                     */
                  
                    label1.setEnabled(false);
    
                    /*
                     * To re-enable the same JLabel use
                     * setEnabled(true) method.
                     */
            }
    }

Saturday, November 5, 2011

Get Horizontal Alignment of JLabel

    package org.best.example;
    /*
            Get Horizontal Alignment of JLabel Example
            This java example shows how to get horizontal alignment of label's content using
            Java Swing JLabel class.
    */
    
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="GetHorizontalAlignmenntExample" width=200 height=200>
    </applet>
    */
    
    public class GetHorizontalAlignmentExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel(String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Get Horizontal Alignment Example.");
                  
                    //add label to applet
                    add(label1);
                  
                  
                    /*
                     * To get horizontal alignment of label's content use,
                     * int getHorizontalAlignment()
                     * method of JLabel's class.
                     *
                     *
                     * Return value is one of the following constants,
                     * LEFT, CENTER, RIGHT, LEADING or TRAILING.
                     *
                     */
                  
                    int alignment = label1.getHorizontalAlignment();
                  
                    switch(alignment){
                  
                            case JLabel.LEFT:
                                    label1.setText("Left");
                                    break;
                            case JLabel.CENTER:
                                    label1.setText("Center");
                                    break;
                            case JLabel.RIGHT:
                                    label1.setText("Right");
                                    break;
                            case JLabel.LEADING:
                                    label1.setText("Leading");
                                    break;
                            case JLabel.TRAILING:
                                    label1.setText("Trailing");
                                    break;
                          
                    }
                  
            }
    }

Friday, November 4, 2011

Get JLabel's Text

    package org.best.example;
    /*
            Get JLabel's Text Example
            This java example shows how to get JLabel's text using getText method of
            Java Swing JLabel class.
    */
    
    
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="GetLabelTextExample" width=200 height=200>
    </applet>
    */
    
    public class GetLabelTextExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("This is JLabel Get Text Example.");
                          
                    //add label to applet
                    add(label1);
                  
                    /*
                     * To get text displayed in JLabel use,
                     * String getText()
                     * method of JLabel class.
                     */
                  
                    String text = label1.getText();
            }
          
    }

Thursday, November 3, 2011

Get X and Y Coordinates of JLabel

    package org.best.example;
    /*
            Get X and Y Coordinates of JLabel Example
            This java example shows how to get X and Y coordinates using
            Java Swing JLabel class.
    */
    
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="JLabelGetXYCoordinatesExample" width=200 height=200>
    </applet>
    */
    
    
    public class JLabelGetXYCoordinatesExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("This is JLabel Example.");
                          
                    //add label to applet
                    add(label1);
                  
                    /*
                     * To get X coordinate of JLabel use,
                     * int getX()
                     * method.
                     *
                     * To get Y coordinate of JLabel use,
                     * int getY()
                     * method.
                     */
                  
                    label1.setText(label1.getX() + ", " + label1.getY());
                  
            }
    }

Wednesday, November 2, 2011

Get or Set Gap Between JLabel's Icon and Text

    package org.best.example;
    /*
            Get or Set Gap Between JLabel's Icon and Text Example
            This java example shows how to get gap between JLabel's icon and text 
            using Java Swing JLabel class.
    */
    
    
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    /*
    <applet code="JLabelGapBetweenTextAndIconExample" width=200 height=200>
    </applet>
    */
    
    public class JLabelGapBetweenTextAndIconExample extends JApplet{
          
            public void init(){
                  
                    //create ImageIcon
                    ImageIcon icon = new ImageIcon("images/copy.gif");
                  
                    //create JLabel
                    JLabel copyLabel = new JLabel("Copy", icon, JLabel.CENTER);
                    add(copyLabel);
                    /*
                     * To determine gap between Icon and Text use,
                     * int getIconTextGap()
                     * method.
                     *
                     * Return value is the number of pixels between
                     * icon and text.
                     *
                     * Default gap between JLabel's text and Icon is
                     * 4 pixels.
                     */
                  
                    int gap = copyLabel.getIconTextGap();
                  
                    /*
                     * To set gap between JLabel's icon and text, use
                     * void setIconTextGap(int gap)
                     * method.
                     *
                     * where gap is number of pixels.
                     */
                  
                    copyLabel.setIconTextGap(50);
                                  
            }
          
    }

Tuesday, November 1, 2011

JLabel Disabled Icon

   package org.best.example;
    /*
            JLabel Disabled Icon Example
            This java example shows how to get and set icon when JLabel is disabled using
            setDisabled and getDisabled methods of Java Swing JLabel class.
    */
         
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;


    public class JLabelDisabledIconExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Disabled Icon Example.");
                          
                    //add label to applet
                    add(label1);
    
                    /*
                     * To set icon when particular JLabel is disabled use,
                     * void setDisabledIcon(Icon icon)
                     * method.
                     */
                    label1.setDisabledIcon(new ImageIcon("images/copy.gif"));
                  
                    /*
                     * To get Icon which is displayed when JLabel is disabled use,
                     * Icon getDisabledIcon()
                     * method of JLabel class.
                     */
                  
                    Icon icon = label1.getDisabledIcon();
            }
    }

Monday, October 31, 2011

JLabel Get Tooltip

   package org.best.example;
    /*
            JLabel Get Tooltip Example
            This java example shows how to get tooltip text for JLabel using
            getToolTipText method of Java Swing JLabel class.
    */    
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    public class JLabelGetTooltipExample extends JApplet{
                  
          
            public void init(){
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Get ToolTip Example.");
                          
                    //add label to applet
                    add(label1);
                  
                    /*
                     * To get ToolTip text of JLabel use,
                     * String getToolTipText()
                     * method of JLabel class.
                     */
                  
                    String toolTipText = label1.getToolTipText();
            }
    
    }

Sunday, October 30, 2011

JLabel Horizontal Text Position

   package org.best.example;
   /*
            JLabel Horizontal Text Position Example
            This java example shows how to set and get horizontal position of text relative to
            the image using Java Swing JLabel class.
    */         
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    public class JLabelHorizontalPositionExample extends JApplet{
    
            public void init(){
                    /*
                     * Create an icon from an image using
                     * ImageIcon(String imagePath, String description)
                     * constructor.
                     */
                  
                    ImageIcon icon = new ImageIcon("images/copy.gif");
                  
                    /*
                     * To create a JLabel with image icon and text use,
                     * JLabel(String text, Icon icon, int horizontalAlignment)
                     * constructor of JLabel class.
                     */
                  
                    JLabel copyLabel = new JLabel("Copy", icon, JLabel.CENTER);
                  
                    /*
                     * Add label to an applet
                     */
                  
                    add(copyLabel);
                  
                    /*
                     * To get horizontal position of JLabel's content relative to it's
                     * image use,
                     *
                     * int getHorizontalTextPosition()
                     * method of JLabel class.
                     *
                     * Return value is one of the following constants,
                     * LEFT, CENTER, RIGHT, LEADING or TRAILING.
                     */
                  
                    int position = copyLabel.getHorizontalTextPosition();
                  
                    /*
                     * To set horizontal position of text relative to it's image use,
                     * void setHorizontalTextPosition(int position)
                     * method of JLabel class.
                     *
                     * Possible value is one of the following constants,
                     * LEFT, CENTER, RIGHT, LEADING or TRAILING.
                     */
                  
                    copyLabel.setHorizontalTextPosition(JLabel.RIGHT);
            }
    }

Saturday, October 29, 2011

JLabel Set Font

   package org.best.example;
    /*
            JLabel Set Font Example
            This java example shows how to set custom font for JLabel using
            Java Swing JLabel class.
    */   
    
    import java.awt.Font;
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
     
    public class JLabelSetFontExample extends JApplet{
    
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Set Font Example.");
                          
                    /*
                     * To set custom font for JLabel use,
                     * void setFont(Font font)
                     * method.
                     */
                  
                    //create new Font
                    Font font = new Font("Courier", Font.BOLD,12);
                  
                    //set font for JLabel
                    label1.setFont(font);
                  
                    //add label to applet
                    add(label1);
            }
    }

Friday, October 28, 2011

JLabel Set Tooltop

   package org.best.example;
    /*
            JLabel Set Tooltop Example
            This java example shows how to set tooltip text for JLabel using
            setToolTipText method of Java Swing JLabel class.
    */
         
    import javax.swing.JApplet;
    import javax.swing.JLabel;
         
    public class JLabelSetToolTipExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Set Tooltip Example");
                          
                    //add label to applet
                    add(label1);
                  
                    /*
                     * To set tooltip for JLabel use,
                     * void setToolTipText(String tooltip)
                     * method of JLabel class.
                     */
    
                    label1.setToolTipText("Demo Tooltip for JLabel");
            }
    }

Thursday, October 27, 2011

Set Background Color of JLabel

   package org.best.example;
    /*
            Set Background Color of JLabel Example
            This java example shows how to set background color of JLabel using
            Java Swing JLabel class.
    */
         
    import java.awt.Color;
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    
    
    public class JLabelSetBackgroundColorExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Set Background Color Example.");
                          
                    //this will create light blue color
                    Color customColor = new Color(10,10,255);
                  
                    /*
                     * NOTE: By default JLabel is set to transparent mode. Calling setBackground
                     * directly will not set the background color of JLabel.
                     *
                     * In order to make the label set opaque, you need to call
                     * void setOpaque(boolean opaque)
                     * method.
                     */
                  
                    label1.setOpaque(true);
                  
                    /*
                     * To set background color of JLabel text use,
                     * void setBackground(Color color)
                     * method.
                     */
                  
                    label1.setBackground(customColor);
                  
                    //add label to applet
                    add(label1);
            }
    }



Wednesday, October 26, 2011

Set Foreground Color of JLabel

   package org.best.example;
    /*
            Set Foreground Color of JLabel Example
            This java example shows how to set foreground color of JLabel using
            Java Swing JLabel class.
    */
    
    
    import java.awt.Color;
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;




       public class JLabelSetForegroundColorExample extends JApplet{
    
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Set Forground Color Example.");
                          
                    /*
                     * To set foreground color of JLabel text use,
                     * void setForeground(Color color)
                     * method.
                     */
                  
                    //this will create light blue color
                    Color customColor = new Color(10,10,255);
                  
                    label1.setForeground(customColor);
                  
                    //add label to applet
                    add(label1);
            }
    }


Tuesday, October 25, 2011

Set Horizontal Alignment of JLabel

   package org.best.example;
    /*
            Set Horizontal Alignment of JLabel Example
            This java example shows how to set horizontal alignment of label's content using
            Java Swing JLabel class.
    */
    
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;

    
    public class SetHorizontalAlignmentExample extends JApplet{
    
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Set Horizontal Alignment Example.");
                  
                    //add label to applet
                    add(label1);
                  
                  
                    /*
                     * To set horizontal alignment of label's content use,
                     * void setHorizontalAlignment(int alignment)
                     * method of JLabel's class.
                     *
                     *
                     * Where value of alignment is one of the following constants,
                     * LEFT, CENTER, RIGHT, LEADING or TRAILING.
                     *
                     */
                  
                    label1.setHorizontalAlignment(JLabel.CENTER);
            }
    }

Monday, October 24, 2011

Set Icon for JLabel

   package org.best.example;
    /*
            Set Icon for JLabel Example
            This java example shows how to set image icon for JLabel using
            setIcon method of Java Swing JLabel class.
    */
    
    
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;


    public class SetJLabelIconExample extends JApplet{
          
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Set Icon Example.");
                          
                    //add label to applet
                    add(label1);
    
                    /*
                     * To set image icon for JLabel use,
                     * void setLabel(Icon icon)
                     * method of JLabel class.
                     */
                  
                    label1.setIcon(new ImageIcon("images/copy.gif"));
            }
    }

Sunday, October 23, 2011

Set JLabel Border

   package org.best.example;
    /*
            Set JLabel Border Example
            This java example shows how to create a border for JLabel using
            Java Swing JLabel class.
    */
    
    
    import java.awt.Color;
    import javax.swing.BorderFactory;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.border.Border;
    

    public class JLabelSetBorderExample extends JApplet{
    
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Create Border Example.");
                          
                    //add label to applet
                    add(label1);
                  
                    /*
                     * Create new custom border for JLabel
                     */
                    Border border = BorderFactory.createLineBorder(Color.blue);
                  
                    /*
                     * To set JLabel's border use,
                     * void setBorder(Border b)
                     * method.
                     */
                  
                    label1.setBorder(border);
            }
    }