Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1775 > unrolled thread

Can a jPanel's background

Started by"JT" <jt@THRWHITE.remove-dii-this>
First post2011-04-27 15:35 +0000
Last post2011-04-27 15:35 +0000
Articles 16 — 5 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Can a jPanel's background "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
    Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
    Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
      Re: Can a jPanel's backgr "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
        Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
          Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
            Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
              Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
              Re: Can a jPanel's backgr "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
                Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
                  Re: Can a jPanel's backgr "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
                  Re: Can a jPanel's backgr "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
              Re: Can a jPanel's backgr "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
            Re: Can a jPanel's backgr "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
          Re: Can a jPanel's backgr "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
            Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000

#1775 — Can a jPanel's background

From"JT" <jt@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectCan a jPanel's background
Message-ID<466b5ee6$0$4332$9a566e8b@news.aliant.net>
  To: comp.lang.java.gui
or is there an attribute/property of a jPanel that will give it the 
ability to blink... ie.  flash off and on every 5 seconds for 2 minutes?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1776 — Re: Can a jPanel's backgr

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<7378489530db5@uwe>
In reply to#1775
  To: comp.lang.java.gui
JT wrote:
>or is there an attribute/property of a jPanel that will give it the 
>ability to blink... ie.  flash off and on every 5 seconds for 2 minutes?

No.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1777 — Re: Can a jPanel's backgr

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<7379e53847535@uwe>
In reply to#1775
  To: comp.lang.java.gui
JT wrote:

Sub: Can a jPanel's background be made to blink off and on

1) Is that a question?
2) Are you referring to a JPanel, or some class that is 
not in the J2SE?

In any case, it is possible to *extend* JPanel (or jPanel, 
I am guessing) to provide a method that will do that.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1778 — Re: Can a jPanel's backgr

From"JT" <jt@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<466bb25d$0$4336$9a566e8b@news.aliant.net>
In reply to#1777
  To: comp.lang.java.gui
Andrew Thompson wrote:
> In any case, it is possible to *extend* JPanel (or jPanel, 
> I am guessing) to provide a method that will do that.

Sometimes the fonts on my machine make jPanel and JPanel look the same, 
especially if it's late at night.  But, to answer your question, I am 
looking for a way to make my "flashing green" light flash.  I looked in 
the API but could not find anything which would do what I wanted, so I 
thought someone might have done something similar in the past and would 
be willing to share their ideas.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1779 — Re: Can a jPanel's backgr

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<737b62c1b91a9@uwe>
In reply to#1778
  To: comp.lang.java.gui
JT wrote:
>> In any case, it is possible to *extend* JPanel (or jPanel, 
>> I am guessing) to provide a method that will do that.
>
>Sometimes the fonts on my machine make jPanel and JPanel look the same, 
>especially if it's late at night.  

Please either get a better font, or better excuses..

>...But, to answer your question, I am 
>looking for a way to make my "flashing green" light flash.  I looked in 
>the API but could not find anything which would do what I wanted, 

I find that completely unsurprising.  I cannot imagine
it is often that a GUI requires *flashing* panels.  
(muses) Perhaps slightly more common than a 
requirement for panels with polka dots, but not 
much so.

>...so I 
>thought someone might have done something similar in the past and would 
>be willing to share their ideas.

