“Flowing” JToolBar

Here’s my goal: to make a toolbar with buttons that “flow” down as the window is shrinked far enough. It should not be shy about nudging the sibling below if necessary

How do I do it?

Here’s a demo. I addressed some criticisms of my previous questions about using external libraries so none are used

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

public class FlowToolbarDemo {
    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("Flow Toolbar Demo");
        JPanel labelPanel = new JPanel(new BorderLayout());
        JLabel label = new JLabel("Click to see action");
        label.setOpaque(true);
        label.setBackground(new Color(255, 255, 204));
        labelPanel.add(label, BorderLayout.CENTER);

        JToolBar toolbar = new JToolBar();
        LayoutManager toolbarLayout = new FlowLayout();
        toolbar.setLayout(toolbarLayout);
        JButton heartButton = getHeartButton(e -> label.setText("Heart action performed..."));
        JButton starButton = getStarButton(e -> label.setText("Star action performed..."));
        toolbar.add(heartButton);
        toolbar.add(starButton);

        LayoutManager headerPanelLayout = new BorderLayout();
        JPanel headerPanel = new JPanel(headerPanelLayout);
        headerPanel.add(labelPanel, BorderLayout.NORTH);
        headerPanel.add(toolbar, BorderLayout.CENTER);

        JPanel businessPanel = getBusinessPanel();

        LayoutManager mainPanelLayout = new BorderLayout();
        JPanel mainPanel = new JPanel(mainPanelLayout);
        mainPanel.add(headerPanel, BorderLayout.NORTH);
        mainPanel.add(businessPanel, BorderLayout.CENTER);

        frame.setContentPane(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
        System.out.println();
    }

    private static JPanel getBusinessPanel() {
        LayoutManager businessPanelLayout = new BorderLayout();
        JPanel businessPanel = new JPanel(businessPanelLayout);
        JTable tableOne = getTableOne();
        JScrollPane scrollPaneOne = new JScrollPane(tableOne);
        scrollPaneOne.setOpaque(true);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Business Table One", scrollPaneOne);
        JTable tableTwo = getTableTwo();
        JScrollPane scrollPaneTwo = new JScrollPane(tableTwo);
        scrollPaneTwo.setOpaque(true);
        tabbedPane.addTab("Business Table Two", scrollPaneTwo);
        businessPanel.add(tabbedPane);
        return businessPanel;
    }

    private static JTable getTableOne() {
        Object[][] data = {
                {"John Doe", 30, "Male"},
                {"Jane Smith", 25, "Female"},
                {"Alice Johnson", 35, "Female"}
        };
        String[] columns = {"Name", "Age", "Gender"};
        DefaultTableModel model = new DefaultTableModel(data, columns);
        return new JTable(model);
    }

    private static JTable getTableTwo() {
        Object[][] data = {
                {"Apple", 30, true},
                {"Orange", 25, true},
                {"Ackee fruit", 35, false}
        };
        String[] columns = {"Fruit", "Quantity", "Is Edible"};
        DefaultTableModel model = new DefaultTableModel(data, columns);
        return new JTable(model);
    }

    private static JButton getHeartButton(ActionListener heartAction) throws IOException {
        return getButton("Do Heart action", "heart.png", heartAction);
    }
    private static JButton getStarButton(ActionListener starAction) throws IOException {
        return getButton("Do Star action", "star.png", starAction);
    }
    
    private static JButton getButton(String buttonText, String resourceName, ActionListener actionListener) throws IOException {
        URL imageLocation = FlowToolbarDemo.class.getResource(resourceName);
        BufferedImage image = ImageIO.read(Objects.requireNonNull(imageLocation, "Requested resource not found"));
        Image scaledImage = image.getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        ImageIcon icon = new ImageIcon(scaledImage);
        JButton button = new JButton(buttonText, icon);
        button.addActionListener(actionListener);
        button.setFocusPainted(false);
        return button;
    }
}

heart.png

star.png

The buttons do flow, but they flow below the allocated area for the north component and become mostly invisible. JTabbedPane pane titles, on the other hand, flow smoothly. I included it in part to show what type of “flowing” I’m trying to achieve with my toolbar buttons

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật