2014-04-24 89 views
0

我想要執行此任務,以便在每個任務執行時,已存在的JTextArea logText中的某些文本行將被追加。TextArea它動態顯示附加文本

錯誤是線路:

task = new Task(); 

什麼需要做的事情,使得我們得到一個JTextArea其動態顯示附加文本。

public class ScanAccount extends JFrame { 

private JPanel contentPane; 
private JTextField textField; 
private JPasswordField passwordField; 
private JTable table; 
private JTextArea logText = new JTextArea(); 
private static Task task; 
private static String statusText; 

class Task extends SwingWorker<Void, Void> { 
    /* 
    * Main task. Executed in background thread. 
    */ 
    @Override 
    public Void doInBackground() { 
     logText.append(statusText + "\n"); 
     logText.setCaretPosition(logText.getDocument().getLength()-1); 
     return null; 
    } 

    /* 
    * Executed in event dispatching thread 
    */ 
    @Override 
    public void done() { 
    } 
} 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       ScanAccount frame = new ScanAccount(); 
       frame.setVisible(true); 

       while(true) 
       { 
        Random random = new Random(); 
        statusText = "new text is " + random.nextInt(10000); 
        task = new Task(); 
        task.execute(); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public ScanAccount() { 
    setTitle("Scanning Mode : Email Accounts"); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    setBounds(100, 100, 450, 440); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    JLabel lblAccountInformation = new JLabel("Account Information :"); 
    lblAccountInformation.setBounds(12, 12, 168, 15); 
    contentPane.add(lblAccountInformation); 

    JLabel lblUsername = new JLabel("Username :"); 
    lblUsername.setFont(new Font("Dialog", Font.PLAIN, 12)); 
    lblUsername.setBounds(34, 37, 88, 15); 
    contentPane.add(lblUsername); 

    textField = new JTextField(); 
    textField.setBounds(113, 35, 285, 19); 
    contentPane.add(textField); 
    textField.setColumns(10); 

    JLabel lblPassword = new JLabel("Password :"); 
    lblPassword.setFont(new Font("Dialog", Font.PLAIN, 12)); 
    lblPassword.setBounds(34, 66, 88, 15); 
    contentPane.add(lblPassword); 

    passwordField = new JPasswordField(); 
    passwordField.setBounds(113, 64, 285, 19); 
    contentPane.add(passwordField); 

    JButton btnStartScan = new JButton("Start Scan"); 
    btnStartScan.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
     } 
    }); 
    btnStartScan.setBounds(308, 94, 117, 25); 
    contentPane.add(btnStartScan); 

    JLabel label_2 = new JLabel("Summary :"); 
    label_2.setBounds(22, 185, 95, 15); 
    contentPane.add(label_2); 

    table = new JTable(new DefaultTableModel(
      new Object[][] { 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
       {null, null}, 
      }, 
      new String[] { 
       "File Name", "Categeory" 
      } 
     )); 

    JScrollPane scrollPane = new JScrollPane(table); 
    scrollPane.setBounds(77, 212, 312, 187); 
    contentPane.add(scrollPane); 

    JLabel label = new JLabel("Status :"); 
    label.setBounds(25, 131, 70, 15); 
    contentPane.add(label); 

    JScrollPane scrollPane_status = new JScrollPane(logText); 
    scrollPane_status.setBounds(45, 152, 369, 21); 
    contentPane.add(scrollPane_status); 
} 
} 
+2

重讀的SwingWorker的documpentation。 doInBackground恰恰是您不能訪問Swing組件的地方,因爲它們必須從事件派發線程訪問。 –

回答

1

您的嵌套類需要一個外部類的實例,因爲它不是靜態的 - 但是您沒有外部類的實例。

嘗試進行嵌套類的靜態

static class Task extends SwingWorker<Void, Void> 

也使靜態這一申請

private static JTextArea logText = new JTextArea();