I haven't, but a slight tweak of my earlier code should
point *a* way to go about it.  Note that this is still using
AWT (just because I'm feeling perverse) and uses the 
centred circle (rather than the background) to change 
color.  Further, no logic is performed to ensure that only
one signal is flashing at any instant.  

Adapt as needed ('batteries not included').

<sscce>
import java.awt.*;
import java.awt.event.*;

class TrafficLight {

  public static void main(String[] args) {
    Frame f = new Frame("Lights");
    Panel lightboard = new Panel( new GridLayout(0,1) );

    lightboard.add( new TrafficSignal(Color.green) );
    lightboard.add( new TrafficSignal(Color.yellow) );
    lightboard.add( new TrafficSignal(Color.red) );

    f.add( lightboard );
    f.pack();
    f.addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    } );
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

class TrafficSignal extends Panel implements Runnable {

  Color on;
  int radius = 55;
  int border = 10;
  boolean active;

  boolean flashing = false;
  long startFlash = 0;
  /** It should flash for 10 seconds. */
  long lengthFlash = 10000;


  TrafficSignal(Color color) {
    on = color;
    active = true;
    this.addMouseListener( new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
        setFlashing(true);
      }
    } );
    Thread t = new Thread(this);
    t.start();
  }

  public Dimension getPreferredSize() {
    int size = (radius+border)*2;
    return new Dimension( size, size );
  }

  public void run() {
    while (true) {
      if ( flashing ) {
        active = !active;
        repaint();
        if ( System.currentTimeMillis()-startFlash>lengthFlash ) {
          flashing = false;
        }
      }
      try {
        Thread.sleep(400);
      } catch (InterruptedException ie) {
        // wake and continue
      }
    }
  }

  public void setFlashing(boolean flash) {
    flashing = flash;
    startFlash = System.currentTimeMillis();
  }

  public void paint(Graphics g) {
    g.setColor( Color.black );
    g.fillRect(0,0,getWidth(),getHeight());

    if (active) {
      g.setColor( on );
    } else {
      g.setColor( on.darker().darker().darker() );
    }
    g.fillOval( border,border,2*radius,2*radius );
  }
}
</sscce>

HTH

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1781 — Re: Can a jPanel's backgr

From"Philipp" <philipp@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<1181468093_2183@sicinfo3.epfl.ch>
In reply to#1779
  To: comp.lang.java.gui
