javaeclipseswingjscrollpanejtextarea

How to use JScrollPane with JTextArea in Swing?


I've managed to get my small experiment project going further, but as you go further, there are always obstacles, so I again ran into one.

I got a JTextArea outputting a list of running processes, but JScrollPane isn't showing up, but when I resize the window itself the JScrollPane hovers over the whole JTextArea.

Looks like this:

jscrollpane glitching

After several hours of scratching the back of my head I decided to ask you guys!

Code is:

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.io.*;
    import javax.swing.*;
    
    public class JTask {
        
        JButton button = new JButton();
        JFrame frame = new JFrame();
        JTextArea area = new JTextArea();
        JScrollPane scrollPane = new JScrollPane();
        
        JTask() throws IOException{
            frame.setBounds(100, 100, 1000, 700);
            frame.setLayout(null);
            area.setFont(new Font("monospaced", Font.PLAIN, 14));
            area.setBounds(5,25,972,500);
            scrollPane.add(area);
            scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPane.setBounds(5,25,972,500);
            scrollPane.setViewportView(area);
            
            scrollPane.setEnabled(true);
            scrollPane.setVisible(true);
            scrollPane.repaint();
            scrollPane.revalidate();
            area.setVisible(true);  
            
            frame.add(scrollPane);
            frame.add(area);
            
            frame.setVisible(true);
            
            try {
                String line;
                Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                    area.append(line);
                    area.append("\n");
                }
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        }
    
    public static void main(String args []) throws IOException{
        JTask task = new JTask();
        
    }
}

I apologize for a messy code, and if anything could be done better, but I hope you understand, that I am just a beginner, who is trying to understand the mechanics of Java programming language and sinking in the world of Java.


Solution

  • Why are you using two JScrollPane? Simply add JScrollPane in JFrame and JTextArea in JScrollPane.

    sample code:

    public JTask() throws IOException {
        JFrame  frame = new JFrame();
        JTextArea  area = new JTextArea();
        area.setFont(new Font("monospaced", Font.PLAIN, 14));
        JScrollPane  scrollPane = new JScrollPane(area);
        frame.add(scrollPane);
        ...
        // set text in JTextArea
        ...
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    

    It's worth reading Swing Tutorial on How to Use Text Areas to learn more and find more detail examples.

    Some points