Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
What is the right way to flash a lable?
Yaroslav Chinskiy
Ranch Hand
Posts: 147
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I am new to SWING and looking for the best practices.
I need to write a gui where based on some events table cells start/stop flashing. In the attached code I get 70% CPU util if I add 1000 lables.
So what am i doing wrong?
/* * Created on Jul 27, 2004 */ package com.yarik.test.swing; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class SimpleFlasingLables { class TickerListenerLable extends JLabel implements ActionListener { private int id; Color offColor = Color.GREEN; boolean on = true; Color onColor = Color.RED; /** * */ public TickerListenerLable(String name, int id) { super(name); this.id = id; } /* * (non-Javadoc) * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { if (on) { setForeground(onColor); } else { setForeground(offColor); } on = !on; } } public static void main(String[] args) { SimpleFlasingLables frame = new SimpleFlasingLables(); } int count = 1000; int delay = 500; JButton flashAll = new JButton("FLASH ALL"); JButton flashEven = new JButton("FLASH EVEN"); JButton flashOdd = new JButton("FLASH ODD"); TickerListenerLable[] lables = new TickerListenerLable[count]; JButton stop = new JButton("STOP"); Timer timer; SimpleFlasingLables() { JFrame frame = new JFrame("Simple Frame Test."); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JPanel btnPanel = new JPanel(); btnPanel.add(flashAll); btnPanel.add(flashEven); btnPanel.add(flashOdd); btnPanel.add(stop); frame.getContentPane().add(panel, BorderLayout.CENTER); frame.getContentPane().add(btnPanel, BorderLayout.NORTH); flashAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < lables.length; i++) { if (timer == null) { timer = new Timer(delay, lables[i]); timer.start(); } else { timer.addActionListener(lables[i]); } } flashAll.setEnabled(false); flashEven.setEnabled(false); flashOdd.setEnabled(false); } }); flashEven.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < lables.length; i++) { if (i % 2 != 1) { if (timer == null) { timer = new Timer(delay, lables[i]); timer.start(); } else { timer.addActionListener(lables[i]); } } } flashAll.setEnabled(false); flashEven.setEnabled(false); flashOdd.setEnabled(false); } }); flashOdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < lables.length; i++) { if (i % 2 == 1) { if (timer == null) { timer = new Timer(delay, lables[i]); timer.start(); } else { timer.addActionListener(lables[i]); } } } flashAll.setEnabled(false); flashEven.setEnabled(false); flashOdd.setEnabled(false); } }); stop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (timer == null) { return; } for (int i = 0; i < lables.length; i++) { timer.removeActionListener(lables[i]); lables[i].setForeground(Color.BLACK); } flashAll.setEnabled(true); flashEven.setEnabled(true); flashOdd.setEnabled(true); } }); for (int i = 0; i < lables.length; i++) { lables[i] = new TickerListenerLable("Lable " + i, i); panel.add(lables[i]); } frame.pack(); frame.setVisible(true); } }
Thank you.
[ July 27, 2004: Message edited by: Yaroslav Chinskiy ]
Craig Wood
Ranch Hand
Posts: 1535
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This is a little different way of looking at things. I wonder how this does for your cpu usage.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; public class FlashingLabels { public static void main(String[]args) { LabelPanel labelPanel = new LabelPanel(); FlashAction action = labelPanel.action; JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(action.getUIPanel(), "North"); f.getContentPane().add(new JScrollPane(labelPanel)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class LabelPanel extends JPanel { LabelStore[] labels; FlashAction action; public LabelPanel() { int size = 10; labels = new LabelStore[size]; action = new FlashAction(this); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,0,5,0); gbc.weightx = 1.0; gbc.weighty = 1.0; for(int j = 0; j < labels.length; j++) { labels[j] = new LabelStore("label " + (j + 1)); labels[j].getLabel().addMouseListener(action); if(j % 2 == 0) gbc.gridwidth = gbc.RELATIVE; else gbc.gridwidth = gbc.REMAINDER; add(labels[j].getLabel(), gbc); } setBackground(Color.white); } public void flash() { for(int j = 0; j < labels.length; j++) labels[j].flash(); repaint(); } public void flashOdd() { boolean flashing; for(int j = 0; j < labels.length; j++) { if(j % 2 == 0) flashing = true; else flashing = false; labels[j].reset(); labels[j].setIsFlashing(flashing); } } public void reset() { for(int j = 0; j < labels.length; j++) labels[j].reset(); } public LabelStore[] getLabels() { return labels; } } class FlashAction extends MouseAdapter { LabelPanel labelPanel; Timer timer; public FlashAction(LabelPanel lp) { labelPanel = lp; timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { labelPanel.flash(); } }); } public void mousePressed(MouseEvent e) { JLabel label = (JLabel)e.getSource(); LabelStore[] labels = labelPanel.getLabels(); for(int j = 0; j < labels.length; j++) if(label == labels[j].getLabel()) labels[j].toggleFlashing(); } public JPanel getUIPanel() { final JCheckBox flash = new JCheckBox("flash"), odd = new JCheckBox("odd"); ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent e) { JCheckBox check = (JCheckBox)e.getSource(); if(check == flash) { if(timer.isRunning()) timer.stop(); else timer.start(); } if(check == odd) { if(odd.isSelected()) labelPanel.flashOdd(); else labelPanel.reset(); } } }; flash.addActionListener(l); odd.addActionListener(l); JPanel panel = new JPanel(); panel.add(flash); panel.add(odd); return panel; } } class LabelStore { JLabel label; Color on, off; boolean isFlashing; public LabelStore(String text) { label = new JLabel(text, JLabel.CENTER); label.setOpaque(true); label.setPreferredSize(new Dimension(125,20)); on = Color.red; off = Color.pink; isFlashing = false; label.setBackground(off); } public void flash() { Color color = label.getBackground(); if(isFlashing) { if(color == on) color = off; else color = on; } label.setBackground(color); } public JLabel getLabel() { return label; } public void setIsFlashing(boolean flashing) { isFlashing = flashing; } public void toggleFlashing() { isFlashing = !isFlashing; } public void reset() { isFlashing = false; label.setBackground(off); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
java JFreechart compress the line over the time period
Why isnt my thread rerstarted?
How to change Jtable Total Rows in runtime ?
Click a button to display an image. Trouble with add panel to a frame. Help !!
Applet timer
More...