Andrew Thompson a |-crit :
> JT wrote:
>>> In any case, it is possible to *extend* JPanel (or jPanel, 
>>> I am guessing) to provide a method that will do that.
>> Sometimes the fonts on my machine make jPanel and JPanel look the same, 
>> especially if it's late at night.  
> 
> Please either get a better font, or better excuses..
> 
>> ...But, to answer your question, I am 
>> looking for a way to make my "flashing green" light flash.  I looked in 
>> the API but could not find anything which would do what I wanted, 
> 
> I find that completely unsurprising.  I cannot imagine
> it is often that a GUI requires *flashing* panels.  
> (muses) Perhaps slightly more common than a 
> requirement for panels with polka dots, but not 
> much so.
> 
>> ...so I 
>> thought someone might have done something similar in the past and would 
>> be willing to share their ideas.
> 
> I haven't, but a slight tweak of my earlier code should
> point *a* way to go about it.  Note that this is still using
> AWT (just because I'm feeling perverse) and uses the 
> centred circle (rather than the background) to change 
> color.  Further, no logic is performed to ensure that only
> one signal is flashing at any instant.  
> 
> Adapt as needed ('batteries not included').
> 
> <sscce>
> import java.awt.*;
> import java.awt.event.*;
> 
> class TrafficLight {
> 
>   public static void main(String[] args) {
>     Frame f = new Frame("Lights");
>     Panel lightboard = new Panel( new GridLayout(0,1) );
> 
>     lightboard.add( new TrafficSignal(Color.green) );
>     lightboard.add( new TrafficSignal(Color.yellow) );
>     lightboard.add( new TrafficSignal(Color.red) );
> 
>     f.add( lightboard );
>     f.pack();
>     f.addWindowListener( new WindowAdapter() {
>       public void windowClosing(WindowEvent we) {
>         System.exit(0);
>       }
>     } );
>     f.setLocationRelativeTo(null);
>     f.setVisible(true);
>   }
> }
> 
> class TrafficSignal extends Panel implements Runnable {
> 
>   Color on;
>   int radius = 55;
>   int border = 10;
>   boolean active;
> 
>   boolean flashing = false;
>   long startFlash = 0;
>   /** It should flash for 10 seconds. */
>   long lengthFlash = 10000;
> 
> 
>   TrafficSignal(Color color) {
>     on = color;
>     active = true;
>     this.addMouseListener( new MouseAdapter() {
>       public void mouseClicked(MouseEvent me) {
>         setFlashing(true);
>       }
>     } );
>     Thread t = new Thread(this);
>     t.start();
>   }
> 
>   public Dimension getPreferredSize() {
>     int size = (radius+border)*2;
>     return new Dimension( size, size );
>   }
> 
>   public void run() {
>     while (true) {
>       if ( flashing ) {
>         active = !active;
>         repaint();
>         if ( System.currentTimeMillis()-startFlash>lengthFlash ) {
>           flashing = false;
>         }
>       }
>       try {
>         Thread.sleep(400);
>       } catch (InterruptedException ie) {
>         // wake and continue
>       }
>     }
>   }
> 
>   public void setFlashing(boolean flash) {
>     flashing = flash;
>     startFlash = System.currentTimeMillis();
>   }
> 
>   public void paint(Graphics g) {
>     g.setColor( Color.black );
>     g.fillRect(0,0,getWidth(),getHeight());
> 
>     if (active) {
>       g.setColor( on );
>     } else {
>       g.setColor( on.darker().darker().darker() );
>     }
>     g.fillOval( border,border,2*radius,2*radius );
>   }
> }
> </sscce>

Shouldn't the GUI be updated from the EDT thread? How would this be done?

If I understand your SSCCE correctly, the frame is shown from the "main" 
thread. And the blinking is updated from your "TrafficLight" thread.

Thanks for your insight
Phil

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1782 — Re: Can a jPanel's backgr

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<737c4e55bac1d@uwe>
In reply to#1781
  To: comp.lang.java.gui
Philipp wrote:
>Andrew Thompson a |-crit :
>>>> In any case, it is possible to *extend* JPanel (or jPanel, 
>>>> I am guessing) to provide a method that will do that.
..

Please refrain from 'full-quoting'.  Selective trimming 
of earlier posts does wonders.  Note that my current
web interface to usenet automatically trims most text
before I get to reply.

>Shouldn't the GUI be updated from the EDT thread? How would this be done?

Yes and no.

The 'rule' is that UI updates outside the EDT should
be updated using invokeLater, something like..

<snippet>
  public void run() {
    while (true) {
      if ( flashing ) {
        active = !active;
        Runnable r = new Runnable(){
          public void run(){
            repaint();
          }
        };
        EventQueue.invokeLater(r);

        if ( System.currentTimeMillis()-startFlash>lengthFlash ) {
          flashing = false;
        }
      }
      try {
        Thread.sleep(400);
      } catch (InterruptedException ie) {
        // wake and continue
      }
    }
  }
</snippet>

I say 'rule' because I have never observed any GUI
problems to be caused by ignoring it.

(shrugs) YMMV.

[ Waits for the invokeLater devotees to jump in with
lots of long words and remonstrations - with no code 
that can back up the claims. ]

-- 
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1783 — Re: Can a jPanel's backgr

From"Philipp" <philipp@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<1181473590_2190@sicinfo3.epfl.ch>
In reply to#1782
  To: comp.lang.java.gui
Andrew Thompson a |-crit :
> Philipp wrote:
>> Andrew Thompson a |-crit :
>>>>> In any case, it is possible to *extend* JPanel (or jPanel, 
>>>>> I am guessing) to provide a method that will do that.
> ..
>> Shouldn't the GUI be updated from the EDT thread? How would this be done?
> 
> Yes and no.

 > Runnable r = new Runnable(){
 >          public void run(){
 >            repaint();
 >          }
 >        };
 >        EventQueue.invokeLater(r);

If the flashing frequency is very high, does the invokeLater guarantee a 
more responsive GUI (ie will some repaints be discarded if there are 10 
waiting in the queue?).

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1787 — Re: Can a jPanel's backgr

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<GdWai.111049$NU1.17897@newsfe13.lga>
In reply to#1782
  To: comp.lang.java.gui
Andrew Thompson wrote:
>> Shouldn't the GUI be updated from the EDT thread? How would this be done?
> 
> Yes and no.
> 
> The 'rule' is that UI updates outside the EDT should
> be updated using invokeLater, something like..
> 
> <snippet>
>   public void run() {
>     while (true) {
>       if ( flashing ) {
>         active = !active;
>         Runnable r = new Runnable(){
>           public void run(){
>             repaint();
>           }
>         };
>         EventQueue.invokeLater(r);
> 
>         if ( System.currentTimeMillis()-startFlash>lengthFlash ) {
>           flashing = false;
>         }
>       }
>       try {
>         Thread.sleep(400);
>       } catch (InterruptedException ie) {
>         // wake and continue
>       }
>     }
>   }
> </snippet>
> 
> I say 'rule' because I have never observed any GUI
> problems to be caused by ignoring it.
> 
> (shrugs) YMMV.
> 
> [ Waits for the invokeLater devotees to jump in with
> lots of long words and remonstrations - with no code 
> that can back up the claims. ]
> 

The repaint() method need not be called from the EDT as it is only a 
scheduler.  The paint will actually get done on the EDT even though you 
call repaint() from some other thread.

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1788 — Re: Can a jPanel's backgr

From"Philipp" <philipp@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<1181496029_2224@sicinfo3.epfl.ch>
In reply to#1787
  To: comp.lang.java.gui
Knute Johnson a |-crit :
> The repaint() method need not be called from the EDT as it is only a 
> scheduler.  The paint will actually get done on the EDT even though you 
> call repaint() from some other thread.

Could you recommend some free tutorial to read about how to build swing 
GUI applications with threads?


I want to program a feature where the user moves a JSlider which after 
some calculations shows a result in the GUI depending on the slider's 
position. It works now fully in the EDT but lags a lot.

This is because the slider creates continously events and each event 
must be calculated and shown.

What would be the optimal architecture for such a construct. How can I 
drop the processing of events if the GUI lags more than a certain time 
behind the slider position.

Phil

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1791 — Re: Can a jPanel's backgr

From"JT" <jt@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<466c4a13$0$4313$9a566e8b@news.aliant.net>
In reply to#1788
  To: comp.lang.java.gui
Philipp wrote:
> Knute Johnson a |-crit :
>> The repaint() method need not be called from the EDT as it is only a 
>> scheduler.  The paint will actually get done on the EDT even though 
>> you call repaint() from some other thread.
> 
> Could you recommend some free tutorial to read about how to build swing 
> GUI applications with threads?
> 
> 
> I want to program a feature where the user moves a JSlider which after 
> some calculations shows a result in the GUI depending on the slider's 
> position. It works now fully in the EDT but lags a lot.
> 
> This is because the slider creates continously events and each event 
> must be calculated and shown.
> 
> What would be the optimal architecture for such a construct. How can I 
> drop the processing of events if the GUI lags more than a certain time 
> behind the slider position.
> 
> Phil
Hey you.. start your own thread :-) and quit piggy-backing on mine.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1792 — Re: Can a jPanel's backgr

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<EW0bi.542498$2Q1.315370@newsfe16.lga>
In reply to#1788
  To: comp.lang.java.gui
