Hi all,
Pls explain me about Timer and Thread.May I know the usage of Timer and Thread with examples if possible.I can't control these Class very well.Thank in advance.Pls help me.

Recommended Answers

All 2 Replies

Post your relevant code and tell us what part you're having trouble with. If your problem is not knowing enough about Timer and Threads, then read google. We'll be happy to help you with specific problems, however.

Thank your reply.At a forum,Tam give me a program here.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MarqueeFrame extends JFrame implements ActionListener {

    private Timer timer;
    private MarqueePanel marqueePanel;

    // Timer Event Handler
    public void actionPerformed(ActionEvent e) {
        // move the Label in the MarqueePanel
        marqueePanel.moveLabel();
    }

    public MarqueeFrame() {

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(d.width,d.height);
        setTitle("HyperLink MARQUEE - Developed by TamizhVendan.S, Born to Win");
        setResizable(false);

        timer = new Timer(150, this);
        marqueePanel = new MarqueePanel(timer);
        add(marqueePanel);
        timer.start();
        
    }

    public static void main(String[] args) {
        new MarqueeFrame().setVisible(true);
    }

}

I use it at my application.
My needs are

1. want to add marquee panel at my application frame.
2.when I click menuitem,marquee texts will changes in a panel.
3.At that marquee panel has many JLabels with links

Here is my Marquee.java coding .It is the portion of my application.So you can't run it.So I am afraid to bore when you see my program. I am sorry for that.If possible ,pls help me.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.*;

public class Marquee extends JPanel implements ActionListener
{
     private Timer timer;
     private MarqueePanel marqueePanel;
     LayoutSettings settings;
        // Timer Event Handler
        public void actionPerformed(ActionEvent e) {
            // move the Label in the MarqueePanel
            System.out.println("Action Performed");
            int locationx = marqueePanel.getxLocation();
            marqueePanel.moveLabel(locationx);
        }

     /** Creates a new instance of Marquee **/
    public Marquee(int link) 
    {
        //this.setSize(50,10);
        this.setOpaque(false);
        timer = new Timer(200, this);
        marqueePanel = new MarqueePanel(timer,link);
        //marqueePanel.setSize(300,20);
        add(marqueePanel);                       //add marquee panel at my application
        validate();
        timer.start();
    }
  public void changeRSSLink(int j)     //when I click a menuitem,change marquee text
{
    remove(marqueePanel);
    validate();
    timer = new Timer(200, this);
        marqueePanel = new MarqueePanel(timer,j);
        add(marqueePanel);
        validate();
        timer.start();
}
}
class MarqueePanel extends JPanel 
{
   private int xLoc,yLoc;
    private Dimension dimension;
    private Timer timer;
    String[][] rssString;
    JLabel[] lblarray;
    //JLabel[][] changeLabelary;
    
    private LayoutSettings settings;
    public MarqueePanel(final Timer timer,int link) 
    {
        System.out.println("MarqueePanel");
        //this.setSize(300,20);
        settings = LayoutSettingsManager.getLayoutSettings();
        setBackground(new Color(200,213,230));
        this.timer = timer;
        //setLayout(null);
        //dimension = Toolkit.getDefaultToolkit().getScreenSize();
       
        RSSReader reader = RSSReader.getInstance();
        //rssString = new String[count][2];
          rssString=reader.writeNews(link);      //get the marquee text and web page link to go
          makePanel(rssString,0);
          
    }
  
   public void makePanel(String[][] Stringary,int change)
    {
       lblarray = new JLabel[Stringary.length];

          for(int i=0;i 0) {
                        try {
                         Runtime.getRuntime().exec("cmd.exe /c start "+linklbl);
                        } catch (IOException ex) {
                            System.out.println(ex.getMessage());
                        }
                    }
                }
                public void mouseEntered(MouseEvent e) {
                    timer.stop();
                }

                public void mouseExited(MouseEvent e) {
                    timer.start();
                }
                
            }); 
          add(lblarray[i]);
          validate();
              
           }
          moveLabel(2800);
    }
  
    public void moveLabel(int locationx) 
    {
        xLoc = locationx;
        Rectangle r = new Rectangle();
        //r.setSize(300, 20);
        r.x=xLoc;
        r.y=yLoc;
        Dimension size;
        int count = 0;
        for(int i=0;i< -(count-1000))
            xLoc = 2500;
        System.out.println("x Location = "+xLoc);
    }
    public int getxLocation()
    {
        return xLoc;
    }
  }

My problems are

1.Marquee text's movement more quick,when I click a menuitem one times and one times.
2.When I resize my frame,frame expands as much as Marquee text's length has.My marquee text is very very long.May be nearly 4000 width of text.(At that time ,I can't resize my frame)

Thank in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.