Search code examples
javaswingtooltipjlabel

JLabel getToolTipText() not returning tooltips that have been set


So i'm working on something that will find the locations of Jlabels on the screen so i can use these positions to generate data later at these points depending on what type of point it is.

To keep track of specific points, i have set the tooltip of the important labels to certain text so i can add them to a Map<String, Rectangle>. The rectangle will give its location on the screen, as well as the size of the label. This will be used to generate the positioning for my data points.

The problem right now is that I am not getting any strings that ive set, using the getToolTipText() on the JLabels.

public Map<String, Rectangle> positions() {        
    Map<String, Rectangle> ioPoints = new HashMap<>();
    return positions(mainPanel, ioPoints);
}

// Runs through each component starting from the mainPanel which contains everything
// Only labels have tooltips
public Map<String, Rectangle> positions(Container p1, Map<String, Rectangle> ioPoints) {

    for (Component p : p1.getComponents()) {

        if (p instanceof JLabel) {

            try {

                if (((JLabel) p).getToolTipText() != null) {
                    // Never hit at all???
                    System.out.println("Has a tooltip:" + ((JLabel) p).getToolTipText());
                    Rectangle r = p.getBounds();
                    Component par = p;
                    while (par.getParent() != mainPanel) {
                        par = par.getParent();
                    }
                    r = SwingUtilities.convertRectangle(par, r, mainPanel);
                    //Point spot = ((JLabel) p).getLocation();
                    ((JLabel) p).setText("x=" + r.getX() + ", y=" + r.getY());
                    Rectangle oldRect = ioPoints.put(((JLabel) p).getToolTipText(), r);
                    if (oldRect != null) {
                        System.out.println("Replaced " + ((JLabel) p).getToolTipText()
                                + ".\nOld rectangle " + oldRect.toString()
                                + "\nNew rectangle: " + r.toString());
                    }
                    //System.out.println("Position: " + spot.toString() + "\tr: " + r.toString());
                }
            } catch (NullPointerException | IllegalComponentStateException e) {
                System.out.println("Error with " + ((JLabel) p).getName());
            }
        } else {
            if (p instanceof JPanel) {
                return positions((Container) p, ioPoints);
            }
        }
    }

    return ioPoints;

}

Example code of where i set tooltips.

tooltip = new String[]{"Comp Amps Temp " + rack.getName() + " `%sgname` `%compname`"};

for (int i = 0; i < numSg; i++) {
    for (int j = 0; j < comp[i]; j++) {
        label = new JLabel("");
        label.setToolTipText(tooltip[0]
                .replace("`%sgname`", rack.getSuctionGroupNameIndex(i))
                .replace("`%compname`", rack.getSuctionGroupIndex(i).getCompressorNameIndex(j)));
        label.setFont(font);
        label.setOpaque(true);
        label.setBorder(border);
        label.setBackground(Colours.BlueLight.getCol());
        panel.add(label, c);
        c.gridx += 1;
    }
}

Tooltip showing on JPanel

Any ideas why jlabel.getToolTipText(); wont give me any values for tool tips I've clearly set.


Solution

  • I suggest you just create a class that extends JLabel like so:

    public class ToolTippedJLabelOrWhateverYouWantToCallIt extends JLabel {
        String tooltip = null;
        @Override
        public void setToolTipText(String text) {
            tooltip = text;
            super.setToolTipText(text);
        }
        @Override
        public String getToolTipText() {
            return tooltip;
        }
    }
    

    There might be another way to get the tooltip text, but this is just a simple way.