1

Hi there fellow comrades!
Thanks to you all I've managed to get my small experiment project going further but as you go further there are always obsticles 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 sever 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 that is trying to understand the mechanics of Java programming language and sink in into the world of Java. Thank you all in advance!

Artur
  • 140
  • 1
  • 10
  • Thank you both, at last this is now solved. The problem was "frame.add(area);" so the area was attached to the frame not the scrollPane. Thank you alot! Never thought that this one line could change so much. I will learn now about LayoutManagers aswell! :) Good day to you both! :) – Artur Jul 19 '14 at 21:02
  • 1
    Sounds good. Happy learning. :) – Braj Jul 19 '14 at 21:03

2 Answers2

2

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

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 1
    Add don't "add" content to the scroll pane, it should be applied to the viewport ;) – MadProgrammer Jul 19 '14 at 22:20
  • @MadProgrammer sorry I didn't get your point. `new JScrollPane(area)` adds the area in the viewport of scroll pane. – Braj Jul 19 '14 at 22:22
  • 1
    When I read through the answer, you highlight a number of good points, but I didn't seem like you highlighted the fact that the op was adding content to the scroll pane instead of setting the viewports view, it just doesn't seem like yu highlighted that issue that's all – MadProgrammer Jul 19 '14 at 23:12
2

You may try this code

JScrollPane scrollPane = new JScrollPane(area);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

not necessary to use view port in this case, the same is concerned to revalidation and repainting before frame is visible.

all you need is frame.getContentPane().add(scrollPane);

Roman C
  • 49,761
  • 33
  • 66
  • 176