45,000 Jobs - Get an Interview Call,  Post Your Resume Here
SURESHKUMAR.NET FORUMS
Registered Member Login:
Not a member? Register today!



Welcome to the SURESHKUMAR.NET FORUMS.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.




A java swing program to marquee a text

        

Reply
 
LinkBack Thread Tools Display Modes
Old 27-06-07, 11:35 PM   #1 (permalink)
TAM
Senior Member
 
TAM's Avatar
 
Join Date: Oct 2006
Location: Mayiladurai (TN)
Posts: 243
Thanks: 8
Thanked 12 Times in 8 Posts
Rep Power: 7 TAM will become famous soon enough TAM will become famous soon enough
Exclamation A java swing program to marquee a text

/****************************************************
* A JAVA SWING PROGRAM TO MARQUEE A TEXT
* JAVA VERSION : 5.0
* AUTHOR : TAMIZHVENDAN.S
* NICKNAME : TAM
* FOR FURTHER DETAILS CONTACT tamizh88@gmail.com
****************************************************/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MarqueeTest
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class MyFrame extends JFrame implements ActionListener
{
private ActionListener listener;
private Timer t1;

public MyFrame()
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setSize(d.width,d.height);
setTitle("MARQUEE");
MyPanel panel = new MyPanel();
add(panel);
listener = this;
t1 = new Timer(50,listener);
t1.start();
}
public void actionPerformed(ActionEvent event)
{
repaint();
}

}

class MyPanel extends JPanel
{
private int x,y;
private Dimension d ;

public MyPanel()
{
d= Toolkit.getDefaultToolkit().getScreenSize();
x = d.width - 100;
y = 25;

}

public void paintComponent(Graphics g)
{
x -= 5;
if ( x < -250)
x = d.width;
Graphics2D g2 = (Graphics2D) g;

Font font = new Font("Comic Sans MS",Font.BOLD,25);
g2.setFont(font);
g2.setPaint(Color.BLUE);

g2.drawString("TAM - BORN TO WIN",x,y);
}
}


Last edited by TAM; 31-08-07 at 12:58 AM..
TAM is offline Offline   Reply With Quote
The Following User Says Thank You to TAM For This Useful Post:
Sandar Khin (30-10-09)
Old 31-10-09, 02:38 PM   #2 (permalink)
Junior Member
 
Join Date: Oct 2009
Age: 22
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 Sandar Khin is on a distinguished road
Re: A java swing program to marquee a text

Hi TAM
I want to open webpage,when I click "TAM - BORN TO WIN" String.How will I do it?Pls help me.Mainly,I want to place a "JLabel Component" at the "TAM-BORN TO WIN" String.Thank in advance.
Sandar Khin is offline Offline   Reply With Quote
Old 02-11-09, 01:28 PM   #3 (permalink)
TAM
Senior Member
 
TAM's Avatar
 
Join Date: Oct 2006
Location: Mayiladurai (TN)
Posts: 243
Thanks: 8
Thanked 12 Times in 8 Posts
Rep Power: 7 TAM will become famous soon enough TAM will become famous soon enough
Re: A java swing program to marquee a text

Yeah.. you can do..
Just move the coordinates of the Label in the timer event.
Handle the click event of the Label to open the Web Page
TAM is offline Offline   Reply With Quote
Old 04-11-09, 10:31 AM   #4 (permalink)
Junior Member
 
Join Date: Oct 2009
Age: 22
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 Sandar Khin is on a distinguished road
Re: A java swing program to marquee a text

Hi Tam
Pls help me again.I am ok with your idea.But I want to make multile JLabel or JTextField with multiple Link in Marquee.I can do only one.Here is my code
[code]

import java.awt.*;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class MovingText extends JFrame implements ActionListener{
JLabel label;
JLabel label2;
JLabel label3;
public MovingText()
{
label = new JLabel( "Hello World,Where there is will there is& nbsp;a way");
//label2 = new JLabel(" ......Testing");
//label3 = new JLabel(label.getText().concat(label2.getText()));
getContentPane().add(label, BorderLayout.NORTH);
/*label.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() > 0){
try {
Process pc = Runtime.getRuntime().exec("cmd.exe /c start http://www.iwebie.com");
} catch (IOException ex) {
System.out.println(ex.getMessage());
System.out.println();
}
}
}
});
label2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() > 0){
try {
Process pc = Runtime.getRuntime().exec("cmd.exe /c start http://www.google.com");
} catch (IOException ex) {
System.out.println(ex.getMessage());
System.out.println();
}
}
}
});*/
javax.swing.Timer timer = new javax.swing.Timer(100, this);
timer.start();
//System.out.println("Label 3 "+label3.getText());
}
public void actionPerformed(ActionEvent e) {
String oldText = label.getText();
String newText = oldText.substring(1) + oldText.substring(0, 1);
label.setText( newText );
}
public static void main(String[] args) {
MovingText frame = new MovingText();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setSize(300,100);
frame.setVisible(true);
}
}

[code]

As you know, I takes the times for many weeks about that.So Pls help me.Thank in advance.:)
Sandar Khin is offline Offline   Reply With Quote
Old 04-11-09, 02:41 PM   #5 (permalink)
TAM
Senior Member
 
