-1

i have my JPanel:

 private int status = 0;
public serietv() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 848, 566);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


JPanel labelpanel = new JPanel();  //labelpanel is my problem
    labelpanel.setBounds(92, 329, 625, 128);
    contentPane.add(labelpanel);
    labelpanel.setLayout(null);

   //here my label go transparent
    Color c=new Color(200,0,0,20);
    labelpanel.setBackground(c);
    labelpanel.setOpaque(true);


  JLabel[] labelapp = new JLabel[1000]; //create an arrayJLabel for my    images

with this code my panel becomes transparent with red transparent background like this: when i DONT click "Succ"

but when i click on JLabel "succ":

  JLabel succ = new JLabel("");
    succ.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    succ.setBounds(727, 350, 82, 79);
    contentPane.add(succ);

succ.addMouseListener(new MouseAdapter() {
 @Override
        public void mousePressed(MouseEvent f) {

             int x = 0;

   int i = 0;
    labelpanel.removeAll();

            try {

                for (i = status; i < status + 5; i++) {

                    RidimIcon locand = new RidimIcon();
                    labelapp[i].setBounds(30 + x, 15, 90, 100);
                    Border border = BorderFactory.createLineBorder(Color.BLACK, 3);
                    labelapp[i].setBorder(border);
                    labelapp[i].setIcon(locand.newicona(pathicon[i],labelapp[i]));
                    labelpanel.add(labelapp[i]);


                    x = x + 120;
                }
                status = i;

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


            } catch (NullPointerException e) {

    labelpanel.revalidate();
                labelpanel.repaint();
                appoggio = i - status;
                status = i - appoggio;
              }
        }
    });

this is my ridimIcon: public class RidimIcon{

ImageIcon image;
Image im;
Image myImg;
ImageIcon newImage;
int i=0;
public ImageIcon newicona (String img, JLabel lb){



    image = new ImageIcon(img);
    im = image.getImage();
    myImg = im.getScaledInstance(lb.getWidth(), lb.getHeight(), Image.SCALE_SMOOTH);
    newImage = new ImageIcon(myImg);


    return newImage;

  }

} my panel returns to normal, not transparent.. like this: when i click succ i need to remove all component but dont remove transparency.. how can i fix it?

  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson May 02 '16 at 13:17
  • .. 3) Instead of removing & adding components, use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 4) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson May 02 '16 at 13:18
  • can u show me an exemple with my code? – Francesco Pio Bocci May 02 '16 at 14:50
  • *"can u show me an exemple with my code?"* I don't know. Where is your MCVE? – Andrew Thompson May 02 '16 at 14:54
  • i change my errore descript..now is MCVE? – Francesco Pio Bocci May 02 '16 at 18:46
  • How can we compile and execute the code to see the described problem? A picture is not code! We are asking for simple code that demonstrates the problem. We don't want to debug you entire application. You say the problem is when you click a label. So create a frame with a label and demonstrate the problem in code we can test. – camickr May 02 '16 at 18:54
  • now i think is complete to test my problem D: – Francesco Pio Bocci May 02 '16 at 19:09
  • 1
    (1-) How is that complete? Where is the code to create the frame? Where is the main() method? We should be able to copy a single class file and compile it. You need to create a simple example (forget about your real code) that demonstrates the problem. – camickr May 02 '16 at 19:25

1 Answers1

2

Swing does not know how to paint transparent backgrounds, so you need to manage the painting of the background yourself.

Check out Backgrounds With Transparency for more information and two solutions:

  1. you customize the painting of your component to paint the background
  2. use a wrapper component to paint the background.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • i paint my component but when i use ` labelpanel.removeAll();` my trasparency go away and i see only green background.. if i dont click on "succ" my labelpanel is transparent – Francesco Pio Bocci May 02 '16 at 18:35
  • 1
    @FrancescoPioBocci, Not sure what your point is. Swing doesn't support transparency. You have been given two solutions. You have also been asked to post a proper SSSCE or MCVE demonstrating the problem if you want more help. – camickr May 02 '16 at 18:37
  • i post 2 screen in my ask – Francesco Pio Bocci May 02 '16 at 18:46