Anirudh Lou
Anirudh Lou

Reputation: 841

How to add JPanel Dynamically and making it clickable?

I am trying to create a basic gui program that aims to:

  1. Dynamically inserts JPanel (the number of JPanel will be created is base on the size of my List) that is scrollable.
  2. Get the info from the JPanel whenever clicked.

So far this is what i did:

...

public class BeesFrame extends javax.swing.JFrame {
    List<String> bees = new ArrayList<>(Arrays.asList("Bee 1", "Bee 2", "Bee 3",
                                                    "Bee 4", "Bee 5", "Bee 6",
                                                    "Bee 7", "Bee 8", "Bee 9",
                                                    "Bee 10", "Bee 11", "Bee 12",
                                                    "Bee 13"));
    GridBagLayout layout = new GridBagLayout();

    JScrollPane scrollpane;
    JPanel beesPanel;
    JPanel beesCell;
    JLabel label;

    public BeesFrame() {
        initComponents();
        label = new JLabel();
        for(int i = 0; i < bees.size(); i++){
            beesCell = new JPanel();
            beesCell.setName(bees.get(i));
            beesCell.setPreferredSize(new Dimension(100, 100));
            beesCell.setMinimumSize(new Dimension(100, 100));
            beesCell.setBackground(Color.yellow);

            label.setHorizontalTextPosition(SwingConstants.CENTER);
            label.setText(beesCell.getName());

            beesCell.add(label);
            beesCell.validate();
            beesCell.repaint();
            System.out.println(bees.get(i));
        }

        beesMainPanel.setLayout(new GridLayout((bees.size()/4)+1, 4, 1, 1));
        beesMainPanel.add(beesCell);
        beesCell.setVisible(true);
        beesCell.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                JPanel panel = (JPanel) getComponentAt(e.getPoint());
                panel.setName(label.getText());
                outPut(panel);
            }
        });

        beesCell.validate();
        beesCell.repaint();

    }

    void outPut(JPanel panel){
        System.out.println("Panel...."+panel.getName());
    }

... // some other code generated by Netbeans


}

But instead of displaying it correctly. Only the last from my list is being inserted and if i clicked it, its says javax.swing.JRootPane cannot be cast to javax.swing.JPanel. This is the error occurs:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JRootPane cannot be cast to javax.swing.JPanel
    at catchingbees.frame.BeesFrame$1.mousePressed(BeesFrame.java:79)
    at java.awt.Component.processMouseEvent(Component.java:6530)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    ...

Here is the screen shot of my output:

enter image description here

But this is what i intended to do:

enter image description here

Any help is very much appreciated.

Upvotes: 2

Views: 2403

Answers (1)

camickr
camickr

Reputation: 324157

Add the MouseListener to each panel, then you just use the getSource() method of the MouseEvent to access the panel that was clicked.

MouseListener ml = new MouseAdapter()
{
    @Override
    public void mousePressed(MouseEvent e)
    {
        JPanel panel = (JPanel)e.getSource();

        // do your processing on the panel
    }
}

Then in the loop that creates the panels you simply do:

panel.addMouseListener( ml );

Upvotes: 4

Related Questions