TAM's Avatar
 
Join Date: Oct 2006
Location: Mayiladurai (TN)
Posts: 243
Thanks: 8
Thanked 12 Times in 8 Posts
Rep Power: 7 TAM will become famous soon enough TAM will become famous soon enough
Re: A java swing program to marquee a text

Code:
/* Java Program which marquee the hyperlinked Labels across the Window
 * AUTHOR : TAMIZHVENDAN.S
 * FOR FURTHER DETAILS CONTACT tamizh88@gmail.com
 */

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);
    }

}


class MarqueePanel extends JPanel {
    private int xLoc, yLoc;
    private Dimension dimension;
    private JLabel googleLabel;
    private JLabel yahooLabel;
    private Timer timer;

    public MarqueePanel(final Timer timer) {
        this.timer = timer;
        setLayout(null);
        dimension = Toolkit.getDefaultToolkit().getScreenSize();
        xLoc = dimension.width;
        yLoc = 0;
        googleLabel = new JLabel("Google");
        googleLabel.setForeground(Color.BLUE);
        yahooLabel = new JLabel("Yahoo");
        yahooLabel.setForeground(Color.RED);
        googleLabel.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() > 0) {
                        try {
                         Runtime.getRuntime().exec("cmd.exe /c start http://www.google.com");
                        } catch (IOException ex) {
                            System.out.println(ex.getMessage());
                        }
                    }
                }

                public void mouseEntered(MouseEvent e) {
                    timer.stop();
                }

                public void mouseExited(MouseEvent e) {
                    timer.start();
                }
                
            });
        yahooLabel.addMouseListener(new MouseAdapter() {
                
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() > 0) {
                        try {
                         Runtime.getRuntime().exec("cmd.exe /c start http://www.yahoo.com");
                        } catch (IOException ex) {
                            System.out.println(ex.getMessage());
                        }
                    }
                }

                public void mouseEntered(MouseEvent e) {
                    timer.stop();
                }

                public void mouseExited(MouseEvent e) {
                    timer.start();
                }
            });
        add(googleLabel);
        add(yahooLabel);
        moveLabel();
    }

    public void moveLabel() {

        Rectangle r = new Rectangle();

        r.x = xLoc;
        r.y = yLoc;
        Dimension size = googleLabel.getPreferredSize();
        r.width = size.width;
        r.height = size.height;
        googleLabel.setBounds(r);

        // Append the Width of First Label to avoid overlapping
        r.x += r.width + 5;
        size = yahooLabel.getPreferredSize();
        r.width = size.width;
        r.height = size.height;
        yahooLabel.setBounds(r);
        
        xLoc -= 5;
        if (xLoc < 0 )
            xLoc = dimension.width;
    }

 

}

hope this would satisfy your requirements !!
TAM is offline Offline   Reply With Quote
Old 05-11-09, 11:13 AM   #6 (permalink)
Junior Member
 
Join Date: Oct 2009
Age: 22
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 Sandar Khin is on a distinguished road
Re: A java swing program to marquee a text

Hi Tam.
So tank to you.I distrub you many times.I'm so sorry for that.Now,I repair your program as looping style.At that time marquee texts are not appear.May I hope your help.
Code:
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("Marquee");
        setResizable(true);

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

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

}


class MarqueePanel extends JPanel {
    private int xLoc, yLoc;
    private Dimension dimension;
    private JLabel googleLabel;
    private JLabel yahooLabel;
    private Timer timer;
    private JLabel stringlbl;
    String stringlabel[][];

    public MarqueePanel(final Timer timer) {
        this.timer = timer;
        setLayout(null);
        dimension = Toolkit.getDefaultToolkit().getScreenSize();
        xLoc = dimension.width;
        yLoc = 0;
        stringlabel =new String[2][2];
        stringlabel[0][0] = "Google";
        stringlabel[0][1] = "http://www.google.com";
        stringlabel[1][0] = "Yahoo";
        stringlabel[1][1] = "http://www.yahoo.com";
        for(int i=0;i 0) {
                        try {
                         Runtime.getRuntime().exec("cmd.exe /c start "+link);
                        } catch (IOException ex) {
                            System.out.println(ex.getMessage());
                        }
                    }
                }

                public void mouseEntered(MouseEvent e) {
                    timer.stop();
                }

