Solved

Adding a scrollbar to a jtextfield

Posted on 2006-12-01
7
915 Views
Last Modified: 2006-12-17
How can i make the JTextField in the following code have a scrollbar?  I've tried results.setCaretPosition(results.getText().length() - 1); after i've filled it, but that
doesnt have any effect.

public class Interface extends JPanel implements ActionListener
      {
            private JLabel component1;
            private JTextField searchString;
            private JLabel component3;
            private JTextField alphaValue;
            private JLabel component5;
            private JTextArea results;
            private JButton component7;
            
            public Interface()
            {
                  //construct components
                              
                  component1 = new JLabel ("Search Term:");
                  searchString = new JTextField (5);
                  component3 = new JLabel ("Alpha Value:");
                  alphaValue = new JTextField (5);
                  component5 = new JLabel ("Results");
                  results = new JTextArea (5, 5);
                  component7 = new JButton ("Search");
                  
                  component7.addActionListener(this);

                  //adjust size and set layout
                  setPreferredSize (new Dimension (747, 480));
                  setLayout (null);

                  //add components
                  
                  add (component1);
                  add (searchString);
                  add (component3);
                  add (alphaValue);
                  add (component5);
                  add (results);
                  add (component7);

                  //set component bounds (only needed by Absolute Positioning)
                  component1.setBounds (5, 5, 80, 25);
                  searchString.setBounds (85, 5, 500, 25);
                  component3.setBounds (611, 5, 73, 25);
                  alphaValue.setBounds (685, 5, 60, 25);
                  component5.setBounds (345, 95, 45, 25);
                  results.setBounds (50, 120, 650, 350);
                  component7.setBounds (265, 45, 100, 25);
                               }
            }
0
Comment
Question by:twibblejaway
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 18052111
Why don't you use JTextArea?
0
 

Author Comment

by:twibblejaway
ID: 18052115
sorry, I meant jtextarea... that is what i'm using
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 18052122
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:twibblejaway
ID: 18052165
No luck.  Anyone else?
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 500 total points
ID: 18052215
         //add components
           JScrollPane p = new JScrollPane
          (
               results,
               JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
               JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
          );
         
          add (component1);
          add (searchString);
          add (component3);
          add (alphaValue);
          add (component5);
          add (p);
          add (component7);

          //set component bounds (only needed by Absolute Positioning)
          component1.setBounds (5, 5, 80, 25);
          searchString.setBounds (85, 5, 500, 25);
          component3.setBounds (611, 5, 73, 25);
          alphaValue.setBounds (685, 5, 60, 25);
          component5.setBounds (345, 95, 45, 25);
          p.setBounds (50, 120, 650, 350);
          component7.setBounds (265, 45, 100, 25);

That works fine on me.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 18073449
???
0
 

Author Comment

by:twibblejaway
ID: 18156426
Well I dont know why I dont get a scrollbar when I copy and paste that.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 23
JDeveloper 12c for 32 bit 4 71
Problem to start Neon 20 88
java jdbc batch example 8 22
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now