Textfield control creates a single-line text area. The text field allows the user to enter and edit the text using cut, copy and paste keys, arrow keys and mouse selections. It is an object of JTextField class which is a subclass of JTextcomponent.
Some of the constructors defined by JTextField class are as follows.
JTextField ()
JTextField(int cols)
JTextField(String string, int cols)
JTextField(String string)
where,
string is the initial string contained in the text field
cols is the width of the text field in terms of columns
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class JavaExampleTextFieldScrollInJApplet extends JApplet
{
private JTextField Txt = new JTextField("Welcome To Java",12);
public void init()
{
Container Cntnr = getContentPane();
Cntnr.setLayout(new FlowLayout());
Cntnr.add(new JSliderPane());
Cntnr.add(Txt);
}
class JSliderPane extends JPanel
{
BoundedRangeModel BndRngModl = Txt.getHorizontalVisibility();
JSlider Sldr = new JSlider(Txt.getHorizontalVisibility());
public JSliderPane()
{
add(new JLabel("Scroll Text Field"));
BndRngModl.setExtent(0);
Sldr = new JSlider(BndRngModl);
add(Sldr);
Sldr.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e1)
{
Txt.setScrollOffset(Sldr.getValue());
}
});
}
}
}
/*<APPLET CODE=JavaExampleTextFieldScrollInJApplet.class WIDTH=350 HEIGHT=280></APPLET>*/