Why does addSeparator () not work with my JToolBar?

I am having trouble getting JSeparator inside 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 greatly appreciated, I tried to get this to work for too long now> (

+3
source share
3 answers

Having tried my code there when I call the method addSeparator(), it creates a space between the buttons, but without a visible dividing line.

But if I change the method to addSeparator(new Dimension(20,20)), it will create a visible dividing line.

The problem may be that the default appearance creates a height separator of 1, so you won’t be able to see it.

I run it on Mac OSX.

+4

, , , JToolBar . .

Swing Swing.

:

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);

- . . , , . http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html

UPDATE: , , LAF.

+2

. , .

.

    // ---------------------------------------
    // debug below:
    // ---------------------------------------
    JSeparator separator = new JSeparator(JSeparator.VERTICAL);
    System.err.println("getMaximumSize(): " + separator.getMaximumSize());
    System.err.println("getMinimumSize(): " + separator.getMinimumSize());
    separator.setMaximumSize(new Dimension(2, separator.getMaximumSize().height));


    // ---------------------------------------
    // real sample below
    // ---------------------------------------
    // adds a vertical space bar
    toolBar.add(Box.createHorizontalStrut(5));

    // adds a vertical separator
    JSeparator separator = new JSeparator(JSeparator.VERTICAL);
    Dimension maximumSize = separator.getMaximumSize();
    maximumSize.width = 2;
    separator.setMaximumSize(maximumSize); // Important! Update it!
    toolBar.add(separator);

    // adds a vertical space bar
    toolBar.add(Box.createHorizontalStrut(5));

by TJ Tsai ( tsungjung411@yahoo.com.tw )

0
source

Source: https://habr.com/ru/post/1753565/


All Articles