943,537 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 24385
  • Java RSS
Oct 17th, 2003
0

write into and read from JTextField

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nicole_adachi is offline Offline
1 posts
since Oct 2003
Oct 21st, 2003
0

Re: write into and read from JTextField

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sooim3 is offline Offline
11 posts
since Jun 2003
Jul 5th, 2004
0

Re: write into and read from JTextField

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

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
WannaBe is offline Offline
3 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java code newbie
Next Thread in Java Forum Timeline: Error trying to run java application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC