• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 760
  • Last Modified:

ScrollPane in TextArea

How can I add JScrollPane to JTextArea??? What I want to do is to add scrollpane in text area so that I can see or view to the bottom of the text area... just like microsoft word or print preview...Here my code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.List;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class preview extends JFrame {
   
      Container container;
      
      public preview(){
            
            container = this.getContentPane();
        container.setLayout(null);
        container.setBackground(Color.gray);
            
        JPanel pan = new JPanel();
            JTextArea prevArea = new JTextArea();
            JScrollPane scroll = new JScrollPane();
            //prevArea.setText(item);
            //prevArea.setEditable(false);
            
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            
        prevArea.setBounds(40, 30, 700, 800);
        scroll.setBounds(40, 30, 750, 540);
        scroll.setPreferredSize(new Dimension(400,300));
        pan.add(scroll, BorderLayout.CENTER);
       
        container.add(prevArea);
            container.add(scroll);
            container.add(pan);
            
       
            
        setSize(800,600);
        setVisible(true);
       
        setDefaultCloseOperation( DISPOSE_ON_CLOSE );
            
            addWindowListener(
                         new WindowAdapter()
                         {
                            // disconnect from database and exit when window has closed
                            public void windowClosed( WindowEvent event )
                            {
                             
                               System.exit( 0 );
                             
                            } // end method windowClosed
                         } // end WindowAdapter inner class
                      ); // end call to addWindowListener
      }
      
      
      public static void main(String[] args) {
     
          JFrame frame = new JFrame();
        frame.setDefaultLookAndFeelDecorated(true);
          new preview();
   
    }//public static void main
}
0
mattjulius
Asked:
mattjulius
  • 3
  • 2
  • 2
  • +2
1 Solution
 
sciuriwareCommented:
JTextArea ta;
JScrollPane sp = new JScrollPane(ta = new JTextArea());

;JOOP!
0
 
sciuriwareCommented:
Continued:   add 'sp' to a container (JPanel, JFrame ...)
Access the textarea via 'ta'.

;JOOP!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> container.add(prevArea);

Might not be needed if you add the scroll-pane to the container.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sciuriwareCommented:
Read my code ....


;JOOP!
0
 
hoomanvCommented:
scrollpane.setViewportView(prevArea);
0
 
mattjuliusAuthor Commented:
How can I loop a text area just like microsoft word....
0
 
mon_pacey8Commented:
I review your code and you should remove to you code the line :

container.add(prevArea)

0
 
Mayank SAssociate Director - Product EngineeringCommented:
That was a duplicate comment - I already mentioned it.
0
 
hoomanvCommented:
you violate the law
:-O
0

Featured Post

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!

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