Link to home
Start Free TrialLog in
Avatar of Rascal79
Rascal79

asked on

Help with Linking a scrollbar to a textfield


I have been asked to create a small application in java that does the following. On a frame there are 3 types of object - canvas, scrollbar and textfield.  In the textfield the range of numbers will be 0 - 255 If the scrollbar(there are three - red green and blue) is moved the numbers in the corresponding textfield(three also red,green,blue) will change and the canvas will fill with more of or less of that particular color.

All that I have left to do is the textfield part, ie if you put in a number in the textfield it must change the position of the scrollbar and the color of the canvas,  

Below is the code I have so far, (the listeners are the area I need to update).  
Does anyone have any ideas how to go about it??

Thanks R79

package Colourchooser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;




public class Colourchooser extends JFrame implements ActionListener,AdjustmentListener{

    // Variable declarations of textfields and scrollbars.
    JTextField redtfield,greentfield,bluetfield;  
    Container contentpane;  
    JLabel redlabel,greenlabel,bluelabel;
    Scrollbar scrollr,scrollg,scrollb;  
    GridLayout colourlayout;  
    int redValue, greenValue, blueValue;

    // Variable declaration of DrawCanvas
    DrawCanvas c;

    public Colourchooser() {    
        //define and set the layout used
        contentpane = getContentPane () ;
        setTitle ( "Colour Chooser" ) ;
        setBackground(Color.gray);
        setSize(1000,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        colourlayout = new GridLayout(0,10);
        contentpane.setLayout(colourlayout);
     
        // - define the scrollbars, labels and textfields used
        redtfield = new JTextField(redValue);  
        greentfield = new JTextField(greenValue);
        bluetfield = new JTextField(blueValue);  
        redlabel = new JLabel("Red");  
        greenlabel = new JLabel("Green");  
        bluelabel = new JLabel("Blue");
        scrollr = new Scrollbar(Scrollbar.VERTICAL, 0,1, 0, 255);
        scrollg = new Scrollbar(Scrollbar.VERTICAL, 0,1, 0, 255);  
        scrollb = new Scrollbar(Scrollbar.VERTICAL, 0,1, 0, 255);
        // - Setting value of DrawCanvas to null - If this wass'nt here
        // - when the program executed it threw NullPointerExceptions
        c = new DrawCanvas();                                                  
       
        //  - add the scrollbars, labels and textfields to the layout
        contentpane.add(redlabel);  
        contentpane.add(scrollr);  
        contentpane.add(redtfield);
        contentpane.add(greenlabel);
        contentpane.add(scrollg);  
        contentpane.add(greentfield);  
        contentpane.add(bluelabel);  
        contentpane.add(scrollb);  
        contentpane.add(bluetfield);
           
        //    - add the instance (i.e. c) to your layout
        contentpane.add(c);
     
        //    - add the action and adjustment listeners
        scrollr.addAdjustmentListener(new AdjustmentlistenerS());  
        scrollb.addAdjustmentListener(new AdjustmentlistenerS());
        scrollg.addAdjustmentListener(new AdjustmentlistenerS());
        pack();  
        show();
    } // end of the constructor


    public static void main(String args[]){

      new Colourchooser();

    }

    public void actionPerformed(ActionEvent e){

      // In this method, retrieve the red, green and blue
      // values entered into the textfields.
       

      // You also need to set the value in the corresponding scrollbar to be
      // the same value as the textfield value.

      // Call the setCanvasColour method (in the DrawCanvas class) to set the
      // colour of the canvas to the new values i.e.
      //c.setCanvasColor(redtfield, greenValue, blueValue);

    }

    public void adjustmentValueChanged(AdjustmentEvent e) {
    }    
   
   
         class AdjustmentlistenerS implements AdjustmentListener {

         public void adjustmentValueChanged(AdjustmentEvent e){

      // In this method, retrieve the red, green and blue
      // values entered into the textfields.
      redtfield.getText();
      bluetfield.getText();
      greentfield.getText();
                 
      // You also need to set the value in the corresponding textfield to be
      // the same value as the scrollbar value.

      // Call the setCanvasColour method (in the DrawCanvas class) to set the
      // colour of the canvas to the new values i.e.

      c.setCanvasColor(scrollr.getValue(),scrollg.getValue(),scrollb.getValue());

   }

}}


********************************************************************************DrawCanvas.java

package Colourchooser;


import java.awt.* ;


public class DrawCanvas extends Canvas



{
     // Variable declarations
         int greenValue, redValue, blueValue;
     
       
 
   

     public DrawCanvas()
     {
         // set the size of the canvas
              setSize(65,65);
     }

