2016-06-28 66 views
1

我輸出一些登錄到封閉在JScrollPane的一個JTextArea但自動滾動功能,當輸出達到textarea的底部是行不通的。我嘗試了幾種我在網上看到的方法,但都沒有成功。以下是我的代碼到目前爲止的一部分。JScrollPane的和JTextArea的滾動

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); 

    } 
} 

回答

1

你想要做的是閱讀在一個單獨的線程文件,讓你的揮杆線程不會阻止它,可以讓你在同一時間更新文本區是什麼。

你仍然需要但是更新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(); 
     }); 
    } 
} 
+0

我幾乎相同的代碼,看到我的編輯。我不知道爲什麼它不起作用。 – fanbondi

+0

@fanbondi我會更新我的答案 – explv

+0

所以基本上它像textarea的更新被阻止主線程。 – fanbondi

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); 
} 
+0

可以請你檢查我的編輯,我已經提供了完整的源代碼,因爲我有您的兩個方法和,但它不是工作。 – fanbondi

+0

你在對方的回答您的評論的結論是正確的:你阻塞事件調度線程,從而防止任何部件從畫。請參見[併發中的Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)。 – VGR