                public void mouseExited(MouseEvent e) {
                    timer.start();
                }
                
            });
              add(stringlbl);
        }
            moveLabel();
        /*googleLabel = new JLabel("Google");
        googleLabel.setForeground(Color.BLUE);
        yahooLabel = new JLabel("Yahoo");
        yahooLabel.setForeground(Color.RED);
        googleLabel.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() > 0) {
                        try {
                         Runtime.getRuntime().exec("cmd.exe /c start http://www.google.com");
                        } catch (IOException ex) {
                            System.out.println(ex.getMessage());
                        }
                    }
                }

                public void mouseEntered(MouseEvent e) {
                    timer.stop();
                }

                public void mouseExited(MouseEvent e) {
                    timer.start();
                }
                
            });
        yahooLabel.addMouseListener(new MouseAdapter() {
                
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() > 0) {
                        try {
                         Runtime.getRuntime().exec("cmd.exe /c start http://www.yahoo.com");
                        } catch (IOException ex) {
                            System.out.println(ex.getMessage());
                        }
                    }
                }

                public void mouseEntered(MouseEvent e) {
                    timer.stop();
                }

                public void mouseExited(MouseEvent e) {
                    timer.start();
                }
            });
        add(googleLabel);
        add(yahooLabel);
        moveLabel();*/
    }

    public void moveLabel() {

        Rectangle r = new Rectangle();
        r.x = xLoc;
        r.y = yLoc;
        Dimension size;// = googleLabel.getPreferredSize();
        for(int i=0;i Thank in advance.
Sandar Khin is offline Offline   Reply With Quote
Old 07-11-09, 12:19 PM   #7 (permalink)
Junior Member
 
Join Date: Oct 2009
Age: 22
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 Sandar Khin is on a distinguished road
Re: A java swing program to marquee a text

Hi Tam
Now, I can do that I want.So Thank a lot to you.
Sandar Khin is offline Offline   Reply With Quote
Old 09-11-09, 06:34 PM   #8 (permalink)
TAM
Senior Member
 
TAM's Avatar
 
Join Date: Oct 2006
Location: Mayiladurai (TN)
Posts: 243
Thanks: 8
Thanked 12 Times in 8 Posts
Rep Power: 7 TAM will become famous soon enough TAM will become famous soon enough
Re: A java swing program to marquee a text

Hi.. Sorry yar.. I didnt surf for the past few days.. Here is the code that do u need.. its works well...
Instead of creating a single label , i created a JLabel array and the problem is troubleshooted...


Code:
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 NewMarqueeFrame 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 NewMarqueeFrame() {

       Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setSize(d.width,d.height);
       setTitle(" New Marquee");
       setResizable(true);

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

   }

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

}
class MarqueePanel extends JPanel {
   private int xLoc, yLoc;
   private Dimension dimension;
   private JLabel googleLabel;
   private JLabel yahooLabel;
   private Timer timer;
   private JLabel stringlbl[];
   String stringlabel[][];

   public MarqueePanel(final Timer timer) {
       this.timer = timer;
       setLayout(null);
       dimension = Toolkit.getDefaultToolkit().getScreenSize();
       xLoc = dimension.width;
       yLoc = 0;
       stringlbl = new JLabel[3];
       stringlabel =new String[3][3];
       stringlabel[0][0] = "Google";
       stringlabel[0][1] = "http://www.google.com";
       stringlabel[1][0] = "Yahoo";
       stringlabel[1][1] = "http://www.yahoo.com";
       stringlabel[2][0] = "MSN";
       stringlabel[2][1] = "http://www.msn.com";

       for(int i=0;i 0) {
                       try {
                        Runtime.getRuntime().exec("cmd.exe /c start "+link);
                       } catch (IOException ex) {
                           System.out.println(ex.getMessage());
                       }
                   }
               }

               public void mouseEntered(MouseEvent e) {
                   timer.stop();
               }

               public void mouseExited(MouseEvent e) {
                   timer.start();
               }

           });
             add(stringlbl[i]);
       }
       moveLabel();
   }

   public void moveLabel() {

       Rectangle r = new Rectangle();
       r.x = xLoc;
       r.y = yLoc;
       // = googleLabel.getPreferredSize();
       for(int i=0;i 
TAM is offline Offline   Reply With Quote
Old 10-11-09, 04:23 PM   #9 (permalink)
Junior Member
 
Join Date: Oct 2009
Age: 22
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 Sandar Khin is on a distinguished road
Re: A java swing program to marquee a text

Hi TAM,
How are you?I need your help again.If possible,pls help me again.I don't know about Timer and Thread very well.

1.At my application,I want toadd marquee Panel at the below of my Frame as you know.
2.And when I click a Menuitem,marquee text will change.

I can do that cases with your help.But I have some problem until now.
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)

Pls help me about that cases.Here my Marquee.java program.It is the portion of my application.


Code:
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  Sandar Khin is offline Offline 
		
		
		
		
		 
	
Reply With Quote
Old 19-11-09, 10:06 AM   #10 (permalink)
Junior Member
 
Join Date: Oct 2009
Age: 24
Posts: 27
Thanks: 11
Thanked 0 Times in 0 Posts
Rep Power: 1 sonali.jog is on a distinguished road
Re: A java swing program to marquee a text

V. Good program. Works fine.
sonali.jog is offline Offline   Reply With Quote
Reply

Tags
java , marquee , program , swing , text


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sun Certification for Java alchemist JAVA Technologies 11 13-02-09 05:08 PM
JAVA WEB SITES yathish JAVA Technologies 2 20-11-08 12:11 AM
Practice Question in JAVA sowmya571 JAVA Technologies 8 15-02-08 08:09 PM
JAVA Downloads yathish JAVA Technologies 2 22-01-07 01:37 PM


All times are GMT +6.5. The time now is 05:15 PM.

More Interview Questions Here...

Content Relevant URLs by vBSEO 3.3.0