    public void paintComponent(Graphics g)

     {
        // set the colour of the canvas
              g.setColor(getBackground());
              g.fillRect(0, 0, getWidth(), getHeight());

     }

     public void setCanvasColor(int red, int green, int blue)
     {
        // "repaint" the canvas(this will call the paint method)
                 setBackground(new Color(red, green, blue));
                 repaint();
     }

}
 

Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;

class DrawCanvas extends JPanel
{
  // Variable declarations
  int greenValue, redValue, blueValue ;

  public DrawCanvas()
  {
    // set the size of the canvas
    setSize( 65, 65 ) ;
  }

//  public void paintComponent( Graphics g )
//
//  {
//    // set the colour of the canvas
//    g.setColor( getBackground() ) ;
//    g.fillRect( 0, 0, getWidth(), getHeight() ) ;
//
//  }
//
  public void setCanvasColor( int red, int green, int blue )
  {
    // "repaint" the canvas(this will call the paint method)
    setBackground( new Color( red, green, blue ) ) ;
    repaint() ;
  }
}

public class Colourchooser
    extends JFrame
    implements ActionListener, AdjustmentListener
{

  // Variable declarations of textfields and scrollbars.
  JTextField redtfield, greentfield, bluetfield ;
  Container contentpane ;
  JLabel redlabel, greenlabel, bluelabel ;
  Scrollbar scrollr, scrollg, scrollb ;
  GridLayout colourlayout ;
  int redValue, greenValue, blueValue ;

  // Variable declaration of DrawCanvas
  DrawCanvas c ;

  public Colourchooser()
  {
    //define and set the layout used
    contentpane = getContentPane() ;
    setTitle( "Colour Chooser" ) ;
    setBackground( Color.gray ) ;
    setSize( 1000, 500 ) ;
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
    colourlayout = new GridLayout( 0, 10 ) ;
    contentpane.setLayout( colourlayout ) ;

    // - define the scrollbars, labels and textfields used
    redtfield = new JTextField( "" + redValue ) ;
    redtfield.addActionListener( this );
    greentfield = new JTextField( "" + greenValue ) ;
    greentfield.addActionListener( this );
    bluetfield = new JTextField( "" + blueValue ) ;
    bluetfield.addActionListener( this );

    redlabel = new JLabel( "Red" ) ;
    greenlabel = new JLabel( "Green" ) ;
    bluelabel = new JLabel( "Blue" ) ;
    scrollr = new Scrollbar( Scrollbar.VERTICAL, 0, 1, 0, 255 ) ;
    scrollg = new Scrollbar( Scrollbar.VERTICAL, 0, 1, 0, 255 ) ;
    scrollb = new Scrollbar( Scrollbar.VERTICAL, 0, 1, 0, 255 ) ;
    // - Setting value of DrawCanvas to null - If this wass'nt here
    // - when the program executed it threw NullPointerExceptions
    c = new DrawCanvas() ;

    //  - add the scrollbars, labels and textfields to the layout
    contentpane.add( redlabel ) ;
    contentpane.add( scrollr ) ;
    contentpane.add( redtfield ) ;
    contentpane.add( greenlabel ) ;
    contentpane.add( scrollg ) ;
    contentpane.add( greentfield ) ;
    contentpane.add( bluelabel ) ;
    contentpane.add( scrollb ) ;
    contentpane.add( bluetfield ) ;

    //    - add the instance (i.e. c) to your layout
    contentpane.add( c ) ;

    //    - add the action and adjustment listeners
    scrollr.addAdjustmentListener( this ) ;
    scrollb.addAdjustmentListener( this ) ;
    scrollg.addAdjustmentListener( this ) ;
    pack() ;
    update() ;
    show() ;
  } // end of the constructor

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

  public void update()
  {
    redtfield.setText( "" + scrollr.getValue() );
    greentfield.setText( "" + scrollg.getValue() );
    bluetfield.setText( "" + scrollb.getValue() );
    c.setCanvasColor( scrollr.getValue(), scrollg.getValue(),
                      scrollb.getValue() ) ;
  }

  public void actionPerformed( ActionEvent e )
  {
    try
    {
      scrollr.setValue( Integer.parseInt( redtfield.getText() ) ) ;
      scrollg.setValue( Integer.parseInt( greentfield.getText() ) ) ;
      scrollb.setValue( Integer.parseInt( bluetfield.getText() ) ) ;
      update() ;
    }
    catch( NumberFormatException ex )
    {
    }
  }

  public void adjustmentValueChanged( AdjustmentEvent e )
  {
    update() ;
  }
}
Avatar of Rascal79
Rascal79

ASKER

Thank You Tim,

One more q however, I also need to be able to manually type in a number between 1 and 255, when this is done the scroll must change position and the canvas must change color to match the no typed in.

Also in regard to the changes to my code you made
1)   //    - add the action and adjustment listeners
    scrollr.addAdjustmentListener( this ) ;
    scrollb.addAdjustmentListener( this ) ;
    scrollg.addAdjustmentListener( this ) ;

