在 Java 中通过其中的按钮刷新自定义容器
Refresh custom container through a button inside it in Java
早上好,
我有一个继承自 java.awt.Container 的 class。他的范围是包装一个文件列表,将它们垂直显示为 "file name Labels + delete Buttons" 列表。
文件正确显示,每次添加新文件时,它都会正确刷新。
组件class的代码:
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;
}
}
该按钮的范围是从文件列表中删除由他自己的绝对路径标识的特定文件。它有效,但我不知道如何通过主界面刷新他的UI。
上面的代码是在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();
...
每次按下 "deleteFile" 按钮时,我都必须刷新该容器。我该怎么做?
谢谢,问候
安德里亚
终于根据安德鲁的建议找到了解决方案。
这是我的 class:
public class AttachmentsList extends JPanel
{
private List<File> attachments = null;
private JTable jt = null;
private DefaultTableModel model = null;
private JScrollPane scroller = null;
public AttachmentsList(List<File> attachments)
{
super();
this.attachments = attachments;
setLayout(new GridLayout());
Vector<Vector> rowData = new Vector<Vector>();
for (File iterable_element : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(iterable_element.getName());
row.addElement(new JCheckBox());
rowData.add(row);
}
Vector<String> columnNames = new Vector<String>();
columnNames.add("File");
columnNames.add("Selected");
this.jt = new JTable(rowData, columnNames);
this.jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.model = (DefaultTableModel) this.jt.getModel();
final TableColumnModel columnModel = this.jt.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(100);
columnModel.getColumn(1).setPreferredWidth(60);
this.scroller = new JScrollPane(this.jt);
add(this.scroller);
}
@Override
public void repaint()
{
buildTable();
}
protected void buildTable()
{
if (this.attachments != null)
{
Vector<Vector> rowData = new Vector<Vector>();
for (File currentFile : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(currentFile.getName());
row.addElement(false);
rowData.add(row);
}
this.model.addRow(rowData);
}
}
public void addFile(File file)
{
Vector<Object> row = new Vector<Object>();
row.addElement(file.getName());
row.addElement(false);
this.model.addRow(row);
}
public List<File> getAttachments()
{
return attachments;
}
public void setAttachments(List<File> attachments)
{
this.attachments = attachments;
}
}
这是一个工作的原始组件,显示存储在 List 中的文件。
早上好,
我有一个继承自 java.awt.Container 的 class。他的范围是包装一个文件列表,将它们垂直显示为 "file name Labels + delete Buttons" 列表。
文件正确显示,每次添加新文件时,它都会正确刷新。
组件class的代码:
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;
}
}
该按钮的范围是从文件列表中删除由他自己的绝对路径标识的特定文件。它有效,但我不知道如何通过主界面刷新他的UI。
上面的代码是在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();
...
每次按下 "deleteFile" 按钮时,我都必须刷新该容器。我该怎么做?
谢谢,问候
安德里亚
终于根据安德鲁的建议找到了解决方案。
这是我的 class:
public class AttachmentsList extends JPanel
{
private List<File> attachments = null;
private JTable jt = null;
private DefaultTableModel model = null;
private JScrollPane scroller = null;
public AttachmentsList(List<File> attachments)
{
super();
this.attachments = attachments;
setLayout(new GridLayout());
Vector<Vector> rowData = new Vector<Vector>();
for (File iterable_element : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(iterable_element.getName());
row.addElement(new JCheckBox());
rowData.add(row);
}
Vector<String> columnNames = new Vector<String>();
columnNames.add("File");
columnNames.add("Selected");
this.jt = new JTable(rowData, columnNames);
this.jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.model = (DefaultTableModel) this.jt.getModel();
final TableColumnModel columnModel = this.jt.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(100);
columnModel.getColumn(1).setPreferredWidth(60);
this.scroller = new JScrollPane(this.jt);
add(this.scroller);
}
@Override
public void repaint()
{
buildTable();
}
protected void buildTable()
{
if (this.attachments != null)
{
Vector<Vector> rowData = new Vector<Vector>();
for (File currentFile : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(currentFile.getName());
row.addElement(false);
rowData.add(row);
}
this.model.addRow(rowData);
}
}
public void addFile(File file)
{
Vector<Object> row = new Vector<Object>();
row.addElement(file.getName());
row.addElement(false);
this.model.addRow(row);
}
public List<File> getAttachments()
{
return attachments;
}
public void setAttachments(List<File> attachments)
{
this.attachments = attachments;
}
}
这是一个工作的原始组件,显示存储在 List 中的文件。