'Arrow pointing right to collapse, pointing to down to expand(Java Swing) [closed]

I'm developing a plugin. For the UI, I want to add an arrow and a title as follow. If we click the button, three fields below it will expand or collapse. Anyone knows how to implement it in Java Swing ?enter image description here



Solution 1:[1]

A JMenu may be appropriate. Here is a small app that demonstrates.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class MenuTest {

    private JPanel createButton() {
        JPanel panel = new JPanel();
        JMenu menu = new JMenu("George  \u25B6");
        menu.setBorder(BorderFactory.createLineBorder(Color.black));
        menu.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {
            
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                menu.setText("George  \u25BC");
            }
            
            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                menu.setText("George  \u25B6");
            }
            
            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                // Do nothing.
            }
        });
        JMenuItem first = new JMenuItem("Best");
        menu.add(first);
        JMenuItem second = new JMenuItem("Northern");
        menu.add(second);
        JMenuItem last = new JMenuItem("Ireland");
        menu.add(last);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        panel.add(menuBar);
        JButton button = new JButton("Exit");
        button.addActionListener(e -> System.exit(0));
        panel.add(button);
        return panel;
    }

    private void showGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createButton(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new MenuTest().showGui());
    }
}

Refer to How to Use Menus

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abra