-1

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
FoxyBOA
  • 5,788
  • 8
  • 48
  • 82

3 Answers3

1

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));
    }
}
Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • You didn't get my issue. If number of buttons will be big enough (or screen resolution will be small) all buttons starting from let's say button X will be out of screen. I need a simbiosis behaviour between toolbar and flowlayout (when all out of seen buttons will be on a new row). – FoxyBOA Aug 01 '18 at 06:23
  • Adding to your sample as proposed @AndrewThompson tb.setLayout(new FlowLayout()); didn't help either. Out of seen buttons appears only after resize and partially visible. – FoxyBOA Aug 01 '18 at 06:27
  • Using `setLayout(new FlowLayout())` and adding the toolbar with BorderLayout.CENTER works. Looks like BorderLayout is not calculating the height properly when using BorderLayout.NORTH. Check it out. – Dakshinamurthy Karra Aug 01 '18 at 09:43
0

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.

M. Al Jumaily
  • 731
  • 1
  • 6
  • 21
0

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.

prasad_
  • 12,755
  • 2
  • 24
  • 36
  • It's clear direct solution, unfortunately my buttons have variable width (vary variable). I could be pessimistic and allow let's say only 3/4 buttons per toolbar, but that means that for some cases UI will be ugly: (line 1: bbbb hell lot of free spaces; line 2: bbb) – FoxyBOA Aug 01 '18 at 06:32
  • Can you tell what the buttons look like? Do they have long text or large buttons, buttons with images, etc. What is the size of the window in which the button toolbar is being placed? – prasad_ Aug 01 '18 at 07:07
  • Few buttons have only images (16x16). Most buttons have localizable text (based on user language width of this button can vary - ???x16). Windows size has to be 800x600 and up. – FoxyBOA Aug 01 '18 at 07:11
  • If possible post a sketch of how you are visualizing the outcome. – prasad_ Aug 01 '18 at 07:16