Hello,

I have a program below where I am selecting a file and displaying the content using a JTextArea. It works fine, but when I add a JScroller, it does not display the area with the scroller. What am I doing wrong?

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

public class ChooseFileAndDisplay extends JPanel implements ActionListener{
    private JButton button;
    private JPanel panel;
    private static JFrame f;
    private JTextArea area = new JTextArea();
    private JScrollPane scroller;
    public ChooseFileAndDisplay(){
        button = new JButton ("Click me");
        button.setPreferredSize(new Dimension(100, 50));
        scroller = new JScrollPane(area);
        button.addActionListener(this);
        JScrollPane scrollPane = new JScrollPane(area);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        add (button);
        add(scrollPane);
    }

    public void actionPerformed (ActionEvent e){
        JFileChooser choose = new JFileChooser();
        choose.showOpenDialog(f);
        File f = choose.getSelectedFile();
        String filepath =f.getAbsolutePath();
        try{
            FileReader reader = new FileReader(filepath);
            BufferedReader br = new BufferedReader(reader);
            area.read(br, null);
            br.close();
        }
        catch (Exception E){
            JOptionPane.showMessageDialog (null, E);
        }
    }

    public static void main (String args[]) {
        ChooseFileAndDisplay P = new ChooseFileAndDisplay();
        f = new JFrame ("Picture Grid");
        f.add(P);
        f.setSize(800, 800);
        f.setVisible (true);
        f.setResizable(false);
    }
}

Recommended Answers

I'm guessing you meant this example and scroller at https://www.jque.re/plugins/scrollbars-onscroll/jscroller/index.html

In example 4 there, the scroll bars do hide when the mouse is not on the image.

Jump to Post

All 2 Replies

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.