質問のコードは makePanel でフィールドを初期化していますが、そこまで行っていればあと少しで「コンストラクタパラメータで渡してしまえば」と気付けたように思います。
回答としては TN8001 さんと同じく「コンテンツの生成をリンク先のようにメソッドで持つ必要は無い」ということになります。
java
1 class Main {
2 public static void main ( String [ ] args ) {
3 JFrame f = new JFrame ( ) ;
4 JPanel p = new JPanel ( ) ;
5 Accordion instance = new Accordion ( "mockTitle" , new CheckBoxContents ( "chk1" , "chk2" , "chk3" ) ) ;
6 f . add ( p ) ;
7 p . add ( instance ) ;
8 f . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
9 f . setBounds ( 100 , 100 , 640 , 400 ) ;
10 f . setVisible ( true ) ;
11 }
12 }
13 //CheckBox_Accordion の makePanel で作っていたヤツ
14 class CheckBoxContents extends JPanel {
15 CheckBoxContents ( String . . . chkboxTitle ) {
16 super ( null ) ;
17 setLayout ( new BoxLayout ( this , BoxLayout . Y_AXIS ) ) ;
18 Stream . of ( chkboxTitle ) . map ( JCheckBox :: new ) . forEach ( this :: add ) ;
19 }
20 }
21 //makePanel で作るのでは無くコンストラクタパラメータで貰う
22 class Accordion extends JPanel {
23 Accordion ( String iTitle , JPanel panel ) {
24 JLabel label = new JLabel ( iTitle ) ;
25 label . addMouseListener ( /* 省略 */ ) ;
26
27 add ( label , BorderLayout . NORTH ) ;
28 add ( panel , BorderLayout . CENTER ) ;
29 }
30 }
リンク先の GitHub のコードを弄ってみました。
コンテンツクラスを流用ということで CheckPanel を 2 回使っていますが、配列にしてというのは必要か分からなかったのでやっていません。
また、ラベルとコンテンツパネルの組のクラスを土台のパネルに並べるよりも(ラベルとコンテンツの組は複数有るのが通常でしょうから)土台に直接ラベルとパネルを並べたほうが階層が減って良いと思います。
ついでに、タイトルにはラベルで無くチェックボックスを使えば操作はマウス以外でも出来るようになります(チェック部分の画像は差し替えられます)し、 BoxLayout で無く GridBagLayout なら大きさの調整の為にメソッドをオーバーライドする必要もありません。
java
1 import java . awt . * ;
2
3 import javax . swing . * ;
4 import javax . swing . event . AncestorEvent ;
5 import javax . swing . event . AncestorListener ;
6
7 public class MainFrame extends JFrame {
8 public static void main ( String [ ] args ) {
9 SwingUtilities . invokeLater ( ( ) -> new MainFrame ( ) . setVisible ( true ) ) ;
10 }
11
12 MainFrame ( ) {
13 super ( "ExpandableContentsPanel" ) ;
14 setDefaultCloseOperation ( WindowConstants . EXIT_ON_CLOSE ) ;
15
16 add ( new JScrollPane ( new ExpandableContentsPanel ( ) ) ) ;
17
18 setSize ( 320 , 400 ) ;
19 //pack();
20 setLocationRelativeTo ( null ) ;
21 }
22 }
23
24 class ExpandableContentsPanel extends JPanel {
25 private static final AncestorListener visibleToScroll = new AncestorListener ( ) {
26 @Override
27 public void ancestorRemoved ( AncestorEvent event ) { /*no process*/ }
28 @Override
29 public void ancestorMoved ( AncestorEvent event ) { /*no process*/ }
30 @Override
31 public void ancestorAdded ( AncestorEvent event ) { scrollRectToVisible ( event . getComponent ( ) ) ; }
32 } ;
33
34 private static void scrollRectToVisible ( JComponent c ) {
35 ( ( JComponent ) c . getParent ( ) ) . scrollRectToVisible ( c . getBounds ( ) ) ;
36 }
37
38 ExpandableContentsPanel ( ) {
39 super ( new GridBagLayout ( ) ) ;
40 setBackground ( new Color ( 180 , 180 , 255 ) ) ;
41 setBorder ( BorderFactory . createEmptyBorder ( 10 , 5 , 10 , 5 ) ) ;
42
43 GridBagConstraints gbc = new GridBagConstraints ( ) ;
44 gbc . gridx = 0 ;
45 gbc . fill = GridBagConstraints . HORIZONTAL ;
46 gbc . weightx = 1 ;
47 add ( "System Tasks" , new CheckPanel ( "1111" , "222222" ) , gbc ) ;
48 add ( "User Tasks" , new CheckPanel ( "33333" , "444" , "55" ) , gbc ) ;
49 add ( "Other Places" , new LabelPanel ( "Desktop" , "My Network Places" , "My Documents" , "Shared Documents" ) , gbc ) ;
50 add ( "Details" , new RadioPanel ( "aaa" , "bbb" , "ccc" , "ddd" ) , gbc ) ;
51
52 gbc . weighty = 1 ;
53 add ( Box . createVerticalStrut ( 0 ) , gbc ) ;
54
55 KeyboardFocusManager . getCurrentKeyboardFocusManager ( ) . addPropertyChangeListener ( "focusOwner" , e -> {
56 if ( ! ( e . getNewValue ( ) instanceof JComponent ) ) return ;
57 JComponent focused = ( JComponent ) e . getNewValue ( ) ;
58 if ( this . isAncestorOf ( focused ) ) scrollRectToVisible ( focused ) ;
59 } ) ;
60 }
61
62 private void add ( String titleText , JComponent contents , GridBagConstraints gbc ) {
63 Title title = new Title ( titleText ) ;
64 title . addItemListener ( e -> contents . setVisible ( title . isSelected ( ) ) ) ;
65 contents . setVisible ( title . isSelected ( ) ) ;
66 contents . addAncestorListener ( visibleToScroll ) ;
67
68 if ( getComponentCount ( ) > 0 ) add ( Box . createVerticalStrut ( 5 ) , gbc ) ;
69 add ( title , gbc ) ;
70 add ( contents , gbc ) ;
71 }
72 }
73
74 class Title extends JCheckBox {
75 private static Icon icon = new ExpandedStateIcon ( ) ;
76
77 Title ( String text ) {
78 super ( text , icon ) ;
79 setOpaque ( false ) ;
80 setForeground ( Color . BLUE ) ;
81 setBorder ( BorderFactory . createEmptyBorder ( 2 , 5 , 2 , 2 ) ) ;
82 }
83
84 @Override
85 protected void paintComponent ( Graphics g ) {
86 Graphics2D g2 = ( Graphics2D ) g ;
87 g2 . setPaint ( new GradientPaint ( 50 , 0 , Color . WHITE , getWidth ( ) , getHeight ( ) , new Color ( 200 , 200 , 255 ) ) ) ;
88 g2 . fillRect ( 0 , 0 , getWidth ( ) , getHeight ( ) ) ;
89 super . paintComponent ( g ) ;
90 }
91 }
92
93 class ExpandedStateIcon implements Icon {
94 private final int iconWidth , iconHeight ;
95 private final int dx , dy ; //位置補正
96 private final Polygon selectedPolygon , polygon ;
97
98 ExpandedStateIcon ( ) {
99 Icon orgIcon = UIManager . getIcon ( "CheckBox.icon" ) ;
100 iconWidth = orgIcon . getIconWidth ( ) ;
101 iconHeight = orgIcon . getIconHeight ( ) ;
102 int pw = ( int ) ( iconWidth * 0.8 ) ; //大きさはテキトウ
103 int ph = ( int ) ( iconHeight * 0.6 ) ; // 〃
104 dx = ( iconWidth - pw ) / 2 ;
105 dy = ( iconHeight - ph ) / 2 ;
106 selectedPolygon = new Polygon ( new int [ ] { pw / 2 , 0 , pw } , new int [ ] { 0 , ph , ph } , 3 ) ; //上向き三角
107 polygon = new Polygon ( new int [ ] { 0 , pw , pw / 2 } , new int [ ] { 0 , 0 , ph } , 3 ) ; //下向き三角
108 }
109
110 @Override
111 public void paintIcon ( Component c , Graphics g , int x , int y ) {
112 Graphics2D g2 = ( Graphics2D ) g . create ( ) ;
113 g2 . translate ( x + dx , y + dy ) ;
114 g2 . setColor ( c . isEnabled ( ) ? c . getForeground ( ) : Color . GRAY ) ;
115 g2 . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_ON ) ;
116 if ( c instanceof AbstractButton && ( ( AbstractButton ) c ) . isSelected ( ) ) {
117 g2 . draw ( selectedPolygon ) ;
118 } else {
119 g2 . fill ( polygon ) ;
120 }
121 g2 . dispose ( ) ;
122 }
123 @Override
124 public int getIconWidth ( ) { return iconWidth ; }
125 @Override
126 public int getIconHeight ( ) { return iconHeight ; }
127 }
128
129 class Contents extends JPanel {
130 Contents ( LayoutManager layout ) {
131 super ( layout ) ;
132 setBackground ( new Color ( 240 , 240 , 255 ) ) ;
133 setBorder ( BorderFactory . createCompoundBorder (
134 BorderFactory . createMatteBorder ( 0 , 2 , 2 , 2 , Color . WHITE ) ,
135 BorderFactory . createEmptyBorder ( 10 , 10 , 10 , 10 ) ) ) ;
136 }
137 }
138
139 class CheckPanel extends Contents {
140 CheckPanel ( String . . . labels ) {
141 super ( new GridLayout ( 0 , 1 ) ) ;
142
143 for ( String s : labels ) {
144 JCheckBox b = new JCheckBox ( s ) ;
145 b . setOpaque ( false ) ;
146 add ( b ) ;
147 }
148 }
149 }
150
151 class LabelPanel extends Contents {
152 LabelPanel ( String . . . labels ) {
153 super ( new GridLayout ( 0 , 1 ) ) ;
154
155 for ( String s : labels ) {
156 add ( new JLabel ( s ) ) ;
157 }
158 }
159 }
160
161 class RadioPanel extends Contents {
162 RadioPanel ( String . . . labels ) {
163 super ( new GridLayout ( 0 , 1 ) ) ;
164
165 ButtonGroup bg = new ButtonGroup ( ) ;
166 for ( String s : labels ) {
167 JRadioButton b = new JRadioButton ( s ) ;
168 b . setOpaque ( false ) ;
169 b . setSelected ( getComponentCount ( ) == 0 ) ;
170 add ( b ) ;
171 bg . add ( b ) ;
172 }
173 }
174 }