Question

I want to execute this task such that on each task execution some text lines in the already present JTextArea logText gets appended.

Error is the line :

 task = new Task();

what needs to be done such that we get a JTextArea which Dynamically shows appended text.

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);
}
}
Was it helpful?

Solution

Your nested class requires an instance of the outer class, because it's not static - but you don't have an instance of the outer class.

Try to making nested class static

static  class Task extends SwingWorker<Void, Void>

also make static this filed

private static  JTextArea logText = new JTextArea();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top