import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TestLabelMove {
List<MovingLabel> labels;
private int count = 1;
private JLabel statusLabel;
private final int SEPARATION = 100;
private final int SCREEN_W = 1000;
int XLOC = SCREEN_W;
private boolean scrollable = true;
public TestLabelMove() {
// statusLabel = new JLabel("Status");
// statusLabel.setHorizontalAlignment(JLabel.CENTER);
JFrame frame = new JFrame("Test Labels");
// frame.add(statusLabel, BorderLayout.CENTER);
frame.add(new LabelPanel(), BorderLayout.SOUTH);
frame.setSize(1000, 75);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private class LabelPanel extends JPanel {
private static final int INC = 5;
public LabelPanel() {
labels = new LinkedList<MovingLabel>();
for (int i = 0; i < 8; i++) {
MovingLabel label = new MovingLabel(XLOC);
labels.add(label);
XLOC -= SEPARATION;
add(label);
}
Timer timer = new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (MovingLabel label : labels) {
if (isScrollable()) {
if (isWrap(label.getXLoc())) {
label.setXLoc(SCREEN_W);
label.setLocation(label.getXLoc(), 0);
} else {
label.setXLoc(label.getXLoc() - INC);
label.setLocation(label.getXLoc(), 0);
}
}
}
}
});
timer.start();
}
}
public boolean isWrap(int x) {
return x <= -40;
}
public boolean isScrollable() {
return scrollable;
}
private class MovingLabel extends JPanel {
int xLoc;
String phrase;
public MovingLabel(int xLoc) {
this.xLoc = xLoc;
phrase = "Panel " + count;
count++;
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// statusLabel.setText(phrase);
JFrame fr = new JFrame();
fr.setTitle(phrase);
fr.setSize(500, 200);
fr.setLocationRelativeTo(null);
fr.setVisible(true);
System.out.println("phrase - " + phrase);
JLabel lbl = new JLabel();
lbl.setText(phrase);
fr.add(lbl, BorderLayout.CENTER);
JButton bt = new JButton("Completed");
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
// revalidate();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
scrollable = false;
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
scrollable = true;
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Font font = new Font("Helvetica", Font.BOLD, 14);
FontMetrics fm = g.getFontMetrics(font);
int w = fm.stringWidth(phrase);
int h = fm.getAscent();
g.setFont(font);
g.drawString(phrase, getWidth() / 2 - w / 2, getHeight() / 2 + h
/ 2);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 20);
}
public void setXLoc(int xLoc) {
this.xLoc = xLoc;
}
public int getXLoc() {
return xLoc;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestLabelMove();
}
});
}
}
This code also taken from net to learn. It exactly matches my requirement.After removing label on mouse click, it should disappear from scrolling labels from Frame.