Solved

Adding a scrollbar to a jtextfield

Posted on 2006-12-01
7
923 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
Independent Software Vendors: 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!

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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