How to access all components beneath top JFrame

社会主义新天地 提交于 2019-12-04 22:03:14

I guess you need to recurse the whole tree, something like

  • start with the root
  • iterate all components
  • enable each of them
  • test if it's an instance of Container
  • if so, do a recursive call

For the ScrollPane you'll probably need an additional instanceof test and then getViewPort or something like this.

"In which pane do I find the JMenuBar and how would I enable the JMenus? (And even the JMenuItems."

The Root Pane of the JFrame holds the JMenuBar.

JFrame.getRootPane().getJMenuBar();

Of course you can always just call JFrame.getJMenuBar(), without having to access the root pane.

To get the menus of the menu bar, you can can JMenuBar.getSubElements which return the MenuElement[]. JMenu Also has getSubElements. Keep in mind though that a MenuELement can be JMenu or a JMenuItem, and a JMenu can have more layers of JMenus. So you will have to do some recursive calling if you wanted to try to access them this way.

As for trying to access specific component types, check if (obj instanceof SomeComponentType) will help you in what you are trying to achieve.

Here's what worked.

  for(int i = 0; i < menFileAndEdit.getMenuCount(); i++){

      menFileAndEdit.getMenu(i).setEnabled(true);

      Component [] cc = menFileAndEdit.getMenu(i).getMenuComponents();

      for(Component c : cc)

          c.setEnabled(true);
  }

That enables each JMenuItem in JMenu menFileAndEdit in the JMenuBar for the form.

That was a lot harder than expected, given the relative ease with which every other element was enabled by recursion using the method in the second edit of the original post. So I wonder if there's a way to use the recursive method to accomplish this.

I also wonder if there's a way to use a collection-based for loop. I tried a couple of things but they wouldn't compile.

DSlomer64

Six months later, I have a decent, not-altogether-inelegant way to access everything that I needed to below main JFrame, in the context of a menu option action that sets all tool tips to "":

private void tipsOff(Container container)
{
  Component [] c = container.getComponents();
  Component s;
  for (Component cc : c) {
    ((JComponent)cc).setToolTipText("");
    if(cc instanceof JPanel)      tipsOff((Container) cc);
    if(cc instanceof JScrollPane) tipsOff(((JScrollPane)cc).getViewport());
  }
}  

private void mniPrefTooltipsActionPerformed(java.awt.event.ActionEvent evt)
{                                                
  tipsOff(gui.getContentPane());
  tipsOff(gui.mbrMenuBar);
}     

Note recursive call to tipsOff.

Also note comments and Answer about using ToolTipManager (in this thread, which works in the context of my original question to reduce all the code to one line. Point here is that any other operation could be applied instead of ""-ing tool tips--e.g., setting invisible or disabling... etc.

Thanks to maaartinus for the JScrollPane line in tipsOff.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!