Good morning,
I have a class that inherit from java.awt.Container. His scope is to wrap a list of Files showing them vertically as a "file name Labels + delete Buttons" list.
files are shown properly and every time a new one is added, it refresh correctly.
The component class's code:
public class AttachmentsList extends Container {
private List<File> attachments = null;
public AttachmentsList(List<File> attachments)
{
super();
this.attachments = attachments;
buildListAttachments();
}
@Override
public void repaint()
{
buildListAttachments();
super.repaint();
}
protected void buildListAttachments()
{
int yTraslation = 0;
for (File attachment : this.attachments)
{
Label fileName = new Label(attachment.getName());
fileName.setBounds(10, 10 + yTraslation, 70, 20);
//invisible, just contain the absolute path...
final Label path = new Label(attachment.getAbsolutePath());
fileName.setBounds(10, 10 + yTraslation, 70, 20);
Button deleteFile = new Button("x");
deleteFile.setBounds(90, 10 + yTraslation, 20, 20);
deleteFile.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
File fileToRemove = new File(path.getText());
attachments.remove(fileToRemove);
System.out.println(attachments.size());
repaint();//<---- It doesn't refresh the main UI.
}
});
add(fileName);
add(deleteFile);
yTraslation += 20;
}
}
The button's scope is removing from the files list a specific file identified by his own absolute path. It works, but I can't figure how to refresh his UI through the main interface.
The above code it's called in the main UI class:
...
final List<File> filesAttached = new LinkedList<File>();
...
final AttachmentsList attachmentsList = new AttachmentsList(fileAttached);
attachmentsList.setBounds(10, 80, 120, 200);
...
//inside ActionListener
...
//pathChooser is a JFileChooser object
fileAttached.add(pathChooser.getSelectedFile());
//every time that one file is added i have to refresh that component. It refreshes!
attachmentsList.repaint();
...
Every time "deleteFile" button is pressed I have to refresh that Container. How can I do that?
Thankyou, regards
Andrea