Philipp wrote:
> Knute Johnson a |-crit :
>> The repaint() method need not be called from the EDT as it is only a 
>> scheduler.  The paint will actually get done on the EDT even though 
>> you call repaint() from some other thread.
> 
> Could you recommend some free tutorial to read about how to build swing 
> GUI applications with threads?

Look at the Java Tutorial on Sun's web site.  That is the best source 
for how to write Swing code.  Just because you can call repaint() 
outside the EDT doesn't mean that you can update a Swing GUI outside of 
the EDT.

> I want to program a feature where the user moves a JSlider which after 
> some calculations shows a result in the GUI depending on the slider's 
> position. It works now fully in the EDT but lags a lot.
> 
> This is because the slider creates continously events and each event 
> must be calculated and shown.
> 
> What would be the optimal architecture for such a construct. How can I 
> drop the processing of events if the GUI lags more than a certain time 
> behind the slider position.
> 
> Phil

You need to take a look at JSlider.getValueIsAdjusting().  With this 
method you can test in your ChangeListener whether the slider is moving. 
  If it is, don't bother to update your GUI until it stops.  Or make 
occaisional updates while it is moving.


If you want to continue this discussion, start a new thread.

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1789 — Re: Can a jPanel's backgr

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<466c3d5b$0$8741$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1782
  To: comp.lang.java.gui
