king of (re)setting the tooltipText as app runs

T

tek monk

I need to reset a JLabel's Text and TooTip as time passes in the simplest
and least taxing way.
..
I know for that you would use JLabel's setText(String ...) and
setToolTipText(String ...) methods, but the thing is that I do know, based
on the apps logic, when to call setText, but I don't know when a user is
going to place the mouse over the label
..
I have been thinking about some kind of time listener attached to the
JLabel, but I can't think of an elegant way of doing this.
..
How would you approach the solution to such a problem?
..
Thanks
 
L

Lee Weiner

I need to reset a JLabel's Text and TooTip as time passes in the simplest
and least taxing way.
..
I know for that you would use JLabel's setText(String ...) and
setToolTipText(String ...) methods, but the thing is that I do know, based
on the apps logic, when to call setText, but I don't know when a user is
going to place the mouse over the label
..
I have been thinking about some kind of time listener attached to the
JLabel, but I can't think of an elegant way of doing this.

First you say that the ToolTip text changes as a function of time, then you
say you don't know when the user moves the mouse over the JLabel.? Exactly
what is the trigger to change the text in the Tooltop?

Lee Weiner
lee AT leeweiner DOT org
 
O

onetitfemme

... "I do know, based on the apps logic, when to call setText" ...
..
that means, for example, if a user is supposed to read 50 invoices and
she clicks "Next" to go through them, the user is actually cliking on
the "Next" (or "Previous") button, so you know which invoice she is at
and change the label accordingly via setText, but say if you want to
know how much time she will need to finish reading all invoices (based
on reasonable heuristics, like the speed at which she has been reading
them + all of those that still need to be read ...)
..
I think you will have to capture/reimplement some kind of "mouse over"
action responsible for then setting the ToolTipText ...
..
onetitfemme
 
L

Larry Barowski

tek monk said:
I need to reset a JLabel's Text and TooTip as time passes in the simplest
and least taxing way.
.
I know for that you would use JLabel's setText(String ...) and
setToolTipText(String ...) methods, but the thing is that I do know, based
on the apps logic, when to call setText, but I don't know when a user is
going to place the mouse over the label ...

If you mean that you want to compute the text only when the tooltip
is about to be shown, then override getToolTipText(MouseEvent) .
You will also need to do setToolTipText("dummy") to enable tooltips.
 
O

onetitfemme

Larry said:
If you mean that you want to compute the text only when the tooltip
is about to be shown, then override getToolTipText(MouseEvent) .
You will also need to do setToolTipText("dummy") to enable tooltips.
..
Exactly! and thank you for "understanding" me ;-)
..
For those of you trying the same thing here is some demo code that
updates the tooltip based on the current time:
..
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;

import java.util.*;
import java.text.*;

// __
public class ToolTipDemo00 extends JFrame{
JLabel JLbl;
SimpleDateFormat SDF;
// __
public ToolTipDemo00(){
super();
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
String aPttrn = "yyyyMMddHHmmss";
SDF = new SimpleDateFormat(aPttrn);
String aC = ":" + getFrmtdTm() + ":";
JLbl = new JLabel(aC){
// __ Implement JLabel's tool tips.
public String getToolTipText(MouseEvent MEvnt){
return(getFrmtdTm()); }
};
JLbl.setToolTipText(getFrmtdTm());

JLbl.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
JLbl.setSize(100, 25);
getContentPane().add(JLbl);
}
// __
public String getToolTipText() { return
ToolTipDemo00.this.getToolTipText(); }
// __
private String getFrmtdTm(){ return(SDF.format(new Date())); }
// __
public static void main(String[] args) {
ToolTipDemo00 TTDm = new ToolTipDemo00();
TTDm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){ System.exit(0); }
});
TTDm.setSize(200, 150);
TTDm.setVisible(true);
}
}
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top