-2

This is my click action:

  succ.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                int x=0;
                int i;
               labelpanel.revalidate();
               labelpanel.repaint();
            for (i=status; i<status+5; i++){

                RidimIcon locand = new RidimIcon();
                labelapp.get(i).setBounds(74+x, 1, 80, 90);

                labelapp.get(i).setIcon(locand.newicona(pathicon[i], labelapp.get(i)));
                labelpanel.add(labelapp.get(i));

                    x=x+120;
               }
                status=status+5; //change status
            }

         });

"Labelpanel" is an array of JLabel:

    try {
        ResultSet rs = Datainter.eseguiQuery(query);
        while(rs.next())
        {

            pathicon[contatore] = rs.getString("locandina");

            JLabel tmplabel = new JLabel();
            labelapp.add(tmplabel);

            labelapp.get(contatore).setIcon(new ImageIcon(pathicon[contatore]));
            contatore++;

        }


    } catch (SQLException e) {
        e.printStackTrace();
    }

i want to create programm that see 5 image at click. I have an array labels with 50 images, when i click first time i see 1 - 5 labels[1-5], when i click second time i see labels[5-10], third time [10-11] etc.. Why i see only first 5 labelapp when i click on "succ"? How can i see other 5 labelapp when i click again and leave first 5 images in panel?

(with RemoveAll i cant see my last Jlabels:

             prec.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int x=0;
                int i;
                    labelpanel.removeAll();
                        for (i=status; i<status-5; i--){

                            RidimIcon locand = new RidimIcon();
                            labelapp.get(i).setBounds(74+x, 1, 80, 90);
                            labelapp.get(i).setIcon(locand.newicona(pathicon[i], labelapp.get(i)));
                            labelpanel.add(labelapp.get(i));

                                x=x+120;

                        }
                        labelpanel.revalidate();
                        labelpanel.repaint();

                        status=status-5;
            } 
        });
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • What is `status` value? Where do you change it? – rdonuk May 01 '16 at 18:37
  • status is `private int status=0;` .. when i click `succ` status=5 (i see first 5 image), when i click again status=10 (i see 5 to 10 i-images).. I change it when close For (`status=status+5;`) – Francesco Pio Bocci May 01 '16 at 18:41
  • Do you want to see 10 labels after double click? Or only the last 5 labels? – rdonuk May 01 '16 at 18:48
  • 1
    Likely you'll have a much easier time if you simply use a JList, one that displays ImageIcons, and not try to use a bunch of JLabels. You certainly don't want to use null layouts and `setBounds` as it appears that you're doing. If you want to get better answers though, please give us a better understanding of your problem by creating and posting a valid [mcve] -- please read the link! – Hovercraft Full Of Eels May 01 '16 at 18:48
  • @ReşitDönük I want to see other 5 Labels when i click with mouse, not ALL labels.. i want to create programm that see 5 image at click. I have an array labels with 50 images, when i click first time i see 1 - 5 labels[1-5], when i click second time i see labels[5-10], third time [10-11] etc.. – Francesco Pio Bocci May 01 '16 at 19:08
  • I use java from 2 weeks, sorry for my ignorance – Francesco Pio Bocci May 01 '16 at 19:09
  • Don't apologize, but instead (and again) consider creating and posting your [mcve]. That would be time much better spent. – Hovercraft Full Of Eels May 01 '16 at 19:22

1 Answers1

1

Use removeAll() to remove all previous labels from the labelPanel. Then add new labels and call revalidate() and repaint().

public void mouseClicked(MouseEvent arg0) {
     int x=0;
     int i;

     labelpanel.removeAll();
     for (i=status; i<status+5; i++){

         RidimIcon locand = new RidimIcon();
         labelapp.get(i).setBounds(74+x, 1, 80, 90);

         labelapp.get(i).setIcon(locand.newicona(pathicon[i], labelapp.get(i)));
         labelpanel.add(labelapp.get(i));

         x=x+120;
      }

      labelpanel.revalidate();
      labelpanel.repaint();

      status=status+5; //change status
      if(status > 17) {
          status = 17;
      }
}

And some advices;

Community
  • 1
  • 1
rdonuk
  • 3,921
  • 21
  • 39