views:

39

answers:

2

Hey all,

I am having trouble getting a JSeparator to show up inside of a JToolBar. My toolbar is created as follows :

public class ToolBar extends JToolBar {
    super();

    FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 10, 5);
    setLayout(layout);

    add(new JButton("Button 1"));
    addSeparator();
    add(new JButton("Button 2"));
    add(new JButton("Button 3"));
    addSeparator();

    // Show
    setVisible(true);
    setFloatable(false);

}

Any thoughts would be really appreciated, I have been trying to get this to work for way too long now >(

+2  A: 

Trying your code there, when I call the addSeparator() method it creates a space between the buttons but no visible separation line.

But if I change the method to addSeparator(new Dimension(20,20)) it then creates the visible separation line.

The problem could be that the default look and feel creates a separator of height 1 so you would be unable to see it.

I am running it on Mac OSX.

Gordon
Yea, that is the same workaround that I ran into. I think it may just be an issue with the default OS X LAF
Hamy
+2  A: 

The biggest problem you have is that there is no need to sub-class JToolBar and set layout on it. Just create an instance of it and start adding buttons and separators.

In general Swing team does not recommend sub-classing Swing components.

You code should look like:

JToolBar t = new JToolbar();

t.add(new JButton("Button 1"));
t.addSeparator();
t.add(new JButton("Button 2"));
t.add(new JButton("Button 3"));
t.addSeparator();

// Show
t.setVisible(true);
t.setFloatable(false);

The last advice would be not to use buttons. Use actions. This way same actions can be used on toolbar, menus ect. More info at http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html

UPDATE: The way the toolbar separator looks depends on LAF you're using.

eugener
eugener, I have actually be *struggling* with this. I have a ton of subclassed items - JToolbar, JPanel, JButton, etc - for which most of them don't need to be subclasses at all. I have just been trying not to write all of my code into a single method or a single class!! I can do it, but it's like 20 pages long and hard to maintain. In a sense, I know that subclassing is not the right thing to do, but I also don't want huge unmaintainable files :( . Basically, I don't know the 'right' answer here. Do you have any general advice for this?!
Hamy
Most common way to deal with that is to create "build" methods. For example if you need to build the toolbar you create JToolBar buildToobar() method, where you create, configure and return your toolbar. The only time you have to subclass Swing components is when you need to add new state and/or behavior to them.
eugener
Hm. I suppose I could use a Factory class, so that I did not end up with my main class being bloated with build...() methods?
Hamy
In my experience there are many cases where I have very common build methods. For example I have a method where I pass in a collection of actions and it builds me a toolbar, the other one builds me a menu out of the same action collection. Of course there are very specific ones too, which I usually keep as part of the container/panel which needs them - they usually reference some internal variables of the container.Hope it makes sense :)UPDATE: Take a look at code examples at jgoodies.com.
eugener