write into and read from JTextField

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2003
Posts: 1
Reputation: nicole_adachi is an unknown quantity at this point 
Solved Threads: 0
nicole_adachi nicole_adachi is offline Offline
Newbie Poster

write into and read from JTextField

 
0
  #1
Oct 17th, 2003
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 11
Reputation: sooim3 is an unknown quantity at this point 
Solved Threads: 0
sooim3 sooim3 is offline Offline
Newbie Poster

Re: write into and read from JTextField

 
0
  #2
Oct 21st, 2003
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/tutor...ts/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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 3
Reputation: WannaBe is an unknown quantity at this point 
Solved Threads: 0
WannaBe WannaBe is offline Offline
Newbie Poster

Re: write into and read from JTextField

 
0
  #3
Jul 5th, 2004
// 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[i] );
}
}

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);
}

}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC