“Flowing” JToolBar

  Kiến thức lập trình

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

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT