-1

I have a requirement in swing. I need to open a textarea after regular interval of time (say 15 sec) repeatedly.

Here is the code of display the textarea

import javax.swing.*;  
public class TextAreaExample  
{  
     TextAreaExample(){  
        JFrame f= new JFrame();  
        JTextArea area=new JTextArea("Welcome to javatpoint");  
        area.setBounds(10,30, 200,200);  
        f.add(area);  
        f.setSize(300,300);  
        f.setLayout(null);  
        f.setVisible(true);  
     }  
public static void main(String args[])  
    {  
   new TextAreaExample();  
    }} 

Now I think we have to add a thread here which would open the textarea again and again after some interval of time. Right? If so, where should I add the thread related code?

Can anyone add the thread related part in the above code?

RDX
  • 145
  • 3
  • 12
  • 1
    Welcome to StackOverflow! Just a friendly reminder, no one here is paid to do work for you. If you have a specific question about implementation, I'm sure there are plenty of people who would love to help you. Unfortunately, we aren't a code writing service. – CraigR8806 May 11 '17 at 11:06
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) To switch between displayed and not displayed, use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). .. – Andrew Thompson May 11 '17 at 11:34

1 Answers1

2

I suggest using Swing Timers. A Swing timer (an instance of javax.swing.Timer) fires one or more action events after a specified delay. Here is a simple example:

timer = new Timer(speed, this); // this: class implementing ActionListener
timer.setInitialDelay(pause);
timer.start(); 

void actionPerformed(ActionEvent e) {
    ...
  • So that will fire the events again and again or just once? I need to fire the event again and again. – RDX May 11 '17 at 11:19
  • Yes, repeatedly. I suggest taking a look at the api: https://docs.oracle.com/javase/8/docs/api/javax/swing/Timer.html –  May 11 '17 at 11:30