2

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.

Anirudh Lou
  • 585
  • 6
  • 20
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Sep 13 '17 at 13:02

1 Answers1

4

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 );
camickr
  • 308,339
  • 18
  • 152
  • 272
  • Thanks sir for your help. I was able to get info, my remaining problem now is only the last data of my list is being created. How to add all my data from index 0 to index 9? As you see on my screenshot only the last data was inserted. What i want is that as i loop to my list i will create its own panel to look like a GridView then put all the panels on JScrollPane. – Anirudh Lou Sep 13 '17 at 15:18
  • 2
    @AnirudhLou, well what do you expect to happen when you only add the "beesCell" panel "after" you finish the loop?. You need to set the layout manager of the panel before you start the loop. Then inside the loop after you create each "beesCell" panel, you add it to the panel using the GridlLayout. – camickr Sep 13 '17 at 15:40
  • Thanks a lot sir for your help. My code works as you suggested, only a minor that is it always show horizontal scrollbar, is it possible to have a vertical scrollbar? – Anirudh Lou Sep 14 '17 at 10:04
  • Use an appropriate layout manager. The GridLayout should work if you define it properly. Define the row to be 0 and the columns to be 4 and the panels will wrap after every 4 components you add to the layout. – camickr Sep 14 '17 at 15:01
  • Thanks sir. I will do as you suggested. I set up my **beesCell**'s preferredSize(100,100) and it works. Again thank you very much sir for your help. – Anirudh Lou Sep 14 '17 at 18:51
  • Nice to see these type of conversations ;) – chankruze Dec 13 '19 at 05:12