What does this change do??

2) //  public void paintComponent( Graphics g )
//
//  {
//    // set the colour of the canvas
//    g.setColor( getBackground() ) ;
//    g.fillRect( 0, 0, getWidth(), getHeight() ) ;
//
//  }
//

Why comment this method out??

Thanks for the help - R79



ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It does now Tim!

Thank you for your help,

R79
No worries :-)

Glad I could help :-)

Good luck with it!!

Tim
BTW:  I had a quick play around with it, and I'd probably do it like this:

Not sure if you prefer it or not...  Just thought I'd post it here so you could see an alternative!! ;-)

Good luck again!! :-)

Tim

------------------------

import java.awt.* ;
import javax.swing.* ;
import javax.swing.event.* ;

public class Colourchooser extends JFrame implements ChangeListener
{
  // Define out required attributes
  JSpinner red, green, blue ;
  JPanel colorPanel = new JPanel() ;

  public Colourchooser()
  {
    this.getContentPane().setLayout( new FlowLayout( FlowLayout.LEADING, 5, 0 ) );

    // - define the spinners
    red = new JSpinner( new SpinnerNumberModel( 0, 0, 255, 1 ) ) ;
    red.addChangeListener( this );
    green = new JSpinner( new SpinnerNumberModel( 0, 0, 255, 1 ) ) ;
    green.addChangeListener( this );
    blue = new JSpinner( new SpinnerNumberModel( 0, 0, 255, 1 ) ) ;
    blue.addChangeListener( this );

    // - set the color panel size
    colorPanel.setPreferredSize( new Dimension( 64, 64 ) );

    // - define our labels
    JLabel redlabel   = new JLabel( "Red" ) ;
    JLabel greenlabel = new JLabel( "Green" ) ;
    JLabel bluelabel  = new JLabel( "Red" ) ;

    // - add the labels, spinners and the panel
    this.getContentPane().add( redlabel ) ;
    this.getContentPane().add( red ) ;
    this.getContentPane().add( greenlabel ) ;
    this.getContentPane().add( green ) ;
    this.getContentPane().add( bluelabel ) ;
    this.getContentPane().add( blue ) ;
    this.getContentPane().add( colorPanel ) ;
   
    // - fire the update method, to set the panel color up
    update() ;
  } // end of the constructor

  /**
   * This updates the color panel based on the values of the 3 spinners...
   */
  public void update()
  {
    colorPanel.setBackground( new Color( ((Integer)red.getValue()).intValue(),
                                         ((Integer)green.getValue()).intValue(),
                                         ((Integer)blue.getValue()).intValue() ) );
  }

  /**
   * Fired when the spinner value changes
   * @param e The changeEvent sent by the corresponding JSpinner
   */
  public void stateChanged( ChangeEvent e )
  {
    update() ;
  }

  public static void main( String args[] )
  {
    Colourchooser chooser = new Colourchooser() ;
    chooser.setTitle( "Colour Chooser" ) ;
    chooser.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
    chooser.pack() ;
    chooser.show() ;
  }
}
Thnaks Tim - One more question if you dont mind.  I'm just trying to understand fully the code you gave originally.  
How did you specify that the range of the no's was 0 - 254?  I dont see them specifed anywhere???

Thanks - R79
That's here:

    scrollr = new Scrollbar( Scrollbar.VERTICAL, 0, 1, 0, 255 ) ;
    scrollg = new Scrollbar( Scrollbar.VERTICAL, 0, 1, 0, 255 ) ;
    scrollb = new Scrollbar( Scrollbar.VERTICAL, 0, 1, 0, 255 ) ;

Actually...  you should change that to:

    scrollr = new JScrollBar( JScrollBar.VERTICAL, 0, 1, 0, 255 )  ;
    scrollg = new JScrollBar( JScrollBar.VERTICAL, 0, 1, 0, 255 )  ;
    scrollb = new JScrollBar( JScrollBar.VERTICAL, 0, 1, 0, 255 )  ;

to use the Swing controls rather than the AWT ones.. (you'll need to change the 3 declarations from "Scrollbar" to "JScrollBar" too)

That defines orientation, start pos, extent, lowest value, and highest value respectivly :-)

Hope this helps :-)

Tim