| | |
write into and read from JTextField
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2003
Posts: 1
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jun 2003
Posts: 11
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2004
Posts: 3
Reputation:
Solved Threads: 0
// 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);
}
}
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);
}
}
![]() |
Similar Threads
- how to write and read array record into file?? (Pascal and Delphi)
- Write/ Read an object (Visual Basic 4 / 5 / 6)
- Write and Read text files (Java)
- create, write & read file to/from folder (C++)
- Using the STL LIst Container, how do I create, write,read, and store in file. (C++)
- How can you tell how fast a hard drive can write and read? (Storage)
Other Threads in the Java Forum
- Previous Thread: java code newbie
- Next Thread: Error trying to run java application
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working





