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:
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.
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.
Try Java7 - The try-with-resources Statement to close the resources after the program is finished with it.
First don't use null
layout. There are lots of layout manager that is designed for this purpose.
See Swing Tutorial on How to Use Various Layout Managers
Call frame.setVisible(true)
in the end to make sure everything is added in JFrame
Use SwingUtilities.invokeLater() or EventQueue.invokeLater() to make sure that EDT is initialized properly.