我正在将一些日志输出到JScrollPane中包含的JTextArea中,但是当输出到达textArea的底部时,自动滚动功能不起作用。我尝试了几种在网上看到但没有效果的方法。以下是我目前为止的部分代码。
JTextArea ouputLogPane = new JTextArea();
JScrollPane outputPane = new JScrollPane(ouputLogPane);
outputPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
outputPane.setBounds(75, 501, 746, 108);
contentPane.add(outputPane);
现在我在另一个类中从源文件读取并使用下面的代码将日志详细信息附加到textArea。
public void readFile(JTextArea outputLog, JScrollPane scrollPane){
count = 0;
while(moreLinesToRead){
if(count % 100 == 0){
outputLog.update(outputLog.getGraphics());
outputLog.append("Completed Reading"+ count + " Records "\n");
DefaultCaret caret = (DefaultCaret)outputLog.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
outputLog.update(outputLog.getGraphics());
//tried the one below but did not work either
//outputLog.setCaretPosition(outputLog.getDocument().getLength());
}
count++;
}
}
最后,我在点击按钮时调用此方法,如下所示。
JButton btnNewButton = new JButton("Start Reading");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
migrationUtil.readFile(ouputLogPane,outputPane);
}
});
所以基本上完整的输出只在执行结束后打印。我读到我可能必须使用一个单独的线程来处理它,但不太确定如何继续。
修改
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class ReadingExample extends JFrame {
private JPanel contentPane;
private Connection conn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ReadingExample frame = new ReadingExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ReadingExample() {
//setResizable(false);
setFont(new Font("Dialog", Font.BOLD, 13));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 936, 720);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new LineBorder(new Color(0, 0, 0), 2));
setContentPane(contentPane);
contentPane.setLayout(null);
final JTextArea ouputLogPane = new JTextArea();
final JScrollPane outputPane = new JScrollPane(ouputLogPane);
//outputPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
outputPane.setBounds(67, 189, 746, 108);
contentPane.add(outputPane);
JButton btnNewButton = new JButton("Start Reading");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
File file = new File("file.txt");
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
try {
while((line = bufferedReader.readLine()) != null) {
ouputLogPane.append(line + "\n");
ouputLogPane.setCaretPosition(ouputLogPane.getDocument().getLength());
try {
Thread.sleep(200);
} catch (InterruptedException ee) {
ee.printStackTrace();
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 14));
btnNewButton.setBounds(358, 620, 167, 29);
contentPane.add(btnNewButton);
//JPanel panel_3 = new JPanel();
//panel_3.setBorder(new TitledBorder(null, "Process Log", TitledBorder.LEADING, TitledBorder.TOP, null, null));
//panel_3.setBounds(57, 173, 769, 132);
//contentPane.add(panel_3);
}
}
答案 0 :(得分:1)
你想要做的是在一个单独的线程中读取文件,这样你的Swing线程就不会被它阻止,允许你同时更新文本区域。
您仍然需要更新Swing线程上的GUI,因此您可以通过调用SwingUtilities.invokeLater(runnable)
来执行此操作。
这是一个工作示例(注意我添加了Thread.sleep(200)
,以便您可以看到它正在更新):
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class ReadingExample {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
JTextArea jTextArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(jTextArea);
scrollPane.setPreferredSize(new Dimension(300, 300));
mainPanel.add(scrollPane, BorderLayout.CENTER);
JButton btnNewButton = new JButton("Start Reading");
mainPanel.add(btnNewButton, BorderLayout.SOUTH);
jFrame.setContentPane(mainPanel);
jFrame.pack();
jFrame.setVisible(true);
btnNewButton.addActionListener(e -> {
new Thread(() -> {
File file = new File("file.txt");
try (FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader)) {
String line;
while((line = bufferedReader.readLine()) != null) {
final String fLine = line;
SwingUtilities.invokeLater(() -> {
jTextArea.append(fLine + "\n");
jTextArea.setCaretPosition(jTextArea.getDocument().getLength());
});
Thread.sleep(200);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}).start();
});
}
}
答案 1 :(得分:0)
滚动到底部有两种方法。您可以操纵滚动条:
JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
scrollBar.setValue(scrollBar.getMaximum());
或者,您可以使用更可靠的scrollRectToVisible
方法:
try {
textArea.scrollRectToVisible(
textArea.modelToView(
textArea.getDocument().getLength()));
} catch (BadLocationException e) {
throw new RuntimeException(e);
}