2

I am learning Java Swing and have a optical issue that i am trying to solve.

When i use the System-Default Look and Feel on Windows 7, i get the huge close button and empty title-bar over the floating JToolBar.

Image of the floating toolbar only (without the application in background):

the floating toolbar

Is there a way to remove the surrounding Borders & Button of a floating toolbar? Or at least adjust their sizes?

Thanks the suggestion from nachokk and the following snipplet found on https://forums.oracle.com/thread/2279762 i may be did a step forward. But still, not working. Now after undocking, the toolbar become invisible.

    tbFile = new JToolBar();
    tbFile.addHierarchyListener(new HierarchyListener() {

        @Override
        public void hierarchyChanged(HierarchyEvent e) {
            if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) == 0) return;
            JToolBar bar = (JToolBar) e.getComponent();
            if (!((BasicToolBarUI) bar.getUI()).isFloating()) return;
            final Window topLevel = SwingUtilities.windowForComponent(bar);
            if (topLevel instanceof JDialog) {
                //((JDialog) topLevel).setVisible(false);
                ((JDialog) topLevel).setUndecorated(true);
                //((JDialog) topLevel).setVisible(true);
            }    
        }
    });
code_angel
  • 1,537
  • 1
  • 11
  • 21
  • don't think so, change Window theme – Maxim Shoustin Sep 28 '13 at 18:35
  • Could you add an image to be more descriptive? Try `JFrame#setUndecorated(true)` if it's what are you looking for.. – nachokk Sep 28 '13 at 18:45
  • I want have the decoration in the main frame, but not in the toolbar, while floating. Unfortunately there are no setUndecorated method in JToolBar class. – code_angel Sep 28 '13 at 19:53
  • @MaximShoustin, i don't look for a personal, rather general solution. For each user on any operating system – code_angel Sep 28 '13 at 19:56
  • it's illegal to set the undecorated property while the dialog is displayable - dispose it before calling that method. As is, it throws a IllegalStateException which (weirdly) is swallowed somewhere. – kleopatra Sep 29 '13 at 07:48

1 Answers1

1

Thanks to comments here and the answer of Yishai on https://stackoverflow.com/a/875317/1107653 , i got what i wanted - undecorated floating ToolBar. In order to use setUndecorated, the frame/dialog should be disposed first.

    tbFile = new JToolBar();
    final HierarchyListener hierarchyListener = new HierarchyListener() {

        @Override
        public void hierarchyChanged(HierarchyEvent e) {

            if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) == 0) return;
            JToolBar bar = (JToolBar) e.getComponent();
            if (!((BasicToolBarUI) bar.getUI()).isFloating()) return;

            Window topLevel = SwingUtilities.windowForComponent(bar);
            if(topLevel == null) return;

            topLevel.dispose();
            ((JDialog) topLevel).setUndecorated(true);
            topLevel.setVisible(true);

        }
    };
    tbFile.addHierarchyListener(hierarchyListener);
Community
  • 1
  • 1
code_angel
  • 1,537
  • 1
  • 11
  • 21