简体   繁体   中英

How to add files to JList?

I am creating my GUI in NetBeans and I would need a help with JList. Every time I open some TXT file via JFileChooser I would like to add this file to the JList. How can I do it?


Thank you very much for your answers, but it doesn't work, I don't know why :(. However I wil ltry to explain my problem deeper, maybe it will help.

Here is the screenshot of my draft application -

在此处输入图片说明

What I want is to open TXT file a add it to the JList Layers (see on the picture). By this way I would like to add as many layers as the user wants. Then clicking on each Layer in the List I would like to visualize this layer in the panel Layer overview (JLabel). So my problem is - HOW TO STORE all the layers in the JList and then HOW TO ACCESS particular selected layer in order to visualize it.

I know it is a bit weird from me to formulate it in this way but I will appreciate any help.

JFileChooser chooser = new JFileChooser();

int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {

DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
int pos = list.getModel().getSize();
model.add(pos, chooser.getSelectedFile());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM