I am doing my homework right now. my program is providing 3 slider that user can choose RGB value and fill the color in rectangle. also everytime when the user slide the slider, there should be a JTextField can show the value which the user choose.

my problms are:
I don't know how I can write the value from the slider to JTextField.

I want to write the value into JTextField and by the time, the slider should slide to the value which I just enter.

is there any one can help me how I can do those 2 things?

Nicole

Hi,

There's a great tutorial link that gives an example of what you are asking for. Or at least close to what you are asking for - it's about frames per second but the idea is very similar to your problem.

Here is the link:
http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html

So for example, if you add a changeListener to your JSlider component, you can then use the:

public void stateChanged(ChangeEvent e)

method to write your code that will update your JTextField.

Hope this is of some help,
/Soo-Im

// Illustrates the use of the Color class
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class Colors extends Applet implements ActionListener
{
ColorControl[] control = new ColorControl[3];


public Colors()
{
setLayout( new GridLayout(6,1) );


control[0] = new ColorControl( "Red" );
control[1] = new ColorControl( "Green" );
control[2] = new ColorControl( "Blue" );


for (int i = 0; i < 3; i++)
{
add( control );
}
}


public void actionPerformed( ActionEvent theEvent )
{
Color c = new Color( control[0].getValue(),
control[1].getValue(),
control[2].getValue() );


setBackground( c );
repaint();
}


public static void main(String arg[])
{
Colors theApp = new Colors( );


AppletFrame af = new AppletFrame(theApp, "The Colors Applet");


af.add(theApp, "Center");
af.setSize( 200, 300 );
af.setVisible(true);
theApp.init( );
theApp.start( );
}
}


class ColorControl extends Applet implements ActionListener
{
final static int STEP = 16;


private Label name;
private int   value     = 255;
private TextField input = new TextField( "255" );
private Button upButton = new Button( " + " );
private Button dnButton = new Button( " - " );


public int getValue() { return value; }


public ColorControl( String s )
{
setLayout( new GridLayout(1,4) );
name = new Label( s );


add( name );
add( input );
add( upButton );
add( dnButton );
input.addActionListener(this);
upButton.addActionListener(this);
dnButton.addActionListener(this);
}


public void actionPerformed(ActionEvent theEvent)
{
value = Integer.parseInt(input.getText());


if (theEvent.getSource( ) == upButton) value+=(value + STEP > 255 ? 0 : STEP);
if (theEvent.getSource( ) == dnButton) value-=(value - STEP < 0 ? 0 : STEP);


input.setText( "" + value );
((ActionListener) getParent( )).actionPerformed(theEvent);
}
}


class AppletFrame extends Frame implements WindowListener
{
Applet theApplet;


public AppletFrame(Applet anyApplet, String title)
{
super(title);
theApplet = anyApplet;
addWindowListener(this);
}


public void windowClosed     (WindowEvent theEvent) {  }
public void windowClosing    (WindowEvent theEvent) { quitApplication( ); }
public void windowDeiconified(WindowEvent theEvent) { }
public void windowIconified  (WindowEvent theEvent) { }
public void windowOpened     (WindowEvent theEvent) { }
public void windowActivated  (WindowEvent theEvent) { }
public void windowDeactivated(WindowEvent theEvent) { }


public void quitApplication( )
{
setVisible(false);
theApplet.setVisible(false);
theApplet.stop( );
theApplet.destroy( );
dispose( );
System.exit(0);
}


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.