how to reuse the area in a container when any component is removed from it?

Posted by vybhav on Stack Overflow See other posts from Stack Overflow or by vybhav
Published on 2010-04-01T17:55:13Z Indexed on 2010/04/01 18:03 UTC
Read the original article Hit count: 393

Filed under:
|

hi! i hav a panel and i m trying to remove labels from it which were added to it during run-time. but when labels are removed succesfully i m not able to use the space,left by that label,again to add any label to it.

thanks in anticipation of the solution.

here is the relevant code snippet:

  1. to add label to the panel:

         JLabel jl = new JLabel();
     jl.setOpaque(true);
     jl.setIcon(new ImageIcon("D:/Project/router2.jpg"));
     jl.setBounds(x, y,jl.getPreferredSize().width,jl.getPreferredSize().height);
     for(Component c :lcomponent)
       {       flag=true;
               Rectangle r4=c.getBounds();
               int x1=(int) r4.getX();
               int y1=(int) r4.getY();
               Rectangle r5 = new Rectangle(new Point(x1-60, y1-60),new Dimension(170,170));
            if(r5.contains(p)){            //To ensure that two labels do not overlap or are too close
                flag = false;             // to each other
                break;                    
            }}
            if(flag)
            {
            p2.add(jl);   //p2 is a panel
           Component c2 = p2.getComponentAt(x,y);
            p2.repaint();      
            lcomponent.add(c2);  //lcomponent is an ArrayList<Component> to store all the labels added to the panel
    
    
    
        }
    

    2.to remove the label:

    p2.remove(); p2.repaint();

© Stack Overflow or respective owner

Related posts about java

Related posts about swing