Can't turn off tooltips for text area inside s

2019-09-17 09:25发布

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.

1条回答
手持菜刀,她持情操
2楼-- · 2019-09-17 10:17

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.
查看更多
登录 后发表回答