0

enter image description here

All Swing components in my app (except labels) have tooltips that can be annoying once the user knows what's going on, so I have a Preferences menu that allows turning them off. I could name every component and set its tooltip text to "" [e.g., txtPattern.setToolTipText("");] (and 10 others), but I decided (with SO aid that started awhile back) to write code that would be more elegant (a learning experience):

  private void tipsOff(Container container){

    Component [] c = container.getComponents();

    for (Component cc : c)
      ((JComponent)cc).setToolTipText("");
  }  

  private void mniPrefTooltipsActionPerformed(java.awt.event.ActionEvent evt) {                                                

    if(! mniPrefTooltips.isSelected()){
       tipsOff(gui.getContentPane());
       tipsOff(gui.pnlLetters);
       tipsOff(gui.mbrMenuBar);
    }
    else{
      gui.dispose();
      gui = new IO();
      gui.setVisible(true);
    }
  }                                               

I have a problem, which is that the tooltips are NOT turned off for the two large text areas at the bottom of the gui (highlighted in the Navigator pane). The two buttons (marked with green in Nav. pane) ARE processed correctly. These items are supposed to be processed via the first call to tipsOff, which processes gui.getContentPane()).

(I added the two lines bellow to try to rectify the problem. Nope.)

tipsOff(gui.scrOutput); 
tipsOff(gui.scrScratch);

(Also tried this. Nope.)

   tipsOff(gui.txaOutput);
   tipsOff(gui.txaScratchwork);

How can I elegantly (i.e., assume I have many text areas, not just 2) turn off the text area tooltips?

P.S. I get the message Access of private field of another object for all but the first call to tipsOff. I don't care, owing to the nature of the task at hand.

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • 1
    It might be better looking at the TooltipManager... – MadProgrammer Oct 17 '14 at 21:15
  • 3
    `TooltipManager.sharedInstance().setEnabled( false )` definitely sounds more elegant to me. – Robin Oct 17 '14 at 21:30
  • Owe... miiigh... gawwwd... MadProgrammer and @Robin... thanks... – DSlomer64 Oct 17 '14 at 23:15
  • @MadProgrammer I am going to make that an answer so it can be accepted and the question marked as solved. – Robin Oct 18 '14 at 09:00
  • 1
    @Robin Wasn't thinking of doing it that way, but that's a better solution, go ahead, let me know so I can upvote it ;) – MadProgrammer Oct 18 '14 at 09:01
  • The original intent of my "learning experience" was drilling down through all components from top frame. So let's say I do NOT want to disable tooltips but that I instead want to ... I don't know... enable or disable or hide or make visible or change font or something else for each component. I guess I should ask a new question since this comment is pretty deeply buried and the OQ is 3 days old. If nobody gives an answer in this thread that's what I'll do. – DSlomer64 Oct 20 '14 at 23:00
  • Changed my mind. I went back to an old question I had and finished the learning experience [there](http://stackoverflow.com/questions/23177407/how-to-access-all-components-beneath-top-jframe/26433529#26433529) – DSlomer64 Oct 22 '14 at 15:30

1 Answers1

3

Use ToolTipManager.sharedInstance().setEnabled( false ) to disable all tool tips in your Swing application.

Benefits compared to your approach

  • It works :-)
  • You do not clear the tooltips, so it is easy to re-enable them again. For example if you want to offer UI to your user to activate/de-activate the tooltips this approach will work. In your approach, you would have to restore all the tooltips you previously cleared, which would be difficult to do in a generic way.
Robin
  • 36,233
  • 5
  • 47
  • 99
  • @Robin--Restoring the tooltips is easy: as shown, I just `dispose` of the current and instantiate a `new` gui, in which the tooltips are defined. Kind of hacky, but works in a generic way. (Or did I miss your point?) – DSlomer64 Oct 20 '14 at 22:54
  • And I have implemented your suggestion, @MadProgrammer. One line of code to hide, one line to show. – DSlomer64 Oct 20 '14 at 23:18
  • Make that ONE line: `ToolTipManager.sharedInstance().setEnabled( mniPrefTooltips.isSelected());` – DSlomer64 Oct 21 '14 at 00:23