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 8609539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:54:15+00:00 2026-06-12T03:54:15+00:00

I have made a small swing application for my personal use. This app connects

  • 0

I have made a small swing application for my personal use. This app connects to internet, retrieves a text file, displays its contents in a jTextArea and saves the text file locally (in my hardisk). So far so good.

Now I want to receive this text file on a fixed regular interval (say after every 30 seconds). I can do this via for/while loops (and thread.sleep methods), but I know this is not the right way as it does not allow me pause/resume the receiving. I have tried solving my problem using ‘threads’ and ‘callable’ but not able to succeed. (I do not have much knowledge of these)

Can you please guide me how to collect string value from a thread? Am I doing the right thing using Callable/Future? (please see my code). I should be able to pause / resume the text collection.

(During pause and resume my software I will some manual text editing and saving it to the disk. But I can handle that.)

Below is my code which I tried.

public class AmybactUI extends javax.swing.JFrame {

    public AmybactUI() {
        initComponents();
    }
    private void initComponents()  { .... }

    public static void main(String args[]) throws Exception {

    //    (Look and feel setting code here)

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AmybactUI().setVisible(true);
            }
        });

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        List<Future<String>> results = executor.invokeAll(Arrays.asList(new GetScore()), 30, TimeUnit.SECONDS);
        for (Future<String> result : results) {
            if (!result.isCancelled()) {
                   jTextAreaOnlineScore.append(result.get()+"\n");
                   jTextAreaOnlineScore.setCaretPosition(jTextAreaOnlineScore.getDocument().getLength());
                } else {
                    System.out.println("Task timed out.");
                }
                executor.shutdown();
            }            
    }

    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonSaveSMS;
    private javax.swing.JLabel jLabelTitle;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItemAbout;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemOptions;
    private javax.swing.JMenu jMenuTools;
    private javax.swing.JPanel jPanel;
    private static javax.swing.JRadioButton jRadioButtonOffline;
    public static javax.swing.JRadioButton jRadioButtonOnline;
    private javax.swing.JScrollPane jScrollPane;
    public static javax.swing.JTextArea jTextAreaOnlineScore;
    private javax.swing.JTextField jTextFieldCustomSMS;
    // End of variables declaration                   
}

class GetScore implements Callable<String> {
    public String call() throws Exception {        
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String nextLine;
        String myString = "";
        String myTryString;
        URL url;
        URLConnection urlConn;
        InputStreamReader inStream;
        BufferedReader buff;
        FileWriter fustream;
        BufferedWriter bfout;  
        try {
//         (some code here which connects to internet and gets my text)
            } else {
              break;
            }
         }
            bfout.write(myString);
            bfout.close();           
        } catch(MalformedURLException e) {
            System.out.println("Please check the URL:" + e.toString());
        } catch(IOException  e1) {
            System.out.println("Can't read from the Internet: " + e1.toString());
        }
        Thread.sleep(4000);
        return (""+sdf.format(cal.getTime())+myString);
    }
}
  • 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-06-12T03:54:16+00:00Added an answer on June 12, 2026 at 3:54 am

    Actually, you don’t need to use Futures in order to get results. Do the following:

    • make GetScore as Runnable
    • pass TextArea to the constructor of GetScore
    • when GetScore.run method will be executed, wither use textArea.setText method, or use SwingUtilities.invokeLater

    like:

     SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            textArea.setText(text);
         }
     );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a small java swing application that I want to share with
I have made a small application in Java and I would like to make
I have made an update on Google App Engine with a small fix and
I made a simple Swing application. But the rendering behaves buggy. Have I done
I have made a small windows application with visual studio 2008 ,its working properly
In the bug fixing of a small ASP.NET/C# web app I have made, I
I have made a small application in Windows Azure. For research purposes they asked
I have made a small Java .jar GUI application. When I run it on
We have a Flash app (AS3). This is a desktop application that runs in
I have made some small macro that I am using to display text line

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.