简体   繁体   中英

Can i use a String as variable in java

I have two arrays, with Filenames and JTextFields. If files exists, i would like to fill the path in the JTextField. Is there a way to this with a for loop ? I also tried it with a Hashmap, but this isn't working.

String[] Dateiliste = {
                    "A.txt",
                    "B.txt",
                    "C.txt",
                    "D.txt",
                    "E.txt",
                    "F.txt"
            };

String[]textliste ={
                    "text1",
                    "text2",
                    "text3",
                    "text4",
                    "text5",
                    "text6",
            };

Map<Integer, String> Dateilistestreda = new HashMap<Integer,String>();
Dateilistestreda.put(0,"text1");
Dateilistestreda.put(1,"text2");
Dateilistestreda.put(2,"text3");
Dateilistestreda.put(3,"text4");
Dateilistestreda.put(4,"text5");
Dateilistestreda.put(5,"text6");

    for (int i = 0; i < Dateiliste.length; i++){
        File f = new File (path + "\\" +Dateiliste[i]);
        System.out.println(f);
        if (f.exists() && !f.isDirectory()){
            Dateilistestreda.get(i).setText(f.toString());
        }
    }

Since you have control of what list to choose, why don't you just create a class that holds both, the path + JTextField...

For example:

import org.junit.Test;
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class testclass {

    @Test
    public void test() {
        String path = "C:/";
        // Let's assume these are the real JTextFields you have:
        final JTextField text1 = new JTextField();
        final JTextField text2 = new JTextField();
        final JTextField text3 = new JTextField();
        final JTextField text4 = new JTextField();
        final JTextField text5 = new JTextField();
        final JTextField text6 = new JTextField();

        // Here you just populates the List with the file names:
        List<FilePath> files = new ArrayList<FilePath>() {{
            add(new FilePath(text1, "A.txt"));
            add(new FilePath(text2, "B.txt"));
            add(new FilePath(text3, "C.txt"));
            add(new FilePath(text4, "D.txt"));
            add(new FilePath(text5, "E.txt"));
            add(new FilePath(text6, "F.txt"));
        }};

        // And finally you just go one by one checking if it exists and updates the JTextField Text.
        for (FilePath current : files) {
            File file = new File(String.format("%s/%s", path, current.getName()));
            if (file.exists() && !file.isDirectory()) {
                current.getTextField().setText(current.getName());
            }
            System.out.println(current.getTextField().getText());
        }
    }

    // This is just a simple class to hold both structures... 
    public class FilePath {
        private JTextField textField;
        private String name;

        public FilePath(JTextField textField, String path) {
            this.textField = textField;
            this.name = path;
        }

        public JTextField getTextField() {
            return textField;
        }

        public String getName() {
            return name;
        }
    }
}

Hope it helps...

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