Andrew Thompson wrote:
> Philipp wrote:
> 
>> Shouldn't the GUI be updated from the EDT thread? How would this be done?

> I say 'rule' because I have never observed any GUI
> problems to be caused by ignoring it.
> 
> (shrugs) YMMV.
> 
> [ Waits for the invokeLater devotees to jump in with
> lots of long words and remonstrations - with no code 
> that can back up the claims. ]

Somethings to back up my claims:

  o Using early access Java 1.5, jEdit deadlocked due to updating Swing 
components off the EDT.
  o Using early access Java 1.6, jEdit deadlocked due to updating Swing 
components off the EDT.
  o FindBugs gui2 often deadlocked on single threaded hardware, although 
this was trying to do some stuff on the EDT and screwing up. For a 
program designed to find bad code, FindBugs is very badly written.

Writing code that shows threading issues is highly unrewarding (top tip: 
develop your demonstration with multi-threaded hardware, then backport 
to single-threaded).


It is probably true that the vast majority of threading bugs don't 
actually occur. Stuff that occurs in constructors often doesn't have the 
old happens-before action going on, but never ever fails. I'd put money 
on there being more threading bugs in the Sun Java library than it has 
classes.

However, it's much easier all round if you strive to write code that 
obviously has no errors (rather than code that has no obvious errors). 
It'd make my job far less dull if people did that.

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1790 — Re: Can a jPanel's backgr

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<466c425c$0$8739$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1781
  To: comp.lang.java.gui
Philipp wrote:
> Andrew Thompson a |-crit :

>> class TrafficSignal extends Panel implements Runnable {

>>   boolean active;

>>   public void run() {
>>     while (true) {
>>       if ( flashing ) {
>>         active = !active;
>>         repaint();

> Shouldn't the GUI be updated from the EDT thread? How would this be done?

The code is actually using AWT without Swing (for some reason). AWT is 
supposedly thread-safe (it isn't, but it attempts to be).

If this were a Swing application it would still conform to the rules. 
repaint is allowed to be called from non-EDT threads.
Even though active is not protected by being volatile or having locked 
access, it should pick up a happens-before from the event dispatch.

However, I could make lots of criticisms of this code:

  o It uses AWT instead of Swing...
  o It appears to needlessly subclass Panel, where Canvas or JComponent 
would be more appropriate.
  o There is no reason for the same class to implement Runnable as 
extends Panel. <controversial>I think Java would be better without 
interfaces.</controversial>
  o The original problem didn't need Panel to be subclasses at all.
  o The thread goes on until the program exits.
  o Using javax.swing.Timer would be more convenient (and run actions on 
the EDT). o Missing private and final keywords.

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1784 — Re: Can a jPanel's backgr

From"JT" <jt@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<466bdce5$0$4307$9a566e8b@news.aliant.net>
In reply to#1779
  To: comp.lang.java.gui
Andrew Thompson wrote:
> 
>   public void setFlashing(boolean flash) {
>     flashing = flash;
>     startFlash = System.currentTimeMillis();
>   }

So perhaps I can drop a centered circle on to my JPanel and use your 
code to make it blink?  Thanks for your insight.

BTW - the whole JPanel, jPanel thing... I wasn't paying attention to 
what I was typing... hence the mistake.

BTW2 - I am using Swing for my app, so if I use your idea, I'll be 
mixing Swing and AWT.  Is this true?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1785 — Re: Can a jPanel's backgr

From"Philipp" <philipp@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Can a jPanel's backgr
Message-ID<1181475491_2192@sicinfo3.epfl.ch>
In reply to#1784
  To: comp.lang.java.gui
JT a |-crit :
> Andrew Thompson wrote:
>>
>>   public void setFlashing(boolean flash) {
>>     flashing = flash;
>>     startFlash = System.currentTimeMillis();
>>   }
> 
> So perhaps I can drop a centered circle on to my JPanel and use your 
> code to make it blink? 

Probably you'd better use setBackground(Color) on the JPanel and repaint 
it as in Andrews code.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web