Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:37:54+00:00 2026-05-31T02:37:54+00:00

I am using swing worker to validate files, I want the startLine and endLine

  • 0

I am using swing worker to validate files,
I want the startLine and endLine to be sent to the validate class as i do not want to validate the whole file every time. For the first time when an existing file is openend, i want to send the startLine as 0 and endLine as endLine = editorTextArea.getLineCount() – 1;. After that i should be able to send the startLine and endLine to my convenience every second. How do i achieve this?

Validate class:

  class Validate implements Runnable {

     private JTextArea editorTextArea;
     private JTextArea errorTextArea;
     private int startLine;
     private int endLine;

     public Validate(JTextArea editor, JTextArea error, startLine, endLine) {
         this.editorTextArea = editor;
         this.errorTextArea  = error;
         this.startLine = startLine;
         this.endLine = endLine;        
    }

@Override
public void run() {
    SwingWorker worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            //CODE TO VALIDATE
            return null;
        }

        @Override
        protected void done() {                
            //CODE TO DISPLAY THE RESULT
        }
    };
    worker.execute();
    }
 }

Main class:

  //Calls the validate
   public void taskExecutor() {               
          ScheduledExecutorService scheduler =
                 Executors.newSingleThreadScheduledExecutor();
           final ScheduledFuture<?> timeHandle =
                 scheduler.scheduleAtFixedRate(new Validate(editorTextArea,   errorTextArea), 0, 1, SECONDS);                 
}


    private void openFileActionPerformed(java.awt.event.ActionEvent evt)      {                                      

  fileChooser.setCurrentDirectory(new File(".txt"));
int result = fileChooser.showOpenDialog(new JPanel());
int totLines = 0;
String[] content = null;

if (result == JFileChooser.APPROVE_OPTION) {
    try {
        filename = String.valueOf(fileChooser.getSelectedFile());
        setTitle(filename);

        FileReader fr = new FileReader(filename);
        editorTextArea.read(fr, null);
        fr.close();
        startLine = 0;
        endLine = editorTextArea.getLineCount() - 1;
        //here i want to call the validate class once. that is askExecutor();                
    } catch (IOException ex) {
        System.out.println(ex);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:37:55+00:00Added an answer on May 31, 2026 at 2:37 am

    A SwingWorker uses an ExecutorService internally, but the worker “is only designed to be executed once.” I don’t see why you’re wrapping the worker in a Runnable that’s scheduled at a fixed rate. Instead, execute() the task once and let it publish() interim results that can be process()ed on the EDT. For line numbers, SwingWorker<Boolean, Integer> may be appropriate. Integer would represent the last line number processed, while Boolean would represent the final validation result returned by doInBackground().

    If the user is aynchronously selecting multiple files for validation, consider adding each executing worker to a suitable TableModel and displaying the results in the corresponding JTable. @mKorbel has shown several examples featuring JProgressBar.

    Addendum: If you’re validating additions to the JTextArea, you’ll want to execute() a new worker each time, passing the new range of line numbers as parameters to the worker’s constructor. This example, which passes a single int count, may suggest the approach. The trigger can be a java.util.Timer, a javax.swing.Timer or even the ScheduledExecutorService you originally proposed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using javax.swing.SwingWorker for the first time. I want to update a JLabel
I am using threading in application through Swing Worker class. It works fine, yet
I'm making a GUI using Java (who isn't?). I know the Swing Worker class
I am trying to run a swing worker using the Executor Service and something
Do I always have to return something using Worker Thread in Swing? What if
hi i'm using swing and in my main frame(JFrame) i want that when ever
I'm using Swing for the first time to create a simple GUI. It consists
I have a desktop application using Swing library. Application is running a batch file.
Using Swing in Java I wrote a form containing radio buttons, text fields and
I created a form using Swing in Java. In the form I have used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.