Hi,

I got this piece of code from this forum and i was trying to modify it so that I can make it dynamic. I modified it a little bit to add multiple collapsible panels. But I need some help.

1. I need to add these panels dynamically. If I click on a button it will add the panels one at a time and make a sort of queue layout(this queue layout is a problem too).

2. At the queue layout whenever I click a panel, it should move to the bottom of the list and then expand(as opposed to expanding in its own place).

For some reason I cant wrap my head around it. Please help :)

package desktopapplication2;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;


public class Test {
	public static void main(String[] args) {

		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Container contentPane = f.getContentPane();
                //contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
                //contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
                // Define the layout for the subpanels
                GridBagLayout layout = new GridBagLayout();
                contentPane.setLayout( layout );
                GridBagConstraints gbc = new GridBagConstraints();
                //gbc.insets = new Insets(10, 0, 5, 0);
                gbc.weightx = 1.0;
		gbc.fill = gbc.HORIZONTAL;
		gbc.gridwidth = gbc.REMAINDER;

                CollapsablePanel cp1 = new CollapsablePanel("test1", buildPanel());
                gbc.insets = new Insets(10, 0, 5, 0);
                contentPane.add(cp1,gbc);

                CollapsablePanel cp2 = new CollapsablePanel("test2", buildPanel());
                gbc.insets = new Insets( 5, 0, 5, 0 );
		contentPane.add(cp2,gbc);

                CollapsablePanel cp3 = new CollapsablePanel("test3", buildPanel());
		contentPane.add(cp3,gbc);

                f.setSize(360, 500);
		f.setLocation(200, 100);
		f.setVisible(true);



	}



	public static JPanel buildPanel() {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(2, 1, 2, 1);
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;

		JPanel p1 = new JPanel(new GridBagLayout());
		gbc.gridwidth = gbc.RELATIVE;
		p1.add(new JButton("button 1"), gbc);
		gbc.gridwidth = gbc.REMAINDER;
		p1.add(new JButton("button 2"), gbc);
		gbc.gridwidth = gbc.RELATIVE;
		p1.add(new JButton("button 3"), gbc);
		gbc.gridwidth = gbc.REMAINDER;
		p1.add(new JButton("button 4"), gbc);

		return p1;
	}
}

class CollapsablePanel extends JPanel {

	private boolean selected;
	JPanel contentPanel_;
	HeaderPanel headerPanel_;

	private class HeaderPanel extends JPanel implements MouseListener {
		String text_;
		Font font;
		BufferedImage open, closed;
		final int OFFSET = 30, PAD = 5;

		public HeaderPanel(String text) {
			addMouseListener(this);
			text_ = text;
			font = new Font("sans-serif", Font.PLAIN, 12);
			// setRequestFocusEnabled(true);
			setPreferredSize(new Dimension(200, 20));
			int w = getWidth();
			int h = getHeight();

			/*try {
				open = ImageIO.read(new File("images/arrow_down_mini.png"));
				closed = ImageIO.read(new File("images/arrow_right_mini.png"));
			} catch (IOException e) {
				e.printStackTrace();
			}*/

		}

		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			int h = getHeight();
			/*if (selected)
				g2.drawImage(open, PAD, 0, h, h, this);
			else
				g2.drawImage(closed, PAD, 0, h, h, this);
                        */ // Uncomment once you have your own images
			g2.setFont(font);
			FontRenderContext frc = g2.getFontRenderContext();
			LineMetrics lm = font.getLineMetrics(text_, frc);
			float height = lm.getAscent() + lm.getDescent();
			float x = OFFSET;
			float y = (h + height) / 2 - lm.getDescent();
			g2.drawString(text_, x, y);
		}

		public void mouseClicked(MouseEvent e) {
			toggleSelection();
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}

	}

	public CollapsablePanel(String text, JPanel panel) {
		super(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(1, 3, 0, 3);
		gbc.weightx = 1.0;
		gbc.fill = gbc.HORIZONTAL;
		gbc.gridwidth = gbc.REMAINDER;

		selected = false;
		headerPanel_ = new HeaderPanel(text);

		setBackground(new Color(200, 200, 220));
		contentPanel_ = panel;

		add(headerPanel_, gbc);
		add(contentPanel_, gbc);
		contentPanel_.setVisible(false);

		JLabel padding = new JLabel();
		gbc.weighty = 1.0;
		add(padding, gbc);

	}

	public void toggleSelection() {
		selected = !selected;

		if (contentPanel_.isShowing())
			contentPanel_.setVisible(false);
		else
			contentPanel_.setVisible(true);

		validate();

		headerPanel_.repaint();
	}

}

OK I have added this code after the buildpanel() so that it till create a new collapsible panel. What am I doing wrong and how do I show it in the main[]

public static JPanel thumbnail() {

        //addMouseListener(this);
        JPanel t = new JPanel();
        JButton jb = new JButton("Thumb");


        jb.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                buildPanel();
            }
        });


        t.add(jb);



        return t;
    }

