Search code examples
javaswingtooltipjtextfield

How to add a custom tooltip to a JtextField?


sorry for my bad english, I am currently developing an application for a company, I would like to be able to give indications on the information to enter in JtextFields, but the problem is that this indication is sometimes long, so I found a class with a tooltip custom which allows to go to the line in the text of the tooltip but I do not find how to add it to my textfield. I try this code:

JMultiLineToolTip mltt = new JMultiLineToolTip();
    mltt.setTipText("Write an extention or anything you want, if the string apeare in file's name,\n the file will be added in result ");
    textField = new JTextField();
    textField.setBounds(134, 104, 210, 20);
    textField.setColumns(10);
    textField.add(mltt);
    //textField.setToolTipText("Write an extention or anything you want, if the string apeare in file's name,\n the file will be added in result ");
    request.add(textField);

here the code of JMultiLineToolTip

public class JMultiLineToolTip extends JToolTip {

private static final long serialVersionUID = 7813662474312183098L;

public JMultiLineToolTip() {
    updateUI();
}

public void updateUI() {
    setUI(MultiLineToolTipUI.createUI(this));
}

public void setColumns(int columns) {
    this.columns = columns;
    this.fixedwidth = 0;
}

public int getColumns() {
    return columns;
}

public void setFixedWidth(int width) {
    this.fixedwidth = width;
    this.columns = 0;
}

public int getFixedWidth() {
    return fixedwidth;
}

protected int columns = 0;
protected int fixedwidth = 0;

}

class MultiLineToolTipUI extends BasicToolTipUI {

static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI();
Font smallFont;
static JToolTip tip;
protected CellRendererPane rendererPane;

private static JTextArea textArea;

public static ComponentUI createUI(JComponent c) {
    return sharedInstance;
}

public MultiLineToolTipUI() {
    super();
}

public void installUI(JComponent c) {
    super.installUI(c);
    tip = (JToolTip) c;
    rendererPane = new CellRendererPane();
    c.add(rendererPane);
}

public void uninstallUI(JComponent c) {
    super.uninstallUI(c);

    c.remove(rendererPane);
    rendererPane = null;
}

public void paint(Graphics g, JComponent c) {
    Dimension size = c.getSize();
    textArea.setBackground(c.getBackground());
    rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1, size.height - 1, true);
}

public Dimension getPreferredSize(JComponent c) {
    String tipText = ((JToolTip) c).getTipText();
    if (tipText == null) return new Dimension(0, 0);
    textArea = new JTextArea(tipText);
    rendererPane.removeAll();
    rendererPane.add(textArea);
    textArea.setWrapStyleWord(true);
    int width = ((JMultiLineToolTip) c).getFixedWidth();
    int columns = ((JMultiLineToolTip) c).getColumns();

    if (columns > 0) {
        textArea.setColumns(columns);
        textArea.setSize(0, 0);
        textArea.setLineWrap(true);
        textArea.setSize(textArea.getPreferredSize());
    } else if (width > 0) {
        textArea.setLineWrap(true);
        Dimension d = textArea.getPreferredSize();
        d.width = width;
        d.height++;
        textArea.setSize(d);
    } else
        textArea.setLineWrap(false);

    Dimension dim = textArea.getPreferredSize();

    dim.height += 1;
    dim.width += 1;
    return dim;
}

public Dimension getMinimumSize(JComponent c) {
    return getPreferredSize(c);
}

public Dimension getMaximumSize(JComponent c) {
    return getPreferredSize(c);
}

Solution

  • Assuming you took this code from Yoshiko-app, there are examples on this same github:

    String pathTip = "Sets the tmp path.";
    JPanel pathPanel = new JPanel() {
        private static final long serialVersionUID = 1L;
        public JToolTip createToolTip() {
            return new JMultiLineToolTip();
        }
    };
    pathPanel.setLayout(new BorderLayout());
    pathPanel.setToolTipText(pathTip);
    

    You jsut need to adapt this code for JtextField