{"id":27056,"date":"2021-05-09T23:10:05","date_gmt":"2021-05-10T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/custom-awt-slider-programming-code-examples-java-j2ee-j2me-awt-components\/"},"modified":"2021-05-09T23:10:05","modified_gmt":"2021-05-10T03:10:05","slug":"custom-awt-slider-programming-code-examples-java-j2ee-j2me-awt-components","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27056","title":{"rendered":"Custom AWT Slider #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components"},"content":{"rendered":"<pre>\n*****************\nCustom AWT Slider \n\n    * LabeledCostSlider.java. A numeric slider class with attached label.\n    * CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class.\n    * Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class.\n    * ScrollbarPanel.java A Panel with adjustable top and bottom insets, used by the Slider class to change the thickness of the Slider.\n*****************\nLabeledCostSlider.java. A numeric slider class with attached label. \n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nimport java.awt.*;\n\n\/** A CostSlider with a label centered above it. \n *************\n \n\npublic class LabeledCostSlider extends Panel {\n  public LabeledCostSlider(String labelString,\n                           Font labelFont,\n                           int minValue, int maxValue,\n                           int initialValue,\n                           Everest app) {\n    setLayout(new BorderLayout());\n    Label label = new Label(labelString, Label.CENTER);\n    if (labelFont != null) {\n      label.setFont(labelFont);\n    }\n    add(label, BorderLayout.NORTH);\n    CostSlider slider = new CostSlider(minValue, \n                                       maxValue, \n                                       initialValue,\n                                       app);\n    add(slider, BorderLayout.CENTER);\n  }\n}  \n&lt; &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nCostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class. \n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\n\/** A Slider that takes an Everest applet as an argument,\n *  calling back to its setCostField when the slider value\n *  changes.\n *\n *******************\n\npublic class CostSlider extends Slider {\n  private Everest app;\n\n  public CostSlider(int minValue, int maxValue,\n                    int initialValue, Everest app) {\n    super(minValue, maxValue, initialValue);\n    this.app = app;\n  }\n\n  public void doAction(int value) {\n    app.setCostField(value);\n  }\n}\n&lt; &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nSlider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class. \n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nimport java.awt.*;\nimport java.awt.event.*;\n\n\/** A class that combines a horizontal Scrollbar and a TextField\n *  (to the right of the Scrollbar). The TextField shows the\n *  current scrollbar value, plus, if setEditable(true) is set,\n *  it can be used to change the value as well.\n *\n ********************\n\npublic class Slider extends Panel implements ActionListener,\n                                             AdjustmentListener {\n  private Scrollbar scrollbar;\n  private TextField textfield;\n  private ScrollbarPanel scrollbarPanel;\n  private int preferredWidth = 250;\n\n  \/** Construct a slider with the specified min, max and initial\n   *  values. The &quot;bubble&quot; (thumb) size is set to 1\/10th the\n   *  scrollbar range.\n   *\/\n\n  public Slider(int minValue, int maxValue, int initialValue) {\n    this(minValue, maxValue, initialValue,\n         (maxValue - minValue)\/10);\n  }\n\n  \/** Construct a slider with the specified min, max,and initial\n   *  values, plus the specified &quot;bubble&quot; (thumb) value. This\n   *  bubbleSize should be specified in the units that min and\n   *  max use, not in pixels. Thus, if min is 20 and max is 320,\n   *  then a bubbleSize of 30 is 10% of the visible range.\n   *\/\n\n  public Slider(int minValue, int maxValue, int initialValue,\n                int bubbleSize) {\n    setLayout(new BorderLayout());\n    maxValue = maxValue + bubbleSize;\n    scrollbar = new Scrollbar(Scrollbar.HORIZONTAL,\n                              initialValue, bubbleSize,\n                              minValue, maxValue);\n    scrollbar.addAdjustmentListener(this);\n    scrollbarPanel = new ScrollbarPanel(6);\n    scrollbarPanel.add(scrollbar, BorderLayout.CENTER);\n    add(scrollbarPanel, BorderLayout.CENTER);\n    textfield = new TextField(numDigits(maxValue) + 1);\n    textfield.addActionListener(this);\n    setFontSize(12);\n    textfield.setEditable(false);\n    setTextFieldValue();\n    add(textfield, BorderLayout.EAST);\n  }\n\n  \/** A place holder to override for action to be taken when\n   *  scrollbar changes.\n   *\/\n\n  public void doAction(int value) {\n  }\n\n  \/** When textfield changes, sets the scrollbar *\/\n\n  public void actionPerformed(ActionEvent event) {\n    String value = textfield.getText();\n    int oldValue = getValue();\n    try {\n      setValue(Integer.parseInt(value.trim()));\n    } catch(NumberFormatException nfe) {\n      setValue(oldValue);\n    }\n  }\n\n  \/** When scrollbar changes, sets the textfield *\/\n\n  public void adjustmentValueChanged(AdjustmentEvent event) {\n    setTextFieldValue();\n    doAction(scrollbar.getValue());\n  }\n\n  \/** Returns the Scrollbar part of the Slider. *\/\n\n  public Scrollbar getScrollbar() {\n    return(scrollbar);\n  }\n\n  \/** Returns the TextField part of the Slider *\/\n\n  public TextField getTextField() {\n    return(textfield);\n  }\n\n  \/** Changes the preferredSize to take a minimum width, since\n   *  super-tiny scrollbars are hard to manipulate.\n   *\/\n\n  public Dimension getPreferredSize() {\n    Dimension d = super.getPreferredSize();\n    d.height = textfield.getPreferredSize().height;\n    d.width = Math.max(d.width, preferredWidth);\n    return(d);\n  }\n\n  \/** This just calls preferredSize *\/\n\n  public Dimension getMinimumSize() {\n    return(getPreferredSize());\n  }\n\n  \/** To keep scrollbars legible, a minimum width is set. This\n   *  returns the current value (default is 150).\n   *\/\n\n  public int getPreferredWidth() {\n    return(preferredWidth);\n  }\n\n  \/** To keep scrollbars legible, a minimum width is set. This\n   *  sets the current value (default is 150).\n   *\/\n\n  public void setPreferredWidth(int preferredWidth) {\n    this.preferredWidth = preferredWidth;\n  }\n\n  \/** This returns the current scrollbar value *\/\n\n  public int getValue() {\n    return(scrollbar.getValue());\n  }\n\n  \/** This assigns the scrollbar value. If it is below the\n   *  minimum value or above the maximum, the value is set to\n   *  the min and max value, respectively.\n   *\/\n\n  public void setValue(int value) {\n    scrollbar.setValue(value);\n    setTextFieldValue();\n  }\n\n  \/** Sometimes horizontal scrollbars look odd if they are very\n   *  tall. So empty top\/bottom margins can be set. This returns\n   *  the margin setting. The default is four.\n   *\/\n\n  public int getMargins() {\n    return(scrollbarPanel.getMargins());\n  }\n\n  \/** Sometimes horizontal scrollbars look odd if they are very\n   *  tall. So empty top\/bottom margins can be set. This sets\n   *  the margin setting.\n   *\/\n\n  public void setMargins(int margins) {\n    scrollbarPanel.setMargins(margins);\n  }\n\n  \/** Returns the current textfield string. In most cases this\n   *  is just the same as a String version of getValue, except\n   *  that there may be padded blank spaces at the left.\n   *\/\n\n  public String getText() {\n    return(textfield.getText());\n  }\n\n  \/** This sets the TextField value directly. Use with extreme\n   *  caution since it does not right-align or check if value\n   *  is numeric.\n   *\/\n\n  public void setText(String text) {\n    textfield.setText(text);\n  }\n\n  \/** Returns the Font being used by the textfield.\n   *  Courier bold 12 is the default.\n   *\/\n\n  public Font getFont() {\n    return(textfield.getFont());\n  }\n\n  \/** Changes the Font being used by the textfield. *\/\n\n  public void setFont(Font textFieldFont) {\n    textfield.setFont(textFieldFont);\n  }\n\n  \/** The size of the current font *\/\n\n  public int getFontSize() {\n    return(getFont().getSize());\n  }\n\n  \/** Rather than setting the whole font, you can just set the\n   *  size (Monospaced bold will be used for the family\/face).\n   *\/\n\n  public void setFontSize(int size) {\n    setFont(new Font(&quot;Monospaced&quot;, Font.BOLD, size));\n  }\n\n  \/** Determines if the textfield is editable. If it is, you can\n   *  enter a number to change the scrollbar value. In such a\n   *  case, entering a value outside the legal range results in\n   *  the min or max legal value. A non-integer is ignored.\n   *\/\n\n  public boolean isEditable() {\n    return(textfield.isEditable());\n  }\n\n  \/** Determines if you can enter values directly into the\n   *  textfield to change the scrollbar.\n   *\/\n\n  public void setEditable(boolean editable) {\n    textfield.setEditable(editable);\n  }\n\n  \/\/ Sets a right-aligned textfield number.\n\n  private void setTextFieldValue() {\n    int value = scrollbar.getValue();\n    int digits = numDigits(scrollbar.getMaximum());\n    String valueString = padString(value, digits);\n    textfield.setText(valueString);\n  }\n\n  \/\/ Repeated String concatenation is expensive, but this is\n  \/\/ only used to add a small amount of padding, so converting\n  \/\/ to a StringBuffer would not pay off.\n\n  private String padString(int value, int digits) {\n    String result = String.valueOf(value);\n    for(int i=result.length(); i&lt;digits ; i++) {\n      result = &quot; &quot; + result;\n    }\n    return(result + &quot; &quot;);\n  }\n\n  \/\/ Determines the number of digits in a decimal number.\n\n  private static final double LN10 = Math.log(10.0);\n\n  private static int numDigits(int num) {\n    return(1 + (int)Math.floor(Math.log((double)num)\/LN10));\n  }\n}\n&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nScrollbarPanel.java A Panel with adjustable top and bottom insets, used by the Slider class to change the thickness of the Slider.\n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nimport java.awt.*;\n\n\/** A Panel with adjustable top\/bottom insets value.\n *  Used to hold a Scrollbar in the Slider class.\n *\n *******************\n\npublic class ScrollbarPanel extends Panel {\n  private Insets insets;\n\n  public ScrollbarPanel(int margins) {\n    setLayout(new BorderLayout());\n    setMargins(margins);\n  }\n\n  public Insets insets() {\n    return(insets);\n  }\n\n  public int getMargins() {\n    return(insets.top);\n  }\n\n  public void setMargins(int margins) {\n    this.insets = new Insets(margins, 0, margins, 0);\n  }\n}\n&lt; &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10357<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, AWT Components<br \/>Tags:Java\/J2EE\/J2MEAWT Components<br \/> Post Data:2017-01-02 16:04:35<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>***************** Custom AWT Slider * LabeledCostSlider.java. A numeric slider class with attached label. * CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class. * Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class. * ScrollbarPanel.java A Panel with adjustable top and bottom insets, &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=27056\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-27056","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10374,"url":"http:\/\/bangla.sitestree.com\/?p=10374","url_meta":{"origin":27056,"position":0},"title":"Custom AWT Slider","author":"","date":"August 27, 2015","format":false,"excerpt":"***************** Custom AWT Slider \u00a0\u00a0\u00a0 * LabeledCostSlider.java. A numeric slider class with attached label. \u00a0\u00a0\u00a0 * CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class. \u00a0\u00a0\u00a0 * Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class. \u00a0\u00a0\u00a0 *\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26996,"url":"http:\/\/bangla.sitestree.com\/?p=26996","url_meta":{"origin":27056,"position":1},"title":"Position circles down the diagonal so that their borders #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 7, 2021","format":false,"excerpt":"import java.awt.*; import java.applet.Applet; \/** Position circles down the diagonal so that their borders * just touch. Illustrates that AWT components are * rectangular and opaque. *\/ public class CircleTest2 extends Applet { public void init() { setBackground(Color.lightGray); setLayout(null); \/\/ Turn off layout manager. Circle circle; int radius = getSize().width\/6;\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27000,"url":"http:\/\/bangla.sitestree.com\/?p=27000","url_meta":{"origin":27056,"position":2},"title":"Eight buttons: four each in two panels #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 7, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; *************************** \/** Eight buttons: four each in two panels. * *\/ public class ButtonTest2 extends Applet { public void init() { String[] labelPrefixes = { \"Start\", \"Stop\", \"Pause\", \"Resume\" }; Panel p1 = new Panel(); for (int i=0; i<4; i++) { p1.add(new Button(labelPrefixes[i] + \" Thread1\"));\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26994,"url":"http:\/\/bangla.sitestree.com\/?p=26994","url_meta":{"origin":27056,"position":3},"title":"A Circle component built using a Canvas #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 7, 2021","format":false,"excerpt":"import java.awt.*; \/** A Circle component built using a Canvas. * *\/ public class Circle extends Canvas { private int width, height; public Circle(Color foreground, int radius) { setForeground(foreground); width = 2*radius; height = 2*radius; setSize(width, height); } public void paint(Graphics g) { g.fillOval(0, 0, width, height); } public void\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26998,"url":"http:\/\/bangla.sitestree.com\/?p=26998","url_meta":{"origin":27056,"position":4},"title":"Eight ungrouped buttons in an Applet using FlowLayout #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 7, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; ************************** \/** Eight ungrouped buttons in an Applet using FlowLayout. * *\/ public class ButtonTest1 extends Applet { public void init() { String[] labelPrefixes = { \"Start\", \"Stop\", \"Pause\", \"Resume\" }; for (int i=0; i<4; i++) { add(new Button(labelPrefixes[i] + \" Thread1\")); } for (int i=0;\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27020,"url":"http:\/\/bangla.sitestree.com\/?p=27020","url_meta":{"origin":27056,"position":5},"title":"Uses a FileDialog to choose the file to display #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 8, 2021","format":false,"excerpt":"DisplayFile.java **************** import java.awt.*; import java.awt.event.*; import java.io.*; \/** Uses a FileDialog to choose the file to display. *************** public class DisplayFile extends CloseableFrame implements ActionListener { public static void main(String[] args) { new DisplayFile(); } private Button loadButton; private TextArea fileArea; private FileDialog loader; public DisplayFile() { super(\"Using FileDialog\");\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27056"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27056\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27056"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}