Swing: multiline toolbar or using Flowlayout as toolbar

1.2k Views Asked by At

I have toolbar with random number of buttons from two up to twenty. Width of each button can vary.

I have to put them all at top area and use JToolBar. On a small screen resolutions (e.g. 800x600) some buttons could be out of screen (AFAIK JToolbar can't grows in a height).

Any ideas how to adjust JToolbar to grows in a height or resolve the task in any other way (e.g. using Flowlayout)?

3

There are 3 best solutions below

0
M. Al Jumaily On

I am not sure if this might be helpful, how about you use a ribbonbar instead? That way, it is less complicated for the user (if you have more than ~7 buttons) and seems efficient. To implementation goes as follows (an example on the fly): create a JTabbedPane and add the following tabs File, Edit, Help. In File, have New Project, Open, Save, Save As... in Edit, have Copy, Paste, etc.. and so on.

4
prasad_ On

This solution doesn't use a JToolBar, but a GridLayout with multiple buttons. The program accepts n number of buttons and lays them in the container. The number of columns (or buttons) per row is constant and the number of rows grow based upon the number of buttons.

For example, if a row accommodates 8 columns or buttons - 12 buttons are placed in two rows. Here is some code to show what is possible. Note the program accepts an integer number (number of buttons) as command-line argument.

import javax.swing.*;
import java.awt.*;
public class ToolbarGrid {
    private static final int MAX_BUTTONS_PER_ROW = 4;
    public static void main(String [] args) throws Exception {
        if (args.length == 0) {
            System.out.println("Enter number of buttons as arg> java -cp . ToolbarGrid 9");
            return;
        }
        int noOfButtons = Integer.parseInt(args[0]);
        JFrame frame = new JFrame();
        frame.setTitle("Testing Toolbar in a Grid");
        GridLayout gLayout = new GridLayout(0, MAX_BUTTONS_PER_ROW); // 0 rows = any number of rows
        JPanel panel = new JPanel();
        panel.setLayout(gLayout);
        panel = panelWithButtons(noOfButtons, panel);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();       
        frame.setVisible(true);
    }
    private static JPanel panelWithButtons(int noOfButtons, JPanel panel) {
        for (int i = 0; i < noOfButtons; i++) {
            panel.add(new JButton("Button_" + i+1));
        }
        return panel;
    }
}


EDIT: In a similar manner, one can use multiple toolbars. Each toolbar limiting n number of buttons. Also, the number of buttons per toolbar can also be determined by the total width of buttons.

3
Dakshinamurthy Karra On

JToolBar can increase the height as and when the added components preferredSize is set. Here is an example:

package toolbar;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

public class ToolbarTest extends JFrame {
    private static final long serialVersionUID = 1L;

    private int n = 1;

    public ToolbarTest() {
        super(ToolbarTest.class.getName());
        JToolBar tb = new JToolBar();
        tb.add(new AbstractAction("First Action") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                String text = "";
                for (int i = 0; i < n; i++) {
                    text += "Button - " + i + "<br>";
                }
                n++;
                JButton b = new JButton("<html>" + text + "</html>");
                tb.add(b);
                ToolbarTest.this.doLayout();
                ToolbarTest.this.pack();
            }
        });
        getContentPane().add(tb, BorderLayout.NORTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        ToolbarTest tbt = new ToolbarTest();
        SwingUtilities.invokeLater(() -> tbt.setVisible(true));
    }
}