6.05 - Using Other Components


Introduction

Now that we have seen some of the other components that are available and we have also seen quite a complex event structure for these components an example should help clear up any confusion.

Scrollbar Example

Task: Write the application as shown in Figure 6.18, “A Scrollbar Example” where the Scrollbar component updates the text in the TextField component when it is moved. The Reset Button component should set the value of the TextField object to 0. The Scrollbar component should have the range 0 to 100.

Figure 6.18. A Scrollbar Example 


Solution: You can see this Scrollbar example running in Figure 6.18. The code is at the bottom of the page.

ScrollbarExample.java

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
package ee402;

import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class ScrollbarExample extends Frame implements AdjustmentListener, ActionListener
{
        private Scrollbar scrollbar;
        private TextField status;
        private Button resetButton;

        public ScrollbarExample(){
                super("Scrollbar Example");
                this.setLayout(new GridLayout(2,1));
                Panel topPanel = new Panel();

                this.status = new TextField(20);
                this.scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110);
                this.resetButton = new Button("Reset");

                this.resetButton.addActionListener(this);
                this.scrollbar.addAdjustmentListener(this);

                topPanel.add(status);
                topPanel.add(resetButton);

                this.add(topPanel);
                this.add(scrollbar);

                this.updateText();
                this.pack();
                this.setVisible(true);
        }

        public void actionPerformed(ActionEvent e){
                if (e.getActionCommand().equals("Reset")){
                        this.scrollbar.setValue(0);
                        this.updateText();
                }
        }

        public void adjustmentValueChanged(AdjustmentEvent e){
                if (e.getSource().equals(scrollbar)){
                        this.updateText();
                }
        }

        private void updateText(){
                status.setText("Scroll Value = " + scrollbar.getValue());
        }

        public static void main(String[] args) {
                new ScrollbarExample();
        }
}

To complete this application there are a few points to note:

  • The ScrollbarExample class implements both the AdjustmentListener and the ActionListener interfaces. A class can implement as many interfaces as desired. To implement both these interfaces an adjustmentValueChanged() method and an actionPerformed() method both had to be written, otherwise the code would not compile.

  • I wrote a private method updateText() that updates the status state. I did this as otherwise there would be three different status.setText() calls, replicating code and leading to unpredictable results if two were changed and one was not.

  • For the layout I used a 2x1 (i.e. 2 rows, 1 column) GridLayout with a panel (topPanel) in the top grid section and the scrollbar object in the bottom grid section. To the topPanel container I added the status TextField object and the resetButton Button object. Since the default layout for a Panel object is FlowLayout the components added are given their preferred size. Note that because the scrollbar object was added directly to the grid section it is bound to the grid size and will change size as the application changes size.

  • I have called this.updateText() in the constructor so that the status TextField component will display the initial value of the scrollbar object.

  • Note that in the constructor call to Scrollbar the range was set from 0 to 110. We were required by the task to allow a value to be chosen from 0 to 100. We set the maximum at 110 as the width of the bubble is 10 and the value selected is taken from the start of the bubble. So the maximum range value is always the value desired plus the width of the bubble. Note that when you run this application (alternatively see Figure 6.18, “A Scrollbar Example Application”) you can press on the "arrows" at the left and right of the Scrollbar component and the value will increment/decrement by 1. If you press on the area between the bubble and the arrows then the value will increment/decrement by 10 - the width of the bubble.




These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
ċ
ScrollbarExample.java
(1k)
Derek Molloy,
13 Nov 2013, 03:16
Comments