[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

Adding a scrollbar to a jtextfield

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
twibblejaway
Asked:
twibblejaway
  • 4
  • 3
1 Solution
 
suprapto45Commented:
Why don't you use JTextArea?
0
 
twibblejawayAuthor Commented:
sorry, I meant jtextarea... that is what i'm using
0
 
suprapto45Commented:
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
twibblejawayAuthor Commented:
No luck.  Anyone else?
0
 
suprapto45Commented:
         //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
 
suprapto45Commented:
???
0
 
twibblejawayAuthor Commented:
Well I dont know why I dont get a scrollbar when I copy and paste that.  
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now