Solved

Adding a scrollbar to a jtextfield

Posted on 2006-12-01
7
912 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
Comment Utility
Why don't you use JTextArea?
0
 

Author Comment

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

Expert Comment

by:suprapto45
Comment Utility
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:twibblejaway
Comment Utility
No luck.  Anyone else?
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 500 total points
Comment Utility
         //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
Comment Utility
???
0
 

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

9 Experts available now in Live!

Get 1:1 Help Now