-1

i have a toolBar, and i need to put a series of jbuttons and jlabels to the left of the screen, but one of the buttons to the right. I can't make the last one move to the right.

this.panelControl=new JToolBar();

setLayout(new FlowLayout(FlowLayout.LEFT)); //i use it to move them to the left, probably wrong
panelControl.add(load);
panelControl.addSeparator();
panelControl.add(laws);
panelControl.addSeparator();
panelControl.add(play);
panelControl.add(stop);
panelControl.add(labelSteps);
panelControl.add(steps);
panelControl.add(labelTime);
panelControl.add(time);
panelControl.add(exit); // i need this to be in the right side, but i can't

this.add(panelControl);

Thank you very much in advance.

eisklat
  • 41
  • 6
  • 1
    (1-) `I now have the same problem with steps, which is a jspinner. how can i fix it?` - You have already been given an answer. You have also already been asked for an [mcve] to demonstrate the problem. Post your code showing what you tried!!! – camickr Apr 20 '19 at 17:04

1 Answers1

1

Use the JToolBar's own layout, but add a horizontal glue between the last two buttons. This will space things out for you. For example:

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;

@SuppressWarnings("serial")
public class BoxLayoutEg extends JPanel {
    public BoxLayoutEg() {
        String[] btnTexts = {"One", "Two", "Three", "Four", "Five", "Exit"};

        JToolBar toolBar = new JToolBar();
        for (int i = 0; i < btnTexts.length; i++) {
            JButton button = new JButton(btnTexts[i]);
            if (i != btnTexts.length - 1) {
                toolBar.add(button);
                toolBar.addSeparator();
            } else {
                toolBar.add(Box.createHorizontalGlue());
                toolBar.add(button);
            }
        }

        setLayout(new BorderLayout());
        add(toolBar, BorderLayout.PAGE_START);

        setPreferredSize(new Dimension(400, 300));

    }

    private static void createAndShowGui() {
        BoxLayoutEg mainPanel = new BoxLayoutEg();

        JFrame frame = new JFrame("BoxLayout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

As per VGR's comment:

It’s better to use createGlue(), so the stretched layout is preserved even if the JToolBar is floatable and is floated and re-docked vertically by the user.

and he's right. So change this:

} else {
    toolBar.add(Box.createHorizontalGlue());
    toolBar.add(button);
}

to this:

} else {
    toolBar.add(Box.createGlue());
    toolBar.add(button);
}

so that the separation persists even if the toolbar is placed in a vertical position

Also note that if you need to limit the size of the JTextField, you could set its maximal size to its preferred size, e.g.,

} else {
    int columns = 10;
    JTextField time = new JTextField(columns);
    time.setMaximumSize(time.getPreferredSize());
    toolBar.add(time);
    toolBar.add(Box.createHorizontalGlue());
    toolBar.add(button);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    It’s better to use [createGlue()](https://docs.oracle.com/en/java/javase/12/docs/api/java.desktop/javax/swing/Box.html#createGlue%28%29), so the stretched layout is preserved even if the JToolBar is floatable and is floated and re-docked vertically by the user. – VGR Apr 20 '19 at 16:04
  • if i do that, the jTextfield i have before the button (time) gets super long, to fill all the space. ¿How can i cut it? – eisklat Apr 20 '19 at 16:12
  • @eisklat: this is where a decent [mcve] code post comes in handy. Please see my code above as an example, then modify it to show your problem, and post it in your question. – Hovercraft Full Of Eels Apr 20 '19 at 16:25
  • 1
    @eisklat: One way to handle this is to limit the text field's maximum size by doing something like: `time.setMaximumSize(time.getPreferredSize());` – Hovercraft Full Of Eels Apr 20 '19 at 16:29
  • 1
    @eisklat but you will probably need to set the JTextField's column property first as per my addendum to answer above – Hovercraft Full Of Eels Apr 20 '19 at 16:31
  • Thank you very much, everything works now, you are the best – eisklat Apr 20 '19 at 17:03