I have a strange problem…
I have a JFrame and added two JInternalFrame
I have added a JPanel to one of the internal frames.
I used a mouse listener to the jpanel and wrote a code ‘like placing a device object(a class extendng jpanel ) when i release the mouse.
The device image does display, but only after i move d internal frame or maximize it.. why is tat?
The following code is the JPanel which is added to one of the Internal Frames…
public class Board extends JPanel implements MouseListener{
Device[] devices=new Device[10];
int X,Y,i=0;
int j=10;
Point p1,p2;
ImageIcon icon;
public Board()
{
setBackground(Color.WHITE);
this.addMouseListener(this);
setLayout(null);
}
public void mouseClicked(MouseEvent me){
}
public void mouseExited(MouseEvent me){
}
public void mouseEntered(MouseEvent me){
}
public void mousePressed(MouseEvent me){
}
public void mouseReleased(MouseEvent me){
X=me.getX();
Y=me.getY();
icon=new ImageIcon("E:\\java\\ecadpb\\src\\ecadpb\\device"+Layout1.clicked+".png");
devices[i].setBounds(X,Y,icon.getIconWidth(),icon.getIconHeight()+20);
devices[i].setVisible(true);
this.add(devices[i]);
this.repaint(X,Y,icon.getIconWidth(),icon.getIconHeight()+20);
i++;
}
}
}
The following is the Device class..
public Device(Point pos,JLabel label,Rectangle r,int input,int output){
setVisible(true);
setBackground(Color.BLACK);
deviceNo=dno;
position=pos;
inputs=input;
outputs=output;
this.r=r;
label.setLayout(null);
setOpaque(false);
this.add(label);
label.addMouseListener(this);
dno++;
}
}
Swing has to lay out the new component hierarchy. Take a look at JComponent.revalidate() and its related methods.