//================================================================================================== // G r a p h i c Java // S c r o l l B a r // By Bruno Bachelet //================================================================================================== // Copyright (c) 1999-2016 // Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net // // This file is part of the B++ Library. This library is free software; you can redistribute it // and/or modify it under the terms of the GNU Library General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your option) any later // version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU Library General Public License for more details (http://www.gnu.org).
/*DESCRIPTION*/ /* This module provides a class to create a scroll bar component with its associated value displayed. */
// Package //--------------------------------------------------------------------------------------- package bpp.graphic;
// Importation //----------------------------------------------------------------------------------- import java.awt.*; import java.awt.event.*;
// S c r o l l B a r Class //---------------------------------------------------------------------- /*CLASS ScrollBar */ /* Represents a scroll bar where the associated value is displayed. */ public class ScrollBar extends Panel implements AdjustmentListener { //---------------------------------------------------------------------------------------Attributes protected Scrollbar atBar; // Scroll bar component. protected Label atTitle; // Component displaying the title. protected Label atValue; // Component displaying the value. //--------------------------------------------------------------------------------------Constructor /*METHOD ScrollBar */ /* Builds a scroll bar with a default value and a maximum value. */ public ScrollBar(int agDefaultValue,int agMaximumValue) { super();
atBar=new Scrollbar(Scrollbar.HORIZONTAL,agDefaultValue-1,1,0,agMaximumValue); atValue=new Label(valueString(),Label.CENTER);
setLayout(new BorderLayout(0,0)); add(new LayoutShell(atBar),BorderLayout.CENTER); add(new LayoutShell(atValue),BorderLayout.SOUTH);
Dimension lcSize = atBar.getSize();
atBar.setSize((int)200,(int)lcSize.getHeight()); atBar.addAdjustmentListener(this); } //--------------------------------------------------------------------------------------Constructor /*METHOD ScrollBar */ /* Builds a scroll bar with a default value, a maximum value and a title. */ public ScrollBar(int agDefaultValue,int agMaximumValue,String agTitle) { super();
atBar=new Scrollbar(Scrollbar.HORIZONTAL,agDefaultValue-1,1,0,agMaximumValue); atTitle=new Label(agTitle,Label.CENTER); atValue=new Label(valueString(),Label.CENTER);
setLayout(new BorderLayout(0,0)); add(atTitle,BorderLayout.NORTH); add(atBar,BorderLayout.CENTER); add(atValue,BorderLayout.SOUTH);
Dimension lcSize = atBar.getSize();
atBar.setSize((int)200,(int)lcSize.getHeight()); addAdjustmentListener(this); } //------------------------------------------------------------------------------------------Maximum /*METHOD ScrollBar */ /* Returns the maximum value of the bar. */ public int maximum() { return (atBar.getMaximum()); } //--------------------------------------------------------------------------------------------Value /*METHOD ScrollBar */ /* Returns the value associated with the bar. */ public int value() { return (atBar.getValue()+1); } //---------------------------------------------------------------------------------------SetMaximum /*METHOD ScrollBar */ /* Sets the maximum value of the bar. */ public void setMaximum(int agMaximum) { atBar.setMaximum(agMaximum); atValue.setText(valueString()); } //-----------------------------------------------------------------------------------------SetValue /*METHOD ScrollBar */ /* Sets the value associated with the bar. */ public void setValue(int agValue) { atBar.setValue(agValue-1); atValue.setText(valueString()); } //----------------------------------------------------------------------------AddAdjustmentListener /*METHOD ScrollBar */ /* Adds an adjustment listener to the bar. */ public void addAdjustmentListener(AdjustmentListener agListener) { atBar.addAdjustmentListener(agListener); } //-------------------------------------------------------------------------RemoveAdjustmentListener /*METHOD ScrollBar */ /* Removes an adjustment listener from the bar. */ public void removeAdjustmentListener(AdjustmentListener agListener) { atBar.removeAdjustmentListener(agListener); } //---------------------------------------------------------------------------AdjustmentValueChanged /*METHOD ScrollBar */ /* Called when the position of the scroll bar has changed. */ public void adjustmentValueChanged(AdjustmentEvent agEvent) { atValue.setText(valueString()); } //--------------------------------------------------------------------------------------ValueString protected String valueString() { return (value()+" / "+maximum()); } }
// End //------------------------------------------------------------------------------------------- |
|