我选择文件路径时无法在面板中更新

时间:2014-01-29 18:24:29

标签: java swing arraylist java-io

一旦用户选择“点击此处上传”按钮,则将3个文件路径存储到文件中,并将这些路径读入到JPanel中显示的arraylist中。问题是,一旦选择了文件,除非在显示最新信息数之前重新启动应用程序,否则它永远不会在JPanel上自动更新。怎么能纠正这个问题,因为我不知道哪里出错了。顺便说一句,arraylists在运行时更新,但JPanel永远不会更新。

public class Media2 extends JPanel {

private JPanel video_pnl, control_pnl;
private JButton play_btn;
private JLabel loc_lbl;
private int increment;
ArrayList<String> file_location;
ArrayList<JButton> button_lists;
private FileWriter file_writer;
private BufferedWriter buffered_writer;
private JFileChooser filechooser;
private File file;
private JButton btn_upload;
private BufferedReader br;
private FileReader fr;

public Media2(ArrayList<String> file_location) throws IOException {

    btn_upload = new JButton("Click here to Upload Video");
    String file_path = "C:\\Users\\goldAnthony\\Documents\\NetBeansProjects\\VDMS\\src\\VideoInfos.txt";
    file = new File(file_path);
    if (!file.exists()) {
        file.createNewFile();
        file_writer = new FileWriter(file.getAbsolutePath());
    } else {
        file_writer = new FileWriter(file.getAbsolutePath(), true);
    }
    add(btn_upload);

    Handlers handler = new Handlers();
    btn_upload.addActionListener(handler);

    this.file_location = file_location;
    readFile(file_location, file_path);
}

private void readFile(ArrayList<String> file_location, String file_path) throws FileNotFoundException, IOException {
    fr = new FileReader(file_path);
    br = new BufferedReader(fr);

    String text = "";
    String line2;
    line2 = br.readLine();
    while (line2 != null) {
        int i = 0;
        file_location.add(i, line2);
        line2 = br.readLine();
        i++;
    }
    configurePanel(file_location);
    //System.out.print(file_location.size() + "in the read file 1");
}

private void configurePanel(ArrayList<String> file_location) {
    increment = 0;
    while (increment < file_location.size()) {

        video_pnl = new JPanel();
        video_pnl.setLayout(new BoxLayout(video_pnl, BoxLayout.Y_AXIS));
        loc_lbl = new JLabel();
        loc_lbl.setText(file_location.get(increment));
        control_pnl = new JPanel();
        control_pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
        video_pnl.add(loc_lbl);
        control_pnl.add(createButton(increment));

        video_pnl.add(control_pnl, BorderLayout.SOUTH);
        video_pnl.revalidate();
        add(video_pnl);
        increment++;
    }
}

private JButton createButton(final int i) {
    play_btn = new JButton("Play");
    play_btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
//                System.out.println(file_location.get(i));
            play(i);
        }
    });
    return play_btn;
}

public void play(int i) {
    System.out.println(file_location.get(i));
}

private class Handlers implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn_upload) {
            try {   //display the image in jlabel
                file = new File("C:\\Users\\goldAnthony\\Documents\\NetBeansProjects\\VDMS\\src\\VideoInfos.txt");
                if (!file.exists()) {
                    file.createNewFile();
                    file_writer = new FileWriter(file.getAbsolutePath());
                } else {
                    file_writer = new FileWriter(file.getAbsolutePath(), true);
                }
                buffered_writer = new BufferedWriter(file_writer);
                //creating a file chooser
                filechooser = new JFileChooser();
                filechooser.setDialogTitle("Choose Your Video");
//            //below codes for select  the file 
                int returnval = filechooser.showOpenDialog(null);
                if (returnval == JFileChooser.APPROVE_OPTION) {
                    file = filechooser.getSelectedFile();
                    String filename = file.getAbsolutePath();
                    buffered_writer.write(filename);
                    buffered_writer.newLine();
                    buffered_writer.close();
                }
            } catch (IOException | HeadlessException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
}

public static void main(String[] args) throws IOException {
    //Declare and initialize local variables
    ArrayList<String> file_location = new ArrayList<>();

    //creates instances of the VlcPlayer object, pass the mediaPath and invokes the method "run"
    Media2 mediaplayer = new Media2(file_location);
    JFrame ourframe = new JFrame();
    ourframe.setContentPane(mediaplayer);
    ourframe.setLayout(new GridLayout(5, 1));
    ourframe.setSize(300, 560);
    ourframe.setVisible(true);
    ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

2 个答案:

答案 0 :(得分:1)

将组件添加到可见GUI时,基本代码为:

panel.add(...);
panel.revalidate();
panel.repaint();

您正在重新验证错误的面板:

video_pnl.revalidate(); // wrong panel
add(video_pnl);

你应该这样做:

//video_pnl.revalidate();
add(video_pnl);
revalidate();
repaint();

答案 1 :(得分:-1)

您的代码需要进行一些更改,请尝试:

private void configurePanel(ArrayList<String> file_location) {

    increment = 0;
    while (increment < file_location.size()) {
        addEntry(file_location.get(increment));//--------->New line
        increment++;
    }
}

private void addEntry(String location) {//--------->New constructor
    video_pnl = new JPanel();
    video_pnl.setLayout(new BoxLayout(video_pnl, BoxLayout.Y_AXIS));
    loc_lbl = new JLabel();
    loc_lbl.setText(location);
    control_pnl = new JPanel();
    control_pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
    video_pnl.add(loc_lbl);
    control_pnl.add(createButton(increment));

    video_pnl.add(control_pnl, BorderLayout.SOUTH);
    video_pnl.revalidate();
    add(video_pnl);
}

 private class Handlers implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btn_upload) {
                try {   //display the image in jlabel
                    file = new File("C:\\VideoInfos.txt");
                    if (!file.exists()) {
                        file.createNewFile();
                        file_writer = new FileWriter(file.getAbsolutePath());
                    } else {
                        file_writer = new FileWriter(file.getAbsolutePath(), true);
                    }
                    buffered_writer = new BufferedWriter(file_writer);
                    //creating a file chooser
                    filechooser = new JFileChooser();
                    filechooser.setDialogTitle("Choose Your Video");
//            //below codes for select  the file 
                    int returnval = filechooser.showOpenDialog(null);
                    if (returnval == JFileChooser.APPROVE_OPTION) {
                        file = filechooser.getSelectedFile();
                        String filename = file.getAbsolutePath();
                        buffered_writer.newLine();
                        buffered_writer.write(filename);
                        buffered_writer.newLine();
                        buffered_writer.close();
                        addEntry(filename);//---------------->New Line
                        validate();//------------>New Line
                    }

                } catch (IOException | HeadlessException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
    }

现在它正在运作。当然,你必须做出进一步的改变:)