{"id":32168,"date":"2016-01-22T11:00:49","date_gmt":"2016-01-22T09:00:49","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=32168"},"modified":"2016-01-21T11:19:39","modified_gmt":"2016-01-21T09:19:39","slug":"java-swing-slider-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/","title":{"rendered":"Java Swing Slider example"},"content":{"rendered":"<h2>1. Introduction<\/h2>\n<p>In this article, we&#8217;ll discuss Java swing slider. In Java Swing, slider is normally constructed by a <code>JSlider<\/code> component. <code>JSlider<\/code> is a component that lets the users select a value by sliding a knob within a specified interval. For the knob, it always points to the point which matches the integer values within the interval.<\/p>\n<p>For <code>JSlider<\/code>, six different constructors can be used:<\/p>\n<ul>\n<li>JSlider(): creates a horizontal slider ranging from 0 to 100 with the initial value to be 50.<\/li>\n<li>JSlider(BoundedRangeModel brm): creates a horizontal slider using the specified BounedRangeModel.<\/li>\n<li>JSlider(int orientation): creates a slider using the specified orientation ranging from 0 to 100, with the initial value to be 50.<\/li>\n<li>JSlider(int min, int max): creates \u00a0a slider using the specified min and max value, with a initial value equals to the average of the min plus the max.<\/li>\n<li>JSlider(int min, int max, int value): creates a horizontal slider using the specified min, max and the initial value.<\/li>\n<li>JSlider(int orientation, int min, int max, int value): very similar to the above one, with the orientation specified.<\/li>\n<\/ul>\n<p>For detailed constructors and methods within the <code>JSlider<\/code> class, you can refer to the <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/JSlider.html\" target=\"_blank\">Oracle official document<\/a>.<\/p>\n<h2>2. Examples on Java swing slider<\/h2>\n<p>For the following example parts on <code>JSlider<\/code>, Java 8 and Eclipse IDE (version Mars 4.5.0) are used.<\/p>\n<h3>2.1 Simple examples on Java swing slider<\/h3>\n<p>In this part, different settings on JSlider will be discussed.<\/p>\n<p>JSlider() construct a horizontal slider, ranging from 0 to 100, with the initial value points to 50. It can be constructed by:<\/p>\n<pre class=\"brush:java\">JSlider slider = new JSlider();\r\n<\/pre>\n<p>The figure below shows the result for run the above code:<\/p>\n<p><figure id=\"attachment_32200\" aria-describedby=\"caption-attachment-32200\" style=\"width: 520px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/slider.jpg\" rel=\"attachment wp-att-32200\"><img decoding=\"async\" class=\"wp-image-32200 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/slider.jpg\" alt=\"slider\" width=\"520\" height=\"122\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/slider.jpg 520w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/slider-300x70.jpg 300w\" sizes=\"(max-width: 520px) 100vw, 520px\" \/><\/a><figcaption id=\"caption-attachment-32200\" class=\"wp-caption-text\">Default slider<\/figcaption><\/figure><\/p>\n<p>Also, we can set the orientation of the slider to be either vertical or horizontal, with <code>JSlider.VERTICAL<\/code> or <code>JSlider.HORIZONTAL<\/code> being specified respectively.<\/p>\n<p>For example, a vertical or horizontal slider can be constructed by:<\/p>\n<pre class=\"brush:java\">JSlider slider = new JSlider(JSlider.VERTICAL);\r\nJSlider slider = new JSlider(JSlider.HORIZONTAL);\r\n<\/pre>\n<p>The result is shown in figure below:<\/p>\n<p><figure id=\"attachment_32199\" aria-describedby=\"caption-attachment-32199\" style=\"width: 458px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderVertical.jpg\" rel=\"attachment wp-att-32199\"><img decoding=\"async\" class=\"wp-image-32199 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderVertical.jpg\" alt=\"sliderVertical\" width=\"458\" height=\"440\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderVertical.jpg 458w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderVertical-300x288.jpg 300w\" sizes=\"(max-width: 458px) 100vw, 458px\" \/><\/a><figcaption id=\"caption-attachment-32199\" class=\"wp-caption-text\">Slider in Vertical<\/figcaption><\/figure><\/p>\n<p>In addition, the min, max and initial value can be specified by having different parameters when constructing slider. For example, we can set the min, max and initial value to be -100, 100 and 50 with following setting:<\/p>\n<pre class=\"brush:java\">JSlider slider = new JSlider(-100, 100, 50);\r\n<\/pre>\n<p>The figure below shows that the range is from -100 to 100, and the initial tick is pointing to 50:<\/p>\n<p><figure id=\"attachment_32197\" aria-describedby=\"caption-attachment-32197\" style=\"width: 494px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderWith50.jpg\" rel=\"attachment wp-att-32197\"><img decoding=\"async\" class=\"wp-image-32197 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderWith50.jpg\" alt=\"sliderWith50\" width=\"494\" height=\"124\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderWith50.jpg 494w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderWith50-300x75.jpg 300w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/a><figcaption id=\"caption-attachment-32197\" class=\"wp-caption-text\">Slider in position 50<\/figcaption><\/figure><\/p>\n<p>Also, we can use the <code>BoundedRangeModel<\/code> to specify the min, max, extent and initial value of the slider. For instance, the code below makes a slider ranging from 1 to 100, specifying the initial value to be 20 and the extent to be 0:<\/p>\n<pre class=\"brush:java\">DefaultBoundedRangeModel model = new DefaultBoundedRangeModel(20, 0, 1, 100);\r\nJSlider slider = new JSlider(model);\r\n<\/pre>\n<p>The figure below shows the slider after running code above:<\/p>\n<p><figure id=\"attachment_32198\" aria-describedby=\"caption-attachment-32198\" style=\"width: 492px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitial.jpg\" rel=\"attachment wp-att-32198\"><img decoding=\"async\" class=\"wp-image-32198 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitial.jpg\" alt=\"sliderInitial\" width=\"492\" height=\"128\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitial.jpg 492w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitial-300x78.jpg 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><\/a><figcaption id=\"caption-attachment-32198\" class=\"wp-caption-text\">Slider in 20<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h3>2.2 Adding ticks into Java swing slider<\/h3>\n<p>In most situation, it&#8217;s convenient to add ticks into the slider, making it clearer. In Java swing, the method <code>setMajorTickSpacing<\/code> is used for this purpose. With a int value parameter n being the input, it specifies the major tick spacing between the interval. For example, with n set by 25 and the default setting for the slider, you will get major ticks with following values: 0, 25, 50, 75, 100. The code below shows how to add the major ticks:<\/p>\n<pre class=\"brush:java\">slider.setMajorTickSpacing(25);\r\nslider.setPaintTicks(true);\r\n<\/pre>\n<p>The result can be obtained and shown below:<\/p>\n<p><figure id=\"attachment_32196\" aria-describedby=\"caption-attachment-32196\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMajorTick.jpg\" rel=\"attachment wp-att-32196\"><img decoding=\"async\" class=\"wp-image-32196 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMajorTick.jpg\" alt=\"sliderMajorTick\" width=\"480\" height=\"136\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMajorTick.jpg 480w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMajorTick-300x85.jpg 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-32196\" class=\"wp-caption-text\">Slider with major tick<\/figcaption><\/figure><\/p>\n<p>Note that we need to add the method <code>setPaintTicks<\/code> to be true determine that tick marks are painted on the slider, as the default value is false.<\/p>\n<p>In contrast, the minor ticks can be obtained by using the method <code>setMinorTickSpacing<\/code>. With a int value parameter n being the input, the minor tick spacing can be obtained. For example, if we set the parameter to be 10 with the default setting for the slider, minor ticks will be obtained with following values: 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, with the code below:<\/p>\n<pre class=\"brush:java\">slider.setMinorTickSpacing(10);\r\nslider.setPaintTicks(true);\r\n<\/pre>\n<p><figure id=\"attachment_32194\" aria-describedby=\"caption-attachment-32194\" style=\"width: 486px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMinorTick1.jpg\" rel=\"attachment wp-att-32194\"><img decoding=\"async\" class=\"wp-image-32194 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMinorTick1.jpg\" alt=\"sliderMinorTick1\" width=\"486\" height=\"136\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMinorTick1.jpg 486w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderMinorTick1-300x84.jpg 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/a><figcaption id=\"caption-attachment-32194\" class=\"wp-caption-text\">Slider with minor tick<\/figcaption><\/figure><\/p>\n<p>However, in most situation, we need to specify the major and minor ticks together, which is very easy. What you only need to do is to combine the code above and set the major and minor ticks together, such as:<\/p>\n<pre class=\"brush:java\">slider.setMajorTickSpacing(50);\r\nslider.setMinorTickSpacing(10);\r\nslider.setPaintTicks(true);\r\n<\/pre>\n<p>For this situation, we can run the code and get the following slider:<\/p>\n<p><figure id=\"attachment_32195\" aria-describedby=\"caption-attachment-32195\" style=\"width: 464px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/siderMinorTick.jpg\" rel=\"attachment wp-att-32195\"><img decoding=\"async\" class=\"wp-image-32195 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/siderMinorTick.jpg\" alt=\"siderMinorTick\" width=\"464\" height=\"136\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/siderMinorTick.jpg 464w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/siderMinorTick-300x88.jpg 300w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/a><figcaption id=\"caption-attachment-32195\" class=\"wp-caption-text\">Slider with major and minor tick<\/figcaption><\/figure><\/p>\n<h3>2.3 Labeling positions in slider<\/h3>\n<p>In some situations, we need to know the positions of some values. In this condition, we can add the positions and label them in the slider. Here we use a hash table to map the value with a label with following code:<\/p>\n<pre class=\"brush:java\">\/\/ Set the labels to be painted on the slider\r\nslider.setPaintLabels(true);\r\n\t\t\r\n\/\/ Add positions label in the slider\r\nHashtable position = new Hashtable();\r\nposition.put(0, new JLabel(\"0\"));\r\nposition.put(25, new JLabel(\"25\"));\r\nposition.put(50, new JLabel(\"50\"));\r\nposition.put(75, new JLabel(\"75\"));\r\nposition.put(100, new JLabel(\"100\"));\r\n\t\t\r\n\/\/ Set the label to be drawn\r\nslider.setLabelTable(position); \r\n<\/pre>\n<p>The slider after running the code above is shown below. Here, we label the value of 0, 25, 50, 75 and 100 to make the slider clearer to read.<\/p>\n<p><figure id=\"attachment_32193\" aria-describedby=\"caption-attachment-32193\" style=\"width: 608px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderPosition.jpg\" rel=\"attachment wp-att-32193\"><img decoding=\"async\" class=\"wp-image-32193 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderPosition.jpg\" alt=\"sliderPosition\" width=\"608\" height=\"166\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderPosition.jpg 608w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderPosition-300x82.jpg 300w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/a><figcaption id=\"caption-attachment-32193\" class=\"wp-caption-text\">Slider with position labeled<\/figcaption><\/figure><\/p>\n<p>The code above could be summarized in the code <code>simpleSliderExample.java<\/code> below:<\/p>\n<p><span style=\"text-decoration: underline\"><em>simpleSliderExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package javaCodeGeeks;\r\n\r\nimport javax.swing.DefaultBoundedRangeModel;\r\nimport javax.swing.JFrame;\r\nimport javax.swing.JLabel;\r\nimport javax.swing.JPanel;\r\nimport javax.swing.JSlider;\r\n\r\nimport java.util.Hashtable;\r\n\r\n\/*\r\n * A simple swing slider example with different constructors\r\n *\/\r\n\r\npublic class simpleSliderExample {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/ Create and set up a frame window\r\n\t\tJFrame.setDefaultLookAndFeelDecorated(true);\r\n\t\tJFrame frame = new JFrame(\"JSlider setting examples\");\r\n\t\tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r\n\t\t\r\n\t\t\/\/ Set the panel to add buttons\r\n\t\tJPanel panel = new JPanel();\r\n\t\t\r\n\t\t\/\/ Different settings on the sliders\r\n\t\tJSlider slider = new JSlider();\r\n\t\t\/\/ JSlider slider = new JSlider(JSlider.VERTICAL);\r\n\t\t\/\/ JSlider slider = new JSlider(-100, 100, 50);\r\n\t\t\/\/ JSlider slider = new JSlider(JSlider.VERTICAL, -100, 100, 50);\r\n\t\t\r\n\t\t\/\/ Set the slider with the DefaultBoundedRangeModel\r\n\t\t\/\/DefaultBoundedRangeModel model = new DefaultBoundedRangeModel(20, 0, 1, 100);\r\n\t\t\/\/JSlider slider = new JSlider(model);\r\n\t\t\r\n\t\t\/\/ Set major or minor ticks for the slider\r\n\t\tslider.setMajorTickSpacing(25);\r\n\t\tslider.setMinorTickSpacing(10);\r\n\t\tslider.setPaintTicks(true);\r\n\t\t\r\n\t\t\/\/ Set the labels to be painted on the slider\r\n\t\tslider.setPaintLabels(true);\r\n\t\t\r\n\t\t\/\/ Add positions label in the slider\r\n\t\tHashtable position = new Hashtable();\r\n\t\tposition.put(0, new JLabel(\"0\"));\r\n\t\tposition.put(25, new JLabel(\"25\"));\r\n\t\tposition.put(50, new JLabel(\"50\"));\r\n\t\tposition.put(75, new JLabel(\"75\"));\r\n\t\tposition.put(100, new JLabel(\"100\"));\r\n\t\t\r\n\t\t\/\/ Set the label to be drawn\r\n\t\tslider.setLabelTable(position);\r\n\t\t\r\n\t\t\/\/ Add the slider to the panel\r\n\t\tpanel.add(slider);\r\n\t\t\r\n\t\t\/\/ Set the window to be visible as the default to be false\r\n\t\tframe.add(panel);\r\n\t\tframe.pack();\r\n\t\tframe.setVisible(true);\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>After running the code above, we can get the following figure:<br \/>\n<figure id=\"attachment_32192\" aria-describedby=\"caption-attachment-32192\" style=\"width: 624px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderSum.jpg\" rel=\"attachment wp-att-32192\"><img decoding=\"async\" class=\"wp-image-32192 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderSum.jpg\" alt=\"sliderSum\" width=\"624\" height=\"168\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderSum.jpg 624w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderSum-300x81.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderSum-620x168.jpg 620w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/a><figcaption id=\"caption-attachment-32192\" class=\"wp-caption-text\">Slider summary<\/figcaption><\/figure><\/p>\n<h3>2.4 Adding change listener to slider<\/h3>\n<p>To make the slider more functional, we can add the change listener to the slider and perform different functions.<\/p>\n<p>For example, we can add another panel to show the value of the slider in real-time. Then we know where is the slider now. In the example below, we add an change listener to the slider and show the value of the slider in a panel below the slider.<\/p>\n<p><span style=\"text-decoration: underline\"><em>sliderWithEvent.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package javaCodeGeeks;\r\n\r\nimport java.awt.GridLayout;\r\nimport java.util.Hashtable;\r\n\r\nimport javax.swing.JFrame;\r\nimport javax.swing.JLabel;\r\nimport javax.swing.JPanel;\r\nimport javax.swing.JSlider;\r\nimport javax.swing.event.ChangeEvent;\r\nimport javax.swing.event.ChangeListener;\r\n\r\npublic class sliderWithEvent {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/ Create and set up a frame window\r\n\t\tJFrame.setDefaultLookAndFeelDecorated(true);\r\n\t\tJFrame frame = new JFrame(\"Slider with change listener\");\r\n\t\tframe.setSize(500, 500);\r\n\t\tframe.setLayout(new GridLayout(3, 1));\r\n\t\tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r\n\t\t\r\n\t\t\/\/ Set the panel to add buttons\r\n\t\tJPanel panel1 = new JPanel();\r\n\t\tJPanel panel2 = new JPanel();\r\n\t\t\r\n\t\t\/\/ Add status label to show the status of the slider\r\n\t\tJLabel status = new JLabel(\"Slide the slider and you can get its value\", JLabel.CENTER);\r\n\t\t\r\n\t\t\/\/ Set the slider\r\n\t\tJSlider slider = new JSlider();\t\r\n\t\tslider.setMinorTickSpacing(10);\r\n\t\tslider.setPaintTicks(true);\r\n\t\t\r\n\t\t\/\/ Set the labels to be painted on the slider\r\n\t\tslider.setPaintLabels(true);\r\n\t\t\r\n\t\t\/\/ Add positions label in the slider\r\n\t\tHashtable&lt;Integer, JLabel&gt; position = new Hashtable&lt;Integer, JLabel&gt;();\r\n\t\tposition.put(0, new JLabel(\"0\"));\r\n\t\tposition.put(50, new JLabel(\"50\"));\r\n\t\tposition.put(100, new JLabel(\"100\"));\r\n\t\t\r\n\t\t\/\/ Set the label to be drawn\r\n\t\tslider.setLabelTable(position);\r\n\t\t\r\n\t\t\/\/ Add change listener to the slider\r\n\t\tslider.addChangeListener(new ChangeListener() {\r\n\t\t\tpublic void stateChanged(ChangeEvent e) {\r\n\t\t\t\tstatus.setText(\"Value of the slider is: \" + ((JSlider)e.getSource()).getValue());\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\t\t\/\/ Add the slider to the panel\r\n\t\tpanel1.add(slider);\r\n\t\t\r\n\t\t\/\/ Set the window to be visible as the default to be false\r\n\t\tframe.add(panel1);\r\n\t\tframe.add(status);\r\n\t\tframe.add(panel2);\r\n\t\tframe.pack();\r\n\t\tframe.setVisible(true);\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>After we run the code, we can see the initial status showing that &#8220;Slide the slider and you can get its value!&#8221;.<\/p>\n<p><figure id=\"attachment_32191\" aria-describedby=\"caption-attachment-32191\" style=\"width: 520px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitialEvent.jpg\" rel=\"attachment wp-att-32191\"><img decoding=\"async\" class=\"wp-image-32191 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitialEvent.jpg\" alt=\"sliderInitialEvent\" width=\"520\" height=\"426\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitialEvent.jpg 520w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderInitialEvent-300x246.jpg 300w\" sizes=\"(max-width: 520px) 100vw, 520px\" \/><\/a><figcaption id=\"caption-attachment-32191\" class=\"wp-caption-text\">Slider with initial event<\/figcaption><\/figure><\/p>\n<p>Then you can change the slider and get different values.<\/p>\n<p><figure id=\"attachment_32190\" aria-describedby=\"caption-attachment-32190\" style=\"width: 504px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderEvent.jpg\" rel=\"attachment wp-att-32190\"><img decoding=\"async\" class=\"wp-image-32190 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderEvent.jpg\" alt=\"sliderEvent\" width=\"504\" height=\"436\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderEvent.jpg 504w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderEvent-300x260.jpg 300w\" sizes=\"(max-width: 504px) 100vw, 504px\" \/><\/a><figcaption id=\"caption-attachment-32190\" class=\"wp-caption-text\">Slider after event<\/figcaption><\/figure><\/p>\n<h2>3. Download the Source Code<\/h2>\n<p>This was an example about Java Swing Slider.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the source code of this example here:\u00a0<strong><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/sliderExample.zip\" target=\"_blank\">sliderExample.zip<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction In this article, we&#8217;ll discuss Java swing slider. In Java Swing, slider is normally constructed by a JSlider component. JSlider is a component that lets the users select a value by sliding a knob within a specified interval. For the knob, it always points to the point which matches the integer values within &hellip;<\/p>\n","protected":false},"author":73,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[1334],"class_list":["post-32168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swing","tag-slider"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Swing Slider example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"1. Introduction In this article, we&#039;ll discuss Java swing slider. In Java Swing, slider is normally constructed by a JSlider component. JSlider is a\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Swing Slider example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"1. Introduction In this article, we&#039;ll discuss Java swing slider. In Java Swing, slider is normally constructed by a JSlider component. JSlider is a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-01-22T09:00:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jun Wu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jun Wu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/\"},\"author\":{\"name\":\"Jun Wu\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678\"},\"headline\":\"Java Swing Slider example\",\"datePublished\":\"2016-01-22T09:00:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/\"},\"wordCount\":962,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"Slider\"],\"articleSection\":[\"swing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/\",\"name\":\"Java Swing Slider example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2016-01-22T09:00:49+00:00\",\"description\":\"1. Introduction In this article, we'll discuss Java swing slider. In Java Swing, slider is normally constructed by a JSlider component. JSlider is a\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Desktop Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"swing\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/swing\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Java Swing Slider example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678\",\"name\":\"Jun Wu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg\",\"caption\":\"Jun Wu\"},\"description\":\"Jun (Steven) Wu is a current Master student in Computer Science &amp; Engineering department of University of Nebraska Lincoln (Lincoln, NE, USA). His current interests focus on Programming Languages (Java, Python), Relational Database (MySQL), NoSQL Database (Apache Cassandra, MongoDB), and Computer Networks.\",\"sameAs\":[\"https:\/\/wuxiaomin98.wordpress.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/jun-wu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Swing Slider example - Java Code Geeks","description":"1. Introduction In this article, we'll discuss Java swing slider. In Java Swing, slider is normally constructed by a JSlider component. JSlider is a","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/","og_locale":"en_US","og_type":"article","og_title":"Java Swing Slider example - Java Code Geeks","og_description":"1. Introduction In this article, we'll discuss Java swing slider. In Java Swing, slider is normally constructed by a JSlider component. JSlider is a","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-01-22T09:00:49+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Jun Wu","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Jun Wu","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/"},"author":{"name":"Jun Wu","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678"},"headline":"Java Swing Slider example","datePublished":"2016-01-22T09:00:49+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/"},"wordCount":962,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["Slider"],"articleSection":["swing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/","name":"Java Swing Slider example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2016-01-22T09:00:49+00:00","description":"1. Introduction In this article, we'll discuss Java swing slider. In Java Swing, slider is normally constructed by a JSlider component. JSlider is a","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-swing-slider-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Desktop Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/"},{"@type":"ListItem","position":4,"name":"swing","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/swing\/"},{"@type":"ListItem","position":5,"name":"Java Swing Slider example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678","name":"Jun Wu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg","caption":"Jun Wu"},"description":"Jun (Steven) Wu is a current Master student in Computer Science &amp; Engineering department of University of Nebraska Lincoln (Lincoln, NE, USA). His current interests focus on Programming Languages (Java, Python), Relational Database (MySQL), NoSQL Database (Apache Cassandra, MongoDB), and Computer Networks.","sameAs":["https:\/\/wuxiaomin98.wordpress.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/jun-wu\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/32168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/73"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=32168"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/32168\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=32168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=32168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=32168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}