> I want to display a JTree on the ToolTip of my component,use maybe
> click the jtree.What should I do?
<my suggestion>
Redesign your GUI to *not* require that.
</my suggestion>
--
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
Microsoft is introducing a "new" "feature" in Office 12. Their current
codename for the feature is "super tooltips", and it's essentially a richer
tooltip component that is capable of display more than just plain text. In
my opinion, it's not a bad idea, and I'd to see a similar feature in a
future release of Java (perhaps Java 7?)
Some details (including screenshots) on my blog:
http://nebupookins.net/entry.php?id=287
But to answer the OP's request, I don't think there's an easy way to do
that. You might have to implement your own tooltips.
- Oliver
>>leemingjun wrote:
>>
>>>I want to display a JTree on the ToolTip of my component,use maybe
>>>click the jtree.
....
> Microsoft is introducing a "new" "feature" in Office 12.
(groan)
> Some details (including screenshots) on my blog:
>
> http://nebupookins.net/entry.php?id=287
Those screenshots do not indicate to me that the user is intended
to *interact* with the elements of the tool tips ('click on a tree').
The tooltips seem to consist purely of information.
> But to answer the OP's request, I don't think there's an easy way to do
> that.
To present the information in a more comlicated form? Sure!
Try the tooltips in The Giffer[1] - they use HTML formatting to
present unordered lists, paragraphs and headings in tooltips.
[1] <http://www.physci.org/giffer/index.jsp>, try the first link in
the list <http://www.physci.org/giffer/giffer.jnlp>.
( and yes, even there, I have pushed tool-tips beyond what
should be presented in a 'quick pop-up'. )
You're right; however, these tooltips seem to at least be aware of the
state of the program. An example explicitly given is that the contents of
the tooltip will be "what does this feature do?" if the user is hovering
over an enabled button and "Why is this button disabled?" if the user is
hovering over a disabled button.
There is some discussion about whether to allow hyperlinks to the help
document to appear in these tooltips too (the idea being that there may be
many different topics that deal with the control being hovered over, so a
list of hyperlinks might be useful to allow the user to select the topic of
interest), which may eventually generalized to fully interactive tool tips.
There is already support for linking a tooltip with a help topic such
that if the user presses F1 while the tooltip is visible, a specific topic
will be shown (which may be different from the topic shown if the tooltip
were not visible), so the tooltip can apparently affect the state of the
program as well.
- Oliver
// MultilineToolTip.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class MultilineToolTip extends JToolTip
{
JTree tree;
private void createNodes(DefaultMutableTreeNode top)
{
DefaultMutableTreeNode category = null;
DefaultMutableTreeNode book = null;
category = new DefaultMutableTreeNode("Books for Java Programmers");
top.add(category);
// original Tutorial
book = new DefaultMutableTreeNode(("The Java Tutorial: A Short Course
on the Basics" + "tutorial.html"));
category.add(book);
// Tutorial Continued
book = new DefaultMutableTreeNode(("The Java Tutorial Continued: The
Rest of the JDK" + "tutorialcont.html"));
category.add(book);
// JFC Swing Tutorial
book = new DefaultMutableTreeNode(("The JFC Swing Tutorial: A Guide
to Constructing GUIs"
+ "swingtutorial.html"));
category.add(book);
// Bloch
book = new DefaultMutableTreeNode(("Effective Java Programming
Language Guide" + "bloch.html"));
category.add(book);
// Arnold/Gosling
book = new DefaultMutableTreeNode(("The Java Programming Language" +
"arnold.html"));
category.add(book);
// Chan
book = new DefaultMutableTreeNode(("The Java Developers Almanac" +
"chan.html"));
category.add(book);
category = new DefaultMutableTreeNode("Books for Java Implementers");
top.add(category);
// VM
book = new DefaultMutableTreeNode(("The Java Virtual Machine
Specification" + "vm.html"));
category.add(book);
// Language Spec
book = new DefaultMutableTreeNode(("The Java Language Specification"
+ "jls.html"));
category.add(book);
}
public MultilineToolTip()
{
this.setLayout(new BorderLayout());
DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java
Series");
createNodes(top);
tree = new JTree(top);
this.add(tree, BorderLayout.CENTER);
this.add(new JLabel("Press F2 to click on the JTree"),
BorderLayout.SOUTH);
}
/*
* public void setTipText(String tipText) { this.tipText = tipText; }
*/
public String getTipText()
{
return "";
}
public Dimension getPreferredSize()
{
return tree.getPreferredSize();
}
public JTree getTree()
{
return tree;
}
}
// Demo.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
public class Demo extends JPanel
{
private static KeyEventPostProcess1 keyEventPostProcess1;
private static JFrame mainFrame;
private class WindowAdapter1 extends WindowAdapter
{
public void windowClosed(WindowEvent e)
{
// TODO 自动生成方法存根
super.windowClosed(e);
}
}
private static final class KeyEventPostProcess1 implements
KeyEventPostProcessor
{
public boolean postProcessKeyEvent(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_F2)
{
Object obj = e.getSource();
if (obj instanceof JComponent)
{
System.out.println(((JComponent) obj));
JToolTip tip = ((JComponent) obj).createToolTip();
if (tip instanceof MultilineToolTip)
{
ToolTipManager.sharedInstance().setEnabled(false);
JTipDialog tipWindow = new JTipDialog(Demo.mainFrame,
((JComponent) obj));
tipWindow.setModal(true);
tipWindow.setVisible(true);
ToolTipManager.sharedInstance().setEnabled(true);
}
}
}
return false;
}
}
public static void main(String[] args)
{
keyEventPostProcess1 = new KeyEventPostProcess1();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(keyEventPostProcess1);
mainFrame = new JFrame();
mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
super.windowClosed(e);
}
});
mainFrame.getContentPane().add(new Demo());
mainFrame.pack();
mainFrame.setSize(300, 350);
mainFrame.setVisible(true);
}
public Demo()
{
this.setLayout(new FlowLayout());
JLabel l = new MyLabel();
l.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
Object obj = e.getSource();
if (obj instanceof JComponent)
{
((JComponent) obj).requestFocus();
}
}
});
l.setText("This is MyLabel");
l.setToolTipText("tool tip");
add(l);
l = new JLabel();
l.setText("This is JLabel");
l.setToolTipText("tool tip");
add(l);
JPanel p = new LFPanel();
add(p);
}
}
class LFPanel extends JPanel
{
/**
* An ActionListener that listens to the radio buttons.
*/
class RadioListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String lnfName = e.getActionCommand();
try
{
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(getParent());
// getParent().pack();
}
catch (Exception exc)
{
JRadioButton button = (JRadioButton) e.getSource();
button.setEnabled(false);
updateState();
System.err.println("Could not load LookAndFeel: " + lnfName);
}
}
}
static String metal = "Metal";
static String metalClassName =
"com.sun.java.swing.plaf.metal.MetalLookAndFeel";
static String motif = "Motif";
static String motifClassName =
"com.sun.java.swing.plaf.motif.MotifLookAndFeel";
static String windows = "Windows";
static String windowsClassName =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
JRadioButton metalButton, motifButton, windowsButton;
public LFPanel()
{
metalButton = new JRadioButton(metal);
metalButton.setMnemonic('o');
metalButton.setActionCommand(metalClassName);
motifButton = new JRadioButton(motif);
motifButton.setMnemonic('m');
motifButton.setActionCommand(motifClassName);
windowsButton = new JRadioButton(windows);
windowsButton.setMnemonic('w');
windowsButton.setActionCommand(windowsClassName);
// Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(metalButton);
group.add(motifButton);
group.add(windowsButton);
metalButton.setSelected(true);
// Register a listener for the radio buttons.
RadioListener myListener = new RadioListener();
metalButton.addActionListener(myListener);
motifButton.addActionListener(myListener);
windowsButton.addActionListener(myListener);
add(metalButton);
add(motifButton);
add(windowsButton);
}
public void updateState()
{
String lnfName = UIManager.getLookAndFeel().getClass().getName();
if (lnfName.indexOf(metal) >= 0)
{
metalButton.setSelected(true);
}
else if (lnfName.indexOf(windows) >= 0)
{
windowsButton.setSelected(true);
}
else if (lnfName.indexOf(motif) >= 0)
{
motifButton.setSelected(true);
}
else
{
System.err.println("SimpleExample if using an unknown L&F: " +
lnfName);
}
}
}
class MyLabel extends JLabel
{
public MyLabel()
{
super();
}
public JToolTip createToolTip()
{
return new MultilineToolTip();
}
}
// JTipDialog.java
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JToolTip;
public class JTipDialog extends JDialog
{
public JTipDialog(Frame owner, JComponent component) throws
HeadlessException
{
super(owner);
JToolTip tip = ((JComponent) component).createToolTip();
int x = ((JComponent) component).getLocationOnScreen().x +
((JComponent) component).getWidth() / 3;
int y = ((JComponent) component).getLocationOnScreen().y +
((JComponent) component).getHeight() + 10;
int width = (int) (tip).getPreferredSize().getWidth() + 10;
int height = (int) (tip).getPreferredSize().getHeight() + 10;
width = (width > 300) ? width : 300;
height = (height > 200) ? height : 200;
setBounds(x, y, width, height);
JScrollPane sp = new JScrollPane(((MultilineToolTip) tip).getTree());
getContentPane().setLayout(new BorderLayout());
getContentPane().add(sp, BorderLayout.CENTER);
}
}