Java Code Examples for javax.swing.plaf.basic.BasicToolBarUI

The following examples show how to use javax.swing.plaf.basic.BasicToolBarUI. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
@Override public void actionPerformed(ActionEvent e) {
  Component root;
  Container parent = SwingUtilities.getUnwrappedParent((Component) e.getSource());
  if (parent instanceof JPopupMenu) {
    JPopupMenu popup = (JPopupMenu) parent;
    root = SwingUtilities.getRoot(popup.getInvoker());
  } else if (parent instanceof JToolBar) {
    JToolBar toolbar = (JToolBar) parent;
    if (((BasicToolBarUI) toolbar.getUI()).isFloating()) {
      root = SwingUtilities.getWindowAncestor(toolbar).getOwner();
    } else {
      root = SwingUtilities.getRoot(toolbar);
    }
  } else {
    root = SwingUtilities.getRoot(parent);
  }
  if (root instanceof Window) {
    Window window = (Window) root;
    window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
  }
}
 
Example 2
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
@Override public void actionPerformed(ActionEvent e) {
  Component root = null;
  Container parent = SwingUtilities.getUnwrappedParent((Component) e.getSource());
  if (parent instanceof JPopupMenu) {
    JPopupMenu popup = (JPopupMenu) parent;
    root = SwingUtilities.getRoot(popup.getInvoker());
  } else if (parent instanceof JToolBar) {
    JToolBar toolbar = (JToolBar) parent;
    if (((BasicToolBarUI) toolbar.getUI()).isFloating()) {
      root = SwingUtilities.getWindowAncestor(toolbar).getOwner();
    } else {
      root = SwingUtilities.getRoot(toolbar);
    }
  } else {
    root = SwingUtilities.getRoot(parent);
  }
  if (root instanceof Window) {
    Window window = (Window) root;
    window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
  }
}
 
Example 3
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
@Override public void actionPerformed(ActionEvent e) {
  Component root = null;
  Container parent = SwingUtilities.getUnwrappedParent((Component) e.getSource());
  if (parent instanceof JPopupMenu) {
    JPopupMenu popup = (JPopupMenu) parent;
    root = SwingUtilities.getRoot(popup.getInvoker());
  } else if (parent instanceof JToolBar) {
    JToolBar toolbar = (JToolBar) parent;
    if (((BasicToolBarUI) toolbar.getUI()).isFloating()) {
      root = SwingUtilities.getWindowAncestor(toolbar).getOwner();
    } else {
      root = SwingUtilities.getRoot(toolbar);
    }
  } else {
    root = SwingUtilities.getRoot(parent);
  }
  if (root instanceof Window) {
    Window window = (Window) root;
    window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
  }
}
 
Example 4
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
@Override public void actionPerformed(ActionEvent e) {
  Component root;
  Container parent = SwingUtilities.getUnwrappedParent((Component) e.getSource());
  if (parent instanceof JPopupMenu) {
    JPopupMenu popup = (JPopupMenu) parent;
    root = SwingUtilities.getRoot(popup.getInvoker());
  } else if (parent instanceof JToolBar) {
    JToolBar toolbar = (JToolBar) parent;
    if (((BasicToolBarUI) toolbar.getUI()).isFloating()) {
      root = SwingUtilities.getWindowAncestor(toolbar).getOwner();
    } else {
      root = SwingUtilities.getRoot(toolbar);
    }
  } else {
    root = SwingUtilities.getRoot(parent);
  }
  if (root instanceof Window) {
    Window window = (Window) root;
    window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
  }
}
 
Example 5
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());

  JToolBar toolbar = new JToolBar("ToolBar");
  toolbar.add(new JCheckBox("JCheckBox"));
  toolbar.add(new JTextField(10));

  EventQueue.invokeLater(() -> {
    Container w = getTopLevelAncestor();
    if (w instanceof Window) {
      // Setting a specific location for a floating JToolBar
      // https://stackoverflow.com/questions/41701664/setting-a-specific-location-for-a-floating-jtoolbar
      Point pt = w.getLocation();
      BasicToolBarUI ui = (BasicToolBarUI) toolbar.getUI();
      ui.setFloatingLocation(pt.x + 120, pt.y + 160);
      ui.setFloating(true, null);
    }
  });

  // // TEST: Here is another approach
  // EventQueue.invokeLater(() -> {
  //   Window w = (Window) getTopLevelAncestor();
  //   Point pt = w.getLocation();
  //   ((BasicToolBarUI) toolbar.getUI()).setFloating(true, null);
  //   Container c = toolbar.getTopLevelAncestor();
  //   if (c instanceof Window) {
  //     c.setLocation(pt.x + 120, pt.y + 160);
  //   }
  // });

  add(toolbar, BorderLayout.NORTH);
  add(Box.createHorizontalStrut(0), BorderLayout.WEST);
  add(Box.createHorizontalStrut(0), BorderLayout.EAST);
  add(new JScrollPane(new JTree()));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 6
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 4 votes vote down vote up
private static JToolBar makeVerticalToolBar() {
  JToolBar toolbar = new JToolBar(SwingConstants.VERTICAL);
  // toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.PAGE_AXIS));

  // TEST:
  // JToolBar toolbar = new JToolBar(orientation) {
  //   @Override public Dimension getPreferredSize() {
  //     if (((BasicToolBarUI) getUI()).isFloating()) {
  //       setLayout(new GridLayout(0, 3));
  //       return new Dimension(ICON_SIZE * 3, ICON_SIZE * 2);
  //     } else {
  //       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  //       return super.getPreferredSize();
  //     }
  //   }
  //   @Override public Dimension getMinimumSize() {
  //     return getPreferredSize();
  //   }
  //   @Override public Dimension getMaximumSize() {
  //     return getPreferredSize();
  //   }
  // };

  JPanel panel = new JPanel() {
    @Override public Dimension getPreferredSize() {
      if (((BasicToolBarUI) toolbar.getUI()).isFloating()) {
        setLayout(new GridLayout(0, 3));
        return new Dimension(ICON_SIZE * 3, ICON_SIZE * 2);
      } else {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        return super.getPreferredSize();
      }
    }

    @Override public Dimension getMinimumSize() {
      return getPreferredSize();
    }

    @Override public Dimension getMaximumSize() {
      return getPreferredSize();
    }
  };
  panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  panel.add(new ColorPanel(Color.RED));
  panel.add(new ColorPanel(Color.GREEN));
  panel.add(new ColorPanel(Color.BLUE));
  panel.add(new ColorPanel(Color.ORANGE));
  panel.add(new ColorPanel(Color.CYAN));

  toolbar.add(panel);
  toolbar.add(Box.createGlue());

  // TEST: AncestorListener
  // toolbar.addAncestorListener(new AncestorListener() {
  //   @Override public void ancestorAdded(AncestorEvent e) {
  //     Container pp = e.getAncestorParent();
  //     if (pp instanceof JFrame) {
  //       System.out.println(((JFrame) pp).getTitle());
  //     }
  //     Container p = e.getAncestor();
  //     if (p instanceof JDialog) {
  //       System.out.println("  GridLayout");
  //       panel.setLayout(new GridLayout(0, 3));
  //       ((JDialog) p).pack();
  //       // ((JDialog) p).setSize(ICON_SIZE * 3, ICON_SIZE * 2);
  //     } else {
  //       System.out.println("  BoxLayout");
  //       panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  //     }
  //   }
  //   @Override public void ancestorMoved(AncestorEvent e) {}
  //   @Override public void ancestorRemoved(AncestorEvent e) {}
  // });
  return toolbar;
}