简体   繁体   中英

Create new instance of JLabel and actionlistener in loop

I am trying to create an application launcher for my class project.

I am trying to implement a function that will take each line from a text file and put it to a JLabel and add an ActionListener each time the loop runs. At the end of the loop it did display all the JLabels but whenever I clicked on any it would open whatever program was last in a file.

My question is how can I make it to where I can click a program name and open the correct file?

Here is the code I did :

public static void list()
{
    String file = "files/programList.txt";

    //final Scanner read = new Scanner(file);
    String filePath;
    String content;
    String line = "";
    content = "<html>";
    int count = 0, i = 0, y = 0;
    int x = 0;
    lines = 0;
    try
    {
        FileReader read = new FileReader(file);
        LineNumberReader lRead = new LineNumberReader(
                          new FileReader(new File("files/programList.txt")));
        lRead.skip(Long.MAX_VALUE);
        BufferedReader bRead = new BufferedReader(read);
        int numbOfLines = lRead.getLineNumber() + 1;
        System.out.println(numbOfLines);
        fixedLine = new ArrayList<String>(lRead.getLineNumber());
        while((line = bRead.readLine()) != null)
        {
            int index = -1;

            if(lines != numbOfLines)
            {
                if(lines == 0)
                {
                    fixedLine.add(line);
                    lines = 1;
                }
                else
                {
                    fixedLine.set(lines++, line);
                    //fixedLine = line;
                    //pLine = new JLabel(fixedLine);
                    //pLine.setFont(new Font("Verdana", 1, 20));
                    //pLine.setForeground(Color.WHITE);
                    //pLine.addMouseListener(new MouseAdapter()
                    //{
                    // public void mouseClicked(MouseEvent e)
                    //{
                    //  fileList.run(pLine.getText());
                    // System.out.println(pLine.getText());
                    //}
                    //});
                    //panel.add(pLine);
                    //frame.repaint();
                    //frame.revalidate();
                    content = content + line + "<br>";
                    System.out.println(lines);

                    System.out.println(fixedLine);
                }
            }
            else
            {
                lines++;
            }
        }
        bRead.close();
        lRead.close();
        content = content + "</html>";
        System.out.println(fixedLine.get(0));

        System.out.println(numbOfLines);//maybe use array or arraylist
    }
    catch(IOException e)
    {
        System.err.println(e.getMessage());
    }

}

I was experimenting with an string arraylist and only got it to add one program before it gives me an error.

What can I do to get it working?

After I get the program working I will simplify my code and improve it as I know my coding isn't the best

So you want to execute something when you click a JLabel, something read from a file. I think you do strange things to read the lines. Why so many lists? You could do something like

Files.lines(Paths.get("files", "programList.txt")).forEach(
            f -> {
                JLabel label = new JLabel(f);
                label.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        executeopeningMagicWithTheFile(f);
                    }
                });
                panel.add(label);
            }
    );

I hope this helps, it's rather unclear what exactly you want since your code contains code in comment and variables that are not defined

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