Z
zerg
What's the best way to force a JComponent to repaint itself? I've got
three candidates:
revalidate()
repaint()
update()
three candidates:
revalidate()
repaint()
update()
What's the best way to force a JComponent to repaint itself? I've got
three candidates:
revalidate()
repaint()
update()
zerg said:What's the best way to force a JComponent to repaint itself? I've got
three candidates:
revalidate()
repaint()
update()
Knute said:Read Roedy's post and then tell us what you really want to do and what
you've been doing before that.
zerg said:I begin to suspect that there's some sort of unwritten code here that
forbids the giving of a straight answer.
Peter said:Forgive us if we suspect that there's some sort of unwritten code among
questioners that forbids the inspection of the relevant documentation
before asking a question.
Either you never bothered to even read the documentation, or there's
something more to your question than simply "what's the API-approved to
force a repaint?" Either way, your question isn't a well-formed one.
If I understand your follow-up correctly, you simply have some sort of
custom component that needs to be redrawn when internal state changes
that would be reflected visually. Assuming that's the correct
interpretation, then the documentation does in fact provide all of the
necessary information you need.
If there's something about the documentation that is confusing you,
there's no shame in being specific about that and asking for help with
it.
If you have read the documentation and need some help, then say so
and be specific about what kind of help you need.
If you simply ask a question that the documentation does in fact
answer, the most obvious explanation is that you haven't bothered
to look at the documentation at all.
zerg said:I begin to suspect that there's some sort of unwritten code here that
forbids the giving of a straight answer.
In case it somehow matters, I've implemented a custom JList subclass
with a custom cell renderer and some other custom behavior. In response
to certain new set methods, the behavior of the cell renderer changes,
and the visible portion of the list obviously needs to be repainted when
this occurs.
I suppose I could do something hackish like set the list's ListModel to
the existing ListModel ...
(Also, is there a way to get the NetBeans GUI editor to recognize new
component classes? Or, perhaps better, to simply bypass it and code the
UI manually, but still get the FrameView saving and restoring of window
state? And is the list definitely only going to call on the cell
renderer to paint the visible cells at any given time? I would prefer
this to be able to scale well with large lists, potentially 10,000 items
or more, with the ListModel storing say 10,000 Integers or other such
handles rather than larger objects that may be retrieved from disk or a
DB on demand.)
zerg said:Which maybe extends to Sun itself -- I found the API docs less than 100%
clear here, and even the Java Tutorial.
Provisionally, I'm using "repaint(getBounds())"; why there isn't a
no-args repaint-the-whole-thing method will probably remain an enduring
mystery long after my app has matured, had its heyday, and become
obsolete...
Peter said:I admit that the Java SDK docs aren't the best docs I've ever seen. But
in this case, you know the exact names of the methods in question and
can easily examine the documentation for each to learn what each is
for. I don't see any ambiguity that would lead to a misunderstanding here.
The only mystery is how you missed it:
http://java.sun.com/javase/6/docs/api/java/awt/Component.html#repaint()
Knute said:Well forgive me but if you want to repaint() something repaint() it.
So you are changing some data in your ListModel and it isn't being
reflected in your JList?
Knute said:If you had looked at the docs you would have seen that the no-arg
repaint() belongs to Component.
So tell us, are you updating your ListModel on the EDT?
zerg said:Suspect what you will, but I did indeed examine the JComponent API docs,
and there were three public methods associated with repainting -- not
counting that repaint() had two overloads.
I may have been somewhat biased by what I was specifically searching
for, namely a no-argument method for "repaint the whole component". It
looks like maybe that is simply lacking.
Anything that I say is well-formed, and I am not in the mood to be
publicly insulted by you or anyone else here. I came here asking for
advice in good faith and I don't appreciate being treated in such a manner.
Perhaps it does, but it is less than clear, for whatever reason.
Probably because the API docs are organized around a
what-this-thingamajig-does scheme, while the tutorial, though organized
around a how-to-do-X scheme, rather glosses over repainting of
components, other than to make it clear that to paint custom stuff
(rather than just at custom times) you override paintComponent.
That's more or less what I did, except that I simply asked directly for
the answer to the question that I had, instead of for clarification of a
particular bit of the docs.
Fine. From now on, when I have a highly specific question like "what is
the best way to force a JComponent to repaint itself?", instead of just
asking "what is the best way to force a JComponent to repaint itself?",
I will do as you advise here and say "I've read the documentation, but
still have a question: what is the best way to force a JComponent to
repaint itself?" -- satisfied?
Sometimes the most obvious explanation is wrong. Regardless, what the
explanation is is not relevant. The question I asked was about
JComponent painting, not about what the most likely explanation was for
why I asked the question. If you'd please just focus on the actual
content of a post asking a question, and refrain from speculating about
why the poster might have asked it or other unimportant things like that
that have no bearing on things, I think people might appreciate that.
People post questions here to get them answered, not to get sidetracked
into unrelated topics to satisfy your curiosity as to peoples' motives
in asking questions or for any similar such reason.
It's best to just take peoples' questions at face value, and give them a
simple, correct answer that they can apply immediately. I don't know
about you, but when I have a question, I'm generally interested in
getting an answer as quickly as possible, and one that can be applied
immediately. Therefore I've already read all the obviously-relevant
documentation, since if it has the answer in a clear and unambiguous
form that will get me moving much quicker than waiting for someone to
reply on a Usenet group. If that doesn't suffice, then I ask a question
here, and at that point I'm not going to be pleased if instead of the
one iteration of post-reply that it SHOULD take for me to get an answer
back, it ends up taking two or three because people want to satisfy
their idle curiosity as to why I'm asking the question, and of course if
they simply answered it straight away, I might go away and not answer
their questions! So they withhold my answer until they have satisfied
their curiosity -- wasting my time and everyone's bandwidth with matters
that are tangential, at best, to the purpose of this newsgroup.
Please don't do that any more. I find it annoying. If I ask "how to do
X", please just tell me, in simple terms, how to do X. If you think the
documentation should have answered it for me, well, apparently it
didn't. If you want, you can certainly point to the bit of the
documentation that you think unambiguously answers the question and ask
me why I didn't apparently one of find it relevant enough to look at or
find it clear and certain enough to use, AT THE SAME TIME as supplying
the actual answer. Then, since I'll have the answer I need to continue
with my work without delay, I am likely to be able to find some time
later to satisfy your curiosity a bit, and therefore I am likely to
answer, and to be much less annoyed and irritated when I do.
Thank you.
Knute said:You can get as mad as you want but people are really trying to help you.
And having been around here for a while, when someone asks "What method
do I use to repaint() a JComponent," we get sort of suspicious because
repainting is rarely the solution to anything.
And whether you believe it or not there IS a no-arg repaint() method. It
belongs to JComponent.
1) Are you updating your ListModel on the EDT?
2) Are you doing long running tasks on the EDT (in a listener for example)?
3) Are you adding or removing components to your JList?
Peter said:Yes, it does. In fact, it looks just like what you asked for.
Yes, you can. Even ignoring the fact that if you don't know what
rectangle describes the area in which you're drawing, you've got serious
problems
there is in fact a "no parameter" overload of the method (see
below).
revalidate()
Supports deferred automatic layout.
Calls invalidate and then [technical details]
This method will automatically be called on this component when a
property value changes such that size, location, or internal layout of
this component has been affected.
As the description says, this method is for things that affect layout.
Either the control itself has moved or changed size, or something in it
needs to go through layout again. This obviously has nothing to do with
simply forcing a repaint.
Would you rather have a method that has no arguments, or one that does
what you want?
And when you looked at the description of the paint() method, what did
you find?
Honestly, I don't know what your deal about the parameter is. The fact
is, a method that does exactly what you want exists, but you need to be
doing a better job looking at what the methods _do_
Once you find a method that has
_functionality_ that you want, then you can look for an overload with a
signature you prefer.
No, it's not apparent to me at all why it's not clear to you.
Of those three methods, two are obviously not what you want
and of one of those two, the documentation related to it even
_explicitly_ says to not call it directly and instead use the repaint()
method for what you're trying to do.
You keep saying you want a method without arguments. Did you bother to
look at the description of the method at the URL I provided?
really, you have no excuse
I don't know how much clearer I can be. I gave you the URL for the
exact method you're looking for.
Peter said:Because Swing is built on top of AWT.
JComponent inherits Component. You can't understand JComponent without
understanding Component.
"Fully intend to" is silly. There's no excuse for not doing it that way
from the outset.
It's _harder_ to write the code wrong and then fix it
than it is to just do it right in the first place.
Peter said:I count FIVE overloads of repaint().
Java is object-oriented programming. You have to look at
Well, unfortunately for you, you're not in a position to dictate
People get tired of responding to questions that are easily answered
simply by looking at the documentation.
When you make your question look like one of those, you're not going
to get the answer you want
Or, in other words, what I said you should do is MUCH more than what you
actually did.
There's nothing at all in your original post to suggest
that you had bothered to look at anything in the documentation.
It's not a question of whether I'm satisfied.
It's a question of
whether you have any interest in asking questions in a way that is
likely to get them answered.
As for your proposed alternative question format, no...that's not going
to get your question answered with any more enthusiasm.
If you have a question about what you've read in the documentation,
you need to be specific about that.
Simply saying "I've read the documentation and I'd
like you to tell me the answer to this question that was answered by the
documentation" doesn't encourage people to be motivated to help you.
True, and irrelevant.
The problem of people asking RTFM questions is far too common
for you to expect anyone to give you the benefit of the doubt.
This isn't about satisfying curiosity.
It's about trying to help you understand why people aren't going
to answer RTFM questions.
It's best to just take peoples' questions at face value, and give them
a simple, correct answer that they can apply immediately. I don't know
about you, but when I have a question, I'm generally interested in
getting an answer as quickly as possible, and one that can be applied
immediately. [...]
The very fastest way for me to get the answer to any question would be
to just ask the expert.
But, that can be a waste of the expert's time,
and as well I will learn more about the subject if I do some research
myself.
Personally, I don't like wasting other people's time.
But the fact is, other people don't like YOU wasting their time.
When it looks like that's what you're doing,
they tend to ignore you rather than answering the question you want
answered.
Again, I've no interest here in making you feel bad about your choices.
I'm simply trying to offer advice that will help you understand better
techniques for getting your questions answered.
Peter said:Had you actually looked at all five overloads for repaint(), you would
have found the one you wanted.
Well, I hope you've learned from that mistake.
The next time you are reading the documentation for a method and it says
that it basically just calls some other method, now you know that you
need to go read the documentation for that other method.
Otherwise, you still don't know what the first method actually does.
You will not tolerate?
Do you have any choice in the matter?
I'm reading the same documentation you are.
It's just that I took the time to actually follow the links
From the description of JComponent.paint()
Invoked by Swing to draw components. Applications should not
invoke paint directly
If you'd read that paragraph, didn't understand, and had posted a
question asking for clarification, that would have been one thing. But
you obviously never got as far as even reading that paragraph.
I'm sorry if you feel insulted. It's not my intent.
So far, you've failed miserably
You've yet to show that I've made any false assumptions.
It turns out that you did in fact not read all of the documentation
that you should have
in order to research your question, and that the answer to your
question is in fact stated clearly in the documenation.
I pointed you to the AWT method BECAUSE IT IS THE ANSWER TO YOUR QUESTION.
Because, as I already pointed out, Java is an OOP language
you cannot understand a given class without also understanding its base
classes.
and you will, if you continue your Java education
Forgive me if I find it relevant that I _answered_ your question
and yet you refused to bother looking at the answer
because you made your own premature, uninformed and ill-advised
judgment that my answer wasn't what you were looking for.
When you've already been given the answer, why would anyone do anything
more than keep pointing out that you've been given the answer?
Not that your characterization of my post is accurate
but, at worst, whatever I write is only a waste of your time if you
read it. No one's forcing you to.
FALSE!
Until you understand your mistake, you will keep making it.
You think I'm sitting here trying to insult you when in fact I'm
trying to point out the mistake so that you can LEARN from it.
Define "right next to".
There certainly are links to the Component
repaint() overloads from the JComponent documentation page. I'd call
that "right next to".
No one's saying that the docs are perfect.
But in this particular case, they did not fail to provide the
information you wanted.
You simply failed to look.
The Java doc format is consistent.
Every class has a description, then a summary of all its members,
followed by simple lists of each member of each base class, and
finally the elaborated descriptions of each member.
All you had to do was scroll down to the base classes for
JComponent, looking at the members listed there, and you would have
found the other repaint() methods.
If you are inexperienced and did not understand that, that's not a
problem at all as long as you're willing to own up to that. But when
you repeatedly assert that you've made no mistake, that the method
you're looking for doesn't exist, even when someone has specifically
pointed you to it, then we come to...
I have announced nothing of the sort.
What's the best way to force a JComponent to repaint itself? I've got
three candidates:
revalidate()
repaint()
update()
Peter said:I never suggested you intend to "thoroughly understand" the class
But you do _need_ to at least understand the parts you're using.
My statement stands valid as is.
I suppose that depends on your definition of "school".
But if you think
this newsgroup is a place for you to just ask whatever questions pop
into your head, without any expectation that you should have to
perform due diligence to find the answer yourself first
you are in for a huge disappointment.
Here's the deal: people answer your questions at their own pleasure.
We're not here to serve your whims, nor to be ordered about by you.
We do as we please, and your ability to get a question answered will
depend heavily on your willingness to adhere to community standards
and expectations.
You're simply mistaken about that.
When the opportunity to help teach a person to fish comes up, many
of us will choose to do that, rather than to just hand out fish.
This isn't a game
Meaning, if we can somehow help you help yourself, then that's more
efficient in the long run and is a worthwhile effort.
More importantly though, I will point out once again: you do not have
the privilege of dictating how your question is answered.
The best you can do is simply ignore answers you don't like.
Throwing a tantrum
This, coming from the person who refused to look at the pointer that was
in fact provided to him.
Your suspicion is unfounded.
If I misunderstood you, it's only because
most programmers aren't in the habit of describing code that's been
written as not actually doing something until it's been executed in the
target environment.
But by your way of speaking, that code doesn't do anything! It hasn't
been run yet, and I only "fully intend" for it to iterate the variable
and execute the empty block with the values of 0 through 4.
Again, I will do as I please. Having you tell me NOT to do things just
makes me want to them all the more.
Peter said:What's your definition of "clueless"? Because my definition, it
includes the kinds of people that _quote_ the answer they are looking
for, while at the same time claiming that what they are looking for does
not exist.
Maybe your definition is different.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.