OK I am taking the original post out and substituting it with this simpler problem. If you look at the code, you will see in the buildMainPanel() all I am trying to do is to create a new panel in the main container. But obciously there is this scope issue how can I get rid of this problem?

package desktopapplication2;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Test {

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Container contentPane = f.getContentPane();

        GridBagLayout layout = new GridBagLayout();
        contentPane.setLayout(layout);
        final GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 0, 5, 0);
        gbc.weightx = 1.0;
        gbc.fill = gbc.HORIZONTAL;
        gbc.gridwidth = gbc.REMAINDER;

        CollapsablePanel cp1 = new CollapsablePanel("Main Queue", buildMainPanel());
        //gbc.insets = new Insets(10, 0, 5, 0);
        contentPane.add(cp1, gbc);

        JButton j = new JButton("IOI");

        j.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                CollapsablePanel cp4 = new CollapsablePanel("test4", buildPanel());
                contentPane.add(cp4, gbc);
                contentPane.validate();
                contentPane.repaint();
            }
        });
        contentPane.add(j, gbc);


        f.setSize(360, 500);
        f.setLocation(200, 100);
        f.setVisible(true);



    }
    // Building the Main Panel

    public static JPanel buildMainPanel() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        JPanel p1 = new JPanel(new GridBagLayout());

        JButton bt1 = new JButton(new ImageIcon("/Users/sha33/NetBeansProjects/DesktopApplication2/src/desktopapplication2/r1.jpg"));
        p1.add(bt1, gbc);
        bt1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                CollapsablePanel cp4 = new CollapsablePanel("test4", buildPanel());
                contentPane.add(cp4, gbc);
                contentPane.validate();
                contentPane.repaint();
            }
        });


        JButton bt2 = new JButton(new ImageIcon("/Users/sha33/NetBeansProjects/DesktopApplication2/src/desktopapplication2/r2.jpg"));
        p1.add(bt2, gbc);

        JButton bt3 = new JButton(new ImageIcon("/Users/sha33/NetBeansProjects/DesktopApplication2/src/desktopapplication2/r3.jpg"));
        p1.add(bt3, gbc);


        return p1;
    }

    public static JPanel buildPanel() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 1, 2, 1);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        JPanel p1 = new JPanel(new GridBagLayout());
        gbc.gridwidth = gbc.RELATIVE;
        p1.add(new JButton("button 1"), gbc);
        gbc.gridwidth = gbc.REMAINDER;
        p1.add(new JButton("button 2"), gbc);
        gbc.gridwidth = gbc.RELATIVE;
        p1.add(new JButton("button 3"), gbc);
        gbc.gridwidth = gbc.REMAINDER;
        p1.add(new JButton("button 4"), gbc);
        ;

        return p1;
    }
}

class CollapsablePanel extends JPanel {

    private boolean selected;
    JPanel contentPanel_;
    HeaderPanel headerPanel_;

    private class HeaderPanel extends JPanel implements MouseListener {

        String text_;
        Font font;
        BufferedImage open, closed;
        final int OFFSET = 30, PAD = 5;

        public HeaderPanel(String text) {
            addMouseListener(this);
            text_ = text;
            font = new Font("sans-serif", Font.PLAIN, 12);
            // setRequestFocusEnabled(true);
            setPreferredSize(new Dimension(200, 20));
            int w = getWidth();
            int h = getHeight();

            /*try {
            open = ImageIO.read(new File("images/arrow_down_mini.png"));
            closed = ImageIO.read(new File("images/arrow_right_mini.png"));
            } catch (IOException e) {
            e.printStackTrace();
            }*/

        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            int h = getHeight();
            /*if (selected)
            g2.drawImage(open, PAD, 0, h, h, this);
            else
            g2.drawImage(closed, PAD, 0, h, h, this);
             */ // Uncomment once you have your own images
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics(text_, frc);
            float height = lm.getAscent() + lm.getDescent();
            float x = OFFSET;
            float y = (h + height) / 2 - lm.getDescent();
            g2.drawString(text_, x, y);
        }

        public void mouseClicked(MouseEvent e) {
            toggleSelection();
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }
    }

    public CollapsablePanel(String text, JPanel panel) {
        super(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(1, 3, 0, 3);
        gbc.weightx = 1.0;
        gbc.fill = gbc.HORIZONTAL;
        gbc.gridwidth = gbc.REMAINDER;

        selected = false;
        headerPanel_ = new HeaderPanel(text);

        setBackground(new Color(200, 200, 220));
        contentPanel_ = panel;

        add(headerPanel_, gbc);
        add(contentPanel_, gbc);
        contentPanel_.setVisible(false);

        JLabel padding = new JLabel();
        gbc.weighty = 1.0;
        add(padding, gbc);

    }

    public void toggleSelection() {
        selected = !selected;

        if (contentPanel_.isShowing()) {
            contentPanel_.setVisible(false);
        } else {
            contentPanel_.setVisible(true);
        }

        validate();

        headerPanel_.repaint();
    }
}

But contentPane is no known inside the buildMainPanel class as it is declared in the main method and the gbc must be declared final. so the code does not compile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.