【问题标题】:cannot update in panel when i choose a file path当我选择文件路径时无法在面板中更新
【发布时间】:2014-01-29 18:24:29
【问题描述】:

一旦用户选择“点击这里上传”按钮,文件路径将被存储到一个文件中,这些路径被读入一个数组列表,显示在 JPanel 上。问题是一旦选择了一个文件,它就永远不会在 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);
}

}

【问题讨论】:

  • 我认为这会有所帮助,如果您添加更多上下文信息而不是仅仅发布整个应用程序代码。现在每个人都必须弄清楚问题的背景!
  • 对 GUI 的更改最好在 Event Dispatch Thread 上完成,我看不到您在哪里更新...您的问题不清楚。

标签: java swing arraylist java-io


【解决方案1】:

当您将组件添加到可见的 GUI 时,基本代码是:

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

您重新验证了错误的面板:

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

你应该这样做:

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

【讨论】:

  • @MastaOpec,那我想我不明白你的问题。发布一个 MCTRE 来说明问题。
  • 来自更新源文件的 Handler 类,从不做任何实际更新 UI 的操作。看看我的意思,当你更新你的答案时让我知道,这样我就可以投票了
【解决方案2】:

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

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());
                }
            }
        }
    }

现在它正在工作。当然,您必须进行进一步的更改:)

【讨论】:

  • 不需要直接调用update(Graphics)方法。当组件需要重新绘制时,Swing 会这样做。
  • 如果您重用组件 - 并且如果您想在组件不可见时更改图形 - update(getGraphics()) 非常有用(我的意思是在将组件设置为可见之前)。 repaint() 仅在组件可见时有效..
  • @MastaOpec - 您的目标是将所有 JPanel 垂直添加到某个 JPanel?如果是这样 - 尝试重新验证父 JPanel。你也可以试试pack()
  • 您也可以在父级JPanel 中使用BoxLayout
  • 请不要建议使用 update 或 getGraphics,您无法控制绘制过程,这是重绘管理器的责任
猜你喜欢
  • 1970-01-01
  • 2015-03-06
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 2019-03-17
  • 1970-01-01
相关资源
最近更新 更多