still2blue
still2blue

Reputation: 193

JScrollPane not scrolling horizontally

I have a program I am working on and cannot figure out why it isn't scrolling horizontally. I have set the scroll policies for both hor and ver however only vertical scroll is showing up. The program gets a txt file that the user chooses and then displays it in the jtextarea. All is working fine except the horizontal scroll. Any ideas?

Thanks!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.*;

class FileChooserDemo3{

   JLabel jlab;
   JButton jbtnShow;
   JFileChooser jfc;
   JTextArea jta;
   JScrollPane scrollPane;
   String filename = null;

   FileChooserDemo3() {
      //create new JFrame container.
      JFrame jfrm = new JFrame("JFileChooser Demo");

      //Specify FlowLayout for layout manager
      jfrm.setLayout(new FlowLayout());

      //Give the frame initial size
      jfrm.setSize(800,800);

      //End program when user closes application
      jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //Create a label to show selected file
      jlab=new JLabel();

      //Create button to show dialog
      jbtnShow = new JButton("Show File Chooser");

      //create textarea with ability to textwrap (p889-891) and scroll (hint:  Use JScrollPane)
      jta = new JTextArea(45, 40);
      jta.setLineWrap(true);
      JScrollPane scrollPane = new JScrollPane(jta);






      //Create file chooser starting at default directory
      jfc=new JFileChooser();
      String name = null;
      //Show file chooser when show file chooser button pressed
      jbtnShow.addActionListener(new ActionListener()  {
         public void actionPerformed(ActionEvent le) {
            //Pass null for the parent.  This centers the dialog on the screen.
            int result = jfc.showOpenDialog(null);

            if(result==JFileChooser.APPROVE_OPTION){
               jlab.setText("Selected file is:  " + jfc.getSelectedFile().getName());
               File file = jfc.getSelectedFile();
               jfrm.add(scrollPane);
               scrollPane.setVerticalScrollBarPolicy(
         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      scrollPane.setHorizontalScrollBarPolicy(
         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
               jta.setText("");
               Scanner in = null;

               //Get selected file stored as a file.


               try{
                  //Do file processing here
                  in = new Scanner(file);
                  while(in.hasNext()) {
                     String line = in.nextLine();
                     jta.append(line+"\n");
                  }
               }

               catch(IOException e){
                  System.out.println("Exception");

               }
               finally {
                  in.close();
              }    



            }
            else{
               jlab.setText("No file selected.");
            }
         }
      });

         //add the show file chooser button and label to content pane
         jfrm.add(jbtnShow);
         jfrm.add(jlab);


         //Display the frame
         jfrm.setVisible(true);
   }

   public static void main(String[] args){
      //Create GUI on the event dispatching thread.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new FileChooserDemo3();
         }
      });
   }
}

Upvotes: 1

Views: 628

Answers (2)

user4759923
user4759923

Reputation: 531

Turn off line wrap for your JTextArea using jta.setLineWrap(false); instead of jta.setLineWrap(true);. Otherwise no horizontal scroll bar is needed because the JTextArea wraps the lines such that it fits into the parent without scrolling.

Upvotes: 3

Reimeus
Reimeus

Reputation: 159864

Remove the line wrap statement to allow the child component of the scrollbar to a have a horizontal size greater than the viewport size

jta.setLineWrap(true); // remove

Upvotes: 3

Related Questions