You are here:   HomeJava ProgrammingSwingJava Swing JTextField Scrollbar Example
by Dinesh Thakur Category: Swing

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>*/

Java Swing JTextField Scrollbar Example





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article