From 9955011dad278183c77dcebb74864ace8ad52e3c Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Sun, 7 Dec 2025 17:19:17 -0500 Subject: Add a collapsible JPanel Adds a collapsible version of a JPanel, useful in various places --- .../examples/gui/panels/CollapsiblePanelDemo.java | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 base/src/examples/java/bjc/utils/examples/gui/panels/CollapsiblePanelDemo.java (limited to 'base/src/examples/java') diff --git a/base/src/examples/java/bjc/utils/examples/gui/panels/CollapsiblePanelDemo.java b/base/src/examples/java/bjc/utils/examples/gui/panels/CollapsiblePanelDemo.java new file mode 100644 index 0000000..fed128f --- /dev/null +++ b/base/src/examples/java/bjc/utils/examples/gui/panels/CollapsiblePanelDemo.java @@ -0,0 +1,71 @@ +package bjc.utils.examples.gui.panels; + +import java.awt.BorderLayout; +import java.awt.GridLayout; +import java.awt.Insets; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; + +import bjc.utils.gui.panels.CollapsiblePanel; + +public class CollapsiblePanelDemo { + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception ignored) {} + + JFrame frame = new JFrame("CollapsiblePanel Demo"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLayout(new BorderLayout()); + + JPanel content = new JPanel(); + content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); + + CollapsiblePanel section1 = new CollapsiblePanel("Search Options"); + // Add some content + section1.add(new JLabel("Keyword:")); + section1.add(new JTextField(20)); + section1.add(new JCheckBox("Case sensitive")); + + // Add custom header bits (e.g. a small gear button) + JButton gear = new JButton("\u2699"); // ⚙ + gear.setMargin(new Insets(0, 4, 0, 4)); + gear.setFocusable(false); + section1.addHeaderComponent(gear); + + CollapsiblePanel section2 = new CollapsiblePanel("Advanced Filters", new GridLayout(0, 2, 4, 4)); + section2.add(new JLabel("From date:")); + section2.add(new JTextField(10)); + section2.add(new JLabel("To date:")); + section2.add(new JTextField(10)); + section2.add(new JLabel("Status:")); + section2.add(new JComboBox<>(new String[]{"Any", "Open", "Closed"})); + section2.setCollapsed(true); // start collapsed + + // Example: a right-aligned "Reset" button in the header + JButton resetBtn = new JButton("Reset"); + resetBtn.setMargin(new Insets(0, 6, 0, 6)); + resetBtn.setFocusable(false); + section2.setHeaderComponent(resetBtn); + + content.add(section1); + content.add(Box.createVerticalStrut(8)); + content.add(section2); + + frame.add(new JScrollPane(content), BorderLayout.CENTER); + frame.setSize(450, 300); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + }); + } +} -- cgit v1.2.3