{"id":991,"date":"2021-01-12T21:18:45","date_gmt":"2021-01-12T21:18:45","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=991"},"modified":"2021-01-12T21:31:00","modified_gmt":"2021-01-12T21:31:00","slug":"borders","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/borders\/","title":{"rendered":"Borders"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Often, you would like to enclose a set of visual components within a&nbsp;<strong><em>border<\/em><\/strong>&nbsp;of some kind.&nbsp; For example, it is often helpful for a set of radio buttons in a single button group to be enclosed in a &#8220;<em>group box<\/em>&#8220;, often with a title of some kind to help the user understand what&#8217;s expected of him\/her.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Note: <\/em><\/strong>&nbsp;This kind of border has nothing to do with the&nbsp;<code><strong>BorderLayout<\/strong><\/code>&nbsp;class.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Swing provides a number of border choices.&nbsp; Here is an example that shows you what they look like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [11,17,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,60,61,62,63,64,65,66,96,97,98,99,100,119]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.BorderLayout;\nimport java.awt.Color;\nimport java.awt.Container;\nimport java.awt.Insets;\n\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\n\nimport javax.swing.BorderFactory;\nimport javax.swing.ButtonGroup;\nimport javax.swing.JFrame;\nimport javax.swing.JPanel;\nimport javax.swing.JRadioButton;\n\nimport javax.swing.border.Border;\n\nclass BordersPanel extends JPanel\n{\n  public BordersPanel()\n  {\n    setLayout( new BorderLayout() );\n    add(new OuterPanel(), BorderLayout.CENTER);\n    add(new InputPanel(), BorderLayout.SOUTH);\n  }\n  \n  public void setPanelBorder()\n  {\n    Border border = null;\n    if (m_borderCommand.equals(LOWERED_BEVEL))\n    {\n      border = BorderFactory.createLoweredBevelBorder();\n    }\n    else if (m_borderCommand.equals(RAISED_BEVEL))\n    {\n      border = BorderFactory.createRaisedBevelBorder();\n    }\n    else if (m_borderCommand.equals(ETCHED))\n    {\n      border = BorderFactory.createEtchedBorder();\n    }\n    else if (m_borderCommand.equals(LINE))\n    {\n      border = BorderFactory.createLineBorder(Color.blue);\n    }\n    else if (m_borderCommand.equals(MATTE))\n    {\n      border = BorderFactory.createMatteBorder(3, 3, 3, 3,\n          Color.darkGray);\n    }\n    else if (m_borderCommand.equals(EMPTY))\n    {\n      border = BorderFactory.createEmptyBorder();\n    }\n    m_borderPanel.setBorder(border);\n  }\n  \n  \/\/\/\/\/\/\/ Private data \/\/\/\/\/\n  private static final String LOWERED_BEVEL = &quot;Lowered bevel&quot;;\n  private static final String RAISED_BEVEL = &quot;Raised bevel&quot;;\n  private static final String ETCHED = &quot;Etched&quot;;\n  private static final String LINE = &quot;Line&quot;;\n  private static final String MATTE = &quot;Matte&quot;;\n  private static final String EMPTY = &quot;Empty&quot;;\n  private String m_borderCommand = EMPTY; \/\/ Current border style\n  \n  private BorderPanel m_borderPanel = new BorderPanel();\n  \n  \/\/\/\/\/\/\/ Inner classes \/\/\/\/\/\n  class OuterPanel extends JPanel\n  {\n    public OuterPanel()\n    {\n      setBackground(Color.pink);\n      setLayout( new BorderLayout() );\n      add(m_borderPanel, BorderLayout.CENTER);\n    }\n    \n    public Insets getInsets()\n    {\n      return new Insets(10, 10, 10, 10);\n    }\n  }\n  \n  class BorderPanel extends JPanel\n  {\n  }\n  \n  class InputPanel extends JPanel \n      implements ActionListener\n  {\n    public InputPanel()\n    {\n      setBackground(Color.cyan);\n      Border etched = BorderFactory.createEtchedBorder();\n      Border titled =\n          BorderFactory.createTitledBorder(\n                                etched, &quot;Choose Border:&quot;);\n      setBorder(titled);\n      \n      addRadioButton(LOWERED_BEVEL);\n      addRadioButton(RAISED_BEVEL);\n      addRadioButton(ETCHED);\n      addRadioButton(LINE);\n      addRadioButton(MATTE);\n      addRadioButton(EMPTY).setSelected(true);\n    }\n    \n    public void actionPerformed(ActionEvent ev)\n    {\n      m_borderCommand = m_group.getSelection().getActionCommand();\n      setPanelBorder();\n    }\n    \n    private JRadioButton addRadioButton(String command)\n    {\n      JRadioButton button = new JRadioButton(command);\n      button.setOpaque(false);\n      add(button);\n      m_group.add(button);\n      button.addActionListener(this);\n      button.setActionCommand(command);\n      return button;\n    }\n    \n    \/\/\/\/\/\/\/\/\/ Private data \/\/\/\/\/\/\/\/\n    private ButtonGroup m_group = new ButtonGroup();\n  }\n}\n\nclass BordersFrame extends JFrame\n{\n  public BordersFrame()\n  {\n    setTitle(&quot;Borders&quot;);\n    setSize(500, 150);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n    Container contentPane = getContentPane();\n    contentPane.add( new BordersPanel() );\n  }\n}\n\npublic class Borders\n{\n  public static void main(String&#x5B;] args)\n  {\n    BordersFrame frame = new BordersFrame();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which produces:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"500\" height=\"150\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/BordersEmpty.gif\" alt=\"\" class=\"wp-image-995\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"500\" height=\"150\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/BordersLoweredBevel.gif\" alt=\"\" class=\"wp-image-996\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"500\" height=\"150\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/BordersRaisedBevel.gif\" alt=\"\" class=\"wp-image-997\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"150\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/BordersEtched.gif\" alt=\"\" class=\"wp-image-998\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"150\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/BordersLine.gif\" alt=\"\" class=\"wp-image-999\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"150\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/BordersMatte.gif\" alt=\"\" class=\"wp-image-1000\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Points to Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I used&nbsp;<code><strong>Insets<\/strong><\/code>&nbsp;to produce a margin in the upper pane, so that the results could more easily be seen.<\/li><li>I applied&nbsp;<code><strong>setOpaque(false)<\/strong><\/code>&nbsp;to each of the radio buttons.&nbsp; If I had not done so, the results would have looked like this:<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"150\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/BordersOpaque.gif\" alt=\"\" class=\"wp-image-1001\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:31px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Not nice looking!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often, you would like to enclose a set of visual components within a&nbsp;border&nbsp;of some kind.&nbsp; For example, it is often helpful for a set of radio buttons in a single button group to be enclosed in a &#8220;group box&#8220;, often with a title of some kind to help the user understand what&#8217;s expected of him\/her. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":71,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-991","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Often, you would like to enclose a set of visual components within a&nbsp;border&nbsp;of some kind.&nbsp; For example, it is often helpful for a set of radio buttons in a single button group to be enclosed in a &#8220;group box&#8220;, often with a title of some kind to help the user understand what&#8217;s expected of him\/her.&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=991"}],"version-history":[{"count":6,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/991\/revisions"}],"predecessor-version":[{"id":1006,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/991\/revisions\/1006"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/71"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}