1

I followed this tutorial: http://explodingpixels.wordpress.com/2008/05/02/sexy-swing-app-the-unified-toolbar/ to create a native looking toolbar on the mac. The problem might be that I'm not adding it correctly to the JFrame, or maybe misunderstanding something.

The way the program should work is adding a panel to the toolbar (or underneath it, I think- it was not clear). Just in case there is confusion: The unified toolbar is the regular toolbar, only with buttons inside it as well.

This is what is should look like: enter image description here

This is what it does look like: (It shouldn't matter that I'm using different looking buttons, should it?) enter image description here

Code for the Unified Toolbar Panel:

package gui;
import java.awt.Color;
import java.awt.Window;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import com.jgoodies.forms.factories.Borders;

public class UnifiedToolbarPanel extends JPanel {

    public static final Color OS_X_UNIFIED_TOOLBAR_FOCUSED_BOTTOM_COLOR =
            new Color(64, 64, 64);
    public static final Color OS_X_UNIFIED_TOOLBAR_UNFOCUSED_BORDER_COLOR =
            new Color(135, 135, 135);    

    public UnifiedToolbarPanel() {
        // make the component transparent
        setOpaque(false);
        // create an empty border around the panel
        // note the border below is created using JGoodies Forms
        setBorder(Borders.createEmptyBorder("3dlu, 3dlu, 1dlu, 3dlu"));
    }



    @Override
    public Border getBorder() {
        Window window = SwingUtilities.getWindowAncestor(this);
        return window != null && window.isFocused()
                ? BorderFactory.createMatteBorder(0,0,1,0,
                        OS_X_UNIFIED_TOOLBAR_FOCUSED_BOTTOM_COLOR)
                : BorderFactory.createMatteBorder(0,0,1,0,
                       OS_X_UNIFIED_TOOLBAR_UNFOCUSED_BORDER_COLOR);
    }
}

Code for the JFrame:

package gui;

import java.awt.EventQueue;
import gui.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Color;
import javax.swing.plaf.metal.*;
public class HaiCue extends JFrame{
    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HaiCue window = new HaiCue();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public HaiCue() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        setForeground(Color.BLACK);
        setBounds(100, 100, 450, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        getRootPane().putClientProperty("apple.awt.brushMetalLook", Boolean.TRUE);

        JPanel panel = new UnifiedToolbarPanel();
//      panel.setLayout(new WrapLayout());
        add(panel, BorderLayout.NORTH); //<--- I think the problem may be how I add it... I have tried several different ways.

        ToolButton lblStop = new ToolButton("Stop", new ImageIcon(getClass().getResource("/images/stop.png")));
        panel.add(lblStop);

        ToolButton btnToolBox = new ToolButton("Tool Box", new ImageIcon(getClass().getResource("/images/Toolbox.png")));
        panel.add(btnToolBox);

        ToolButton btnInspector = new ToolButton("Inspector", new ImageIcon(getClass().getResource("/images/Toolbox.png")));
        panel.add(btnToolBox);
    }
}

I'm running OSX 10.8.2 and Java 1.7.0_13

sinθ
  • 11,093
  • 25
  • 85
  • 121
  • Waht is the Issue ? are you using any thirdPartyButton controls. ? – Akshay Joy May 22 '13 at 13:39
  • @AkshayJoy Maybe this wasn't clear, but the issue is that it is not working correctly. I showed the image of what the tutorial says it should look like versus what it actually looks like (The buttons are just normal JButtons, I was just explaining why they were different) – sinθ May 22 '13 at 13:43
  • I have Kept this Updated Code in this LINK http://www.sendspace.com/file/i3d5ms – Akshay Joy May 22 '13 at 13:46
  • @AkshayJoy What did you change? It just seems like you replaced the buttons with JButtons and got rid of the border... – sinθ May 22 '13 at 13:52
  • For me the buttons Coming Properly, you added the same buttonto the form two times. check the Last line of code – Akshay Joy May 22 '13 at 13:57
  • This example does look correct to me. Java 7u45, OSX 10.9. – Hakanai Jan 12 '14 at 13:45

2 Answers2

2

The preferred size of the button(s) in a tool bar hinges critically in the size of the Icon supplied. Note that pack() is essential to causing a "Window to be sized to fit the preferred size and layouts of its subcomponents." The example below ignores some the finer points adduced in the article, but it captures the essential geomtry. Try it with OptionPane.warningIcon to see the effect.

image

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.UIManager;

/**
 * @see http://stackoverflow.com/a/16694524/230513
 */
public class Main {

    private void display() {
        JFrame f = new JFrame("Main");
        f.getRootPane().putClientProperty("apple.awt.brushMetalLook", true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(createToolBar(f), BorderLayout.NORTH);
        f.add(new JTextArea(5, 16));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JToolBar createToolBar(final Component parent) {
        JToolBar bar = new JToolBar("Toolbar");
        bar.setFloatable(false);
        bar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        bar.add(createButton("FileChooser.detailsViewIcon"));
        bar.add(createButton("FileChooser.homeFolderIcon"));
        bar.add(createButton("FileChooser.newFolderIcon"));
        return bar;
    }

    private JButton createButton(String s) {
        JButton b = new JButton(UIManager.getIcon(s));
        b.setHorizontalTextPosition(JButton.CENTER);
        b.setVerticalTextPosition(JButton.CENTER);
        return b;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you, but for some reason the "toolbar" shows up on my computer as a different color than the top color. I copied the code exactly. – sinθ May 22 '13 at 14:55
  • Here is a pic: http://picpaste.com/pics/8b8028d8921d3d6c3dfa79ad46ebb2af.1369234691.png – sinθ May 22 '13 at 14:58
  • Also the brushMetalLook shows up much lighter on mine than yours and others (in the toolbar). Could it be the OSX version or the JVM? – sinθ May 22 '13 at 15:07
  • Yes & yes. I suspect that Java 7 on Mac OS 10.8 is ignoring the client property. Use Pixie or [`Zoom`](http://stackoverflow.com/a/3742841/230513) to see the color. If it's solid, there's a scheme outlined [here](http://stackoverflow.com/a/16625260/230513) to get the color dynamically. You might also look at [macwidgets](http://code.google.com/p/macwidgets/). – trashgod May 22 '13 at 18:13
  • Thanks, but I'm a little confused. If what's solid? The color, which one? Once a get said color, what should I do with it... i'm pretty new to java/swing/programming – sinθ May 22 '13 at 19:01
  • I'd try `UIManager.put("Panel.background", color)`; failing that, `panel.setBackground(color)`. If it's not a solid color, maybe use a `TexturePaint`, for [example](http://stackoverflow.com/a/11556441/230513). Failing that, hope for a bug fix. – trashgod May 24 '13 at 01:39
  • The lighter shade of the `BorderLayout.NORTH` panel that doesn't match that of the frame's drag bar. – trashgod May 24 '13 at 15:47
0

Please check the Last Lien of Code,

 panel.add(btnInspector );// repalce btnInspector  with btnToolBox
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23