Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2007

Tip: Looking for answers? Try searching our database.

How do you consistently repaint a JComponent?

Thread view: 
phillip.s.powell@gmail.com - 23 Feb 2007 22:13 GMT
[code]
    /**
        * Handle {@link #urlLabelText} to change color if {@link
java.net.URL} displayed is not {@link #homeURL}
        */
       private void handleURLLabelText() {
           if (SimpleBrowser.this.getURL() != null &&
                   SimpleBrowser.this.getHomeURL() != null &&
                   !
SimpleBrowser.this.getURL().equals(SimpleBrowser.this.getHomeURL())) {
               SimpleBrowser.this.urlLabel.setForeground(Color.RED);
               if (!this.hasAddedURLLabelMouseAdapter) {
                   // THIS IS TO ENSURE ONLY ONE MouseAdapter
mouseAdapter IS ADDED TO JLabel SimpleBrowser.this.urlLabel
                   this.hasAddedURLLabelMouseAdapter = true;

SimpleBrowser.this.urlLabel.addMouseListener(mouseAdapter);
               }
               SimpleBrowser.this.urlLabel.setToolTipText("Click onto
\"" + urlLabelText +
                       "\" to set \"" + getURLPath() + "\" as your
default homepage");
           } else if (this.hasAddedURLLabelMouseAdapter) {
               // THIS IS TO ENSURE THE RE-ADDING OF MouseAdapter
mouseAdapter
               this.hasAddedURLLabelMouseAdapter = false;

SimpleBrowser.this.urlLabel.removeMouseListener(mouseAdapter);
               SimpleBrowser.this.urlLabel.setToolTipText(null);

SimpleBrowser.this.urlLabel.setForeground(Color.BLACK);
           }

SimpleBrowser.this.urlLabel.setFont(SimpleBrowserGlobals.FONT);
           SimpleBrowser.this.urlLabel.repaint();
       }
[/code]

I want to always be able to repaint the JLable
SimpleBrowser.this.urlLabel, however, on occasions while its
ToolTipText is always set with a value (or null), and while I can add
or remove its MouseListener, I can't seem to always change the color
back from red to black (though I can always change it from default
black to red).

I thought using repaint() would ensure that the changes to the JLabel
would take place, but unfortunately it does not, not even when I
invoke:

[code]
    /**
        * Handle {@link #urlLabelText} to change color if {@link
java.net.URL} displayed is not {@link #homeURL}
        */
       private void handleURLLabelText() {
           if (SimpleBrowser.this.getURL() != null &&
                   SimpleBrowser.this.getHomeURL() != null &&
                   !
SimpleBrowser.this.getURL().equals(SimpleBrowser.this.getHomeURL())) {
               SimpleBrowser.this.urlLabel.setForeground(Color.RED);
               if (!this.hasAddedURLLabelMouseAdapter) {
                   // THIS IS TO ENSURE ONLY ONE MouseAdapter
mouseAdapter IS ADDED TO JLabel SimpleBrowser.this.urlLabel
                   this.hasAddedURLLabelMouseAdapter = true;

SimpleBrowser.this.urlLabel.addMouseListener(mouseAdapter);
               }
               SimpleBrowser.this.urlLabel.setToolTipText("Click onto
\"" + urlLabelText +
                       "\" to set \"" + getURLPath() + "\" as your
default homepage");
           } else if (this.hasAddedURLLabelMouseAdapter) {
               // THIS IS TO ENSURE THE RE-ADDING OF MouseAdapter
mouseAdapter
               this.hasAddedURLLabelMouseAdapter = false;

SimpleBrowser.this.urlLabel.removeMouseListener(mouseAdapter);
               SimpleBrowser.this.urlLabel.setToolTipText(null);

SimpleBrowser.this.urlLabel.setForeground(Color.BLACK);
           }

SimpleBrowser.this.urlLabel.setFont(SimpleBrowserGlobals.FONT);
           SimpleBrowser.this.urlLabel.repaint();
           SimpleBrowser.this.topPanel.validate(); // CONTAINS
urlLabel
           validate(); // SimpleBrowser ULTIMATELY EXTENDS JFrame
       }
[/code]

Suggestions?
Thanx
Phil
phillip.s.powell@gmail.com - 23 Feb 2007 22:28 GMT
On Feb 23, 5:13 pm, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:
> [code]
>      /**
[quoted text clipped - 33 lines]
>         }
> [/code]

Never mind, I got it..

[code]
             repaint();
[/code]

Phil
Knute Johnson - 23 Feb 2007 22:39 GMT
> [code]
>      /**
[quoted text clipped - 89 lines]
> Thanx
> Phil

Are all the Swing component method calls being made on the EDT?  You
probably don't need the validate() or repaint() calls.  Your problem is
most likely somewhere else other than the code you are showing.

Signature

Knute Johnson
email s/nospam/knute/

phillip.s.powell@gmail.com - 26 Feb 2007 14:02 GMT
On Feb 23, 5:39 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> phillip.s.pow...@gmail.com wrote:
> > [code]
[quoted text clipped - 92 lines]
>
> Are all the Swing component method calls being made on the EDT?

I am not sure what you mean by "EDT", sorry.

You
> probably don't need the validate() or repaint() calls.  Your problem is
> most likely somewhere else other than the code you are showing.
[quoted text clipped - 3 lines]
> Knute Johnson
> email s/nospam/knute/
Andrew Thompson - 26 Feb 2007 14:35 GMT
On Feb 27, 1:02 am, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:

Please note that it is not necessary to quote
over 100 lines of text just to express..

> I am not sure what you mean by "EDT", sorry.

..please trim in future.

In any case, the best place to start
answering questions like this is to
unravel the acronym.  You might start
here..
<http://www.google.com/search?q=acronym+edt>
..but unfortunately, the letters EDT
are a very common acronym, so that
does not help much in this case.

So, I will fill it in.  EDT (in Java)
stands for the Event Dispatch Thread.

The next search is better though, start
with 'java tutorial', and..
<http://www.google.com/search?q=java+tutorial+Event+Dispatch+Thread>

HTH

Andrew T.
phillip.s.powell@gmail.com - 26 Feb 2007 15:34 GMT
> On Feb 27, 1:02 am, "phillip.s.pow...@gmail.com"
>
[quoted text clipped - 6 lines]
>
> HTH

Thanx.  Upon reviewing the Sun Tutorials I am no clearer than I was
before I learned what EDT stood for.  Sorry, way over my head, the
whole SwingWorker vs initial thread vs background thread vs GUI not
being thread safe vs.. not understanding anything I read whatsoever.

As bad as it's going to sound, I can only understand that repaint()
works and that's as far as I'm able to fathom.

Phil

> Andrew T.
phillip.s.powell@gmail.com - 26 Feb 2007 14:36 GMT
On Feb 23, 5:39 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> phillip.s.pow...@gmail.com wrote:
> > [code]
[quoted text clipped - 92 lines]
>
> Are all the Swing component method calls being made on the EDT?

I am not sure what you mean by "EDT", sorry.

You
> probably don't need the validate() or repaint() calls.  Your problem is
> most likely somewhere else other than the code you are showing.
[quoted text clipped - 3 lines]
> Knute Johnson
> email s/nospam/knute/
phillip.s.powell@gmail.com - 26 Feb 2007 14:54 GMT
On Feb 23, 5:39 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> phillip.s.pow...@gmail.com wrote:
> > [code]
[quoted text clipped - 92 lines]
>
> Are all the Swing component method calls being made on the EDT?

I am not sure what you mean by "EDT", sorry.

You
> probably don't need the validate() or repaint() calls.  Your problem is
> most likely somewhere else other than the code you are showing.
[quoted text clipped - 3 lines]
> Knute Johnson
> email s/nospam/knute/
phillip.s.powell@gmail.com - 26 Feb 2007 15:11 GMT
On Feb 23, 5:39 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> phillip.s.pow...@gmail.com wrote:
> > [code]
[quoted text clipped - 92 lines]
>
> Are all the Swing component method calls being made on the EDT?

I am not sure what you mean by "EDT", sorry.

You
> probably don't need the validate() or repaint() calls.  Your problem is
> most likely somewhere else other than the code you are showing.
[quoted text clipped - 3 lines]
> Knute Johnson
> email s/nospam/knute/
Ian Wilson - 26 Feb 2007 15:40 GMT
> I am not sure what you mean by "EDT", sorry.

This explains what EDT means ...
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.