Skip to Content

JLabel Horizontal Text Position Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.         JLabel Horizontal Text Position Example
  3.         This java example shows how to set and get horizontal position of text relative to
  4.         the image using Java Swing JLabel class.
  5. */
  6.  
  7.  
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JApplet;
  10. import javax.swing.JLabel;
  11.  
  12. /*
  13. <applet code="JLabelHorizontalPositionExample" width=200 height=200>
  14. </applet>
  15. */
  16. public class JLabelHorizontalPositionExample extends JApplet{
  17.  
  18.         public void init(){
  19.                 /*
  20.                  * Create an icon from an image using
  21.                  * ImageIcon(String imagePath, String description)
  22.                  * constructor.
  23.                  */
  24.                
  25.                 ImageIcon icon = new ImageIcon("images/copy.gif");
  26.                
  27.                 /*
  28.                  * To create a JLabel with image icon and text use,
  29.                  * JLabel(String text, Icon icon, int horizontalAlignment)
  30.                  * constructor of JLabel class.
  31.                  */
  32.                
  33.                 JLabel copyLabel = new JLabel("Copy", icon, JLabel.CENTER);
  34.                
  35.                 /*
  36.                  * Add label to an applet
  37.                  */
  38.                
  39.                 add(copyLabel);
  40.                
  41.                 /*
  42.                  * To get horizontal position of JLabel's content relative to it's
  43.                  * image use,
  44.                  *
  45.                  * int getHorizontalTextPosition()
  46.                  * method of JLabel class.
  47.                  *
  48.                  * Return value is one of the following constants,
  49.                  * LEFT, CENTER, RIGHT, LEADING or TRAILING.
  50.                  */
  51.                
  52.                 int position = copyLabel.getHorizontalTextPosition();
  53.                
  54.                 /*
  55.                  * To set horizontal position of text relative to it's image use,
  56.                  * void setHorizontalTextPosition(int position)
  57.                  * method of JLabel class.
  58.                  *
  59.                  * Possible value is one of the following constants,
  60.                  * LEFT, CENTER, RIGHT, LEADING or TRAILING.
  61.                  */
  62.                
  63.                 copyLabel.setHorizontalTextPosition(JLabel.RIGHT);
  64.         }
  65. }