简体   繁体   English

按下从File加载的JButton JList不会刷新

[英]on press of JButton JList which is loaded from File does not refresh

When I trigger the actionlistener of the buttons, the JList fails to refresh. 当我触发按钮的actionlistener时,JList无法刷新。 The only method I could do to get it to refresh, is to recall the GUI method, which isn't that efficient because that literally just opens a second window. 我可以做的唯一一个让它刷新的方法是回忆一下GUI方法,这种效率并不高效,因为它只是打开了第二个窗口。 Does anyone have any suggestions to make the add and delete button update the JList automatically? 有没有人有任何建议让添加和删除按钮自动更新JList?

    package movieinfo;    
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;

    import javax.swing.BorderFactory;
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import org.apache.commons.io.FileUtils;
    public class Swinggui {
        public static void main(String[] args) throws IOException {
            new Swinggui();
        }

        public Swinggui() throws IOException {
            yourMovies();
            gui();
            Buttons();
        }

        JFrame maingui;
        JButton enter;
        JButton remove;
        public JTextField movietext;
        JList listofmovies;
        File textfilemovie;
        JScrollPane listscroll;
        ListSelectionListener setSearch;
        JButton add;
        JTextArea movieinfo;
        DefaultListModel lmodel;

        public void gui() throws IOException {
            maingui = new JFrame("Gui");
            maingui.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.VERTICAL;
            lmodel = new DefaultListModel();
            enter = new JButton("Get Info");
            c.gridx = 2;
            c.gridy = 1;
            maingui.add(enter, c);
            add = new JButton("add");
            c.gridx = 5;
            c.gridy = 6;
            maingui.add(add, c);
            remove = new JButton("del");
            c.gridx = 6;
            c.gridy = 6;
            maingui.add(remove, c);
            movieinfo = new JTextArea(5, 20);
            movieinfo.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2,
                    Color.red));
            movietext = new JTextField(18);
            c.gridx = 1;
            c.gridy = 1;
            maingui.add(movietext, c);
            JScrollPane scrolll = new JScrollPane(movieinfo);
            c.gridx = 1;
            c.gridy = 3;
            c.gridwidth = 2;
            maingui.add(scrolll, c);
            final JLabel titlee = new JLabel("Enter movie name below!");
            c.gridx = 1;
            c.gridy = 0;
            maingui.add(titlee, c);
            c.gridx = 1;
            c.gridy = 3;
            maingui.add(titlee, c);
            final JLabel watchlist = new JLabel("Watchlist");
            c.gridx = 5;
            c.gridy = 1;
            maingui.add(watchlist, c);
            maingui.setResizable(false);
            maingui.setVisible(true);
            listofmovies = new JList(FileUtils.readLines(textfilemovie).toArray());
            listscroll = new JScrollPane(listofmovies);
            c.gridx = 4;
            c.gridy = 3;
            maingui.add(listscroll, c);
            movieinfo.setLineWrap(true);
            movieinfo.setWrapStyleWord(true);
            movieinfo.setEditable(false);
            scrolll.getPreferredSize();
            listofmovies.addListSelectionListener(setSearch);
            maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            maingui.pack();
        }

        public void Buttons() {
            enter.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    System.out.println(apicall.getMovieInfo(movietext.getText()
                            .replaceAll(" ", "%20")));
                    movieinfo.setText(apicall.getMovieInfo(movietext.getText()
                            .replaceAll(" ", "%20")));
                }

            });
            add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        FileUtils.writeStringToFile(textfilemovie,
                                "\n" + movietext.getText(), true);
                        maingui.validate();
                        maingui.repaint();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            remove.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    Scanner filescan = null;
                    File textfiletemp = new File(org.apache.commons.io.FileUtils
                            .getUserDirectory() + "/yourmoviestemp.txt");
                    try {
                        textfiletemp.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        filescan = new Scanner(textfilemovie);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    while (filescan.hasNextLine()) {
                        String line = filescan.nextLine();
                        if (line != (String) listofmovies.getSelectedValue()) {
                            try {
                                FileUtils.writeStringToFile(textfiletemp, "\n"
                                        + line, true);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    textfiletemp.renameTo(textfilemovie);
                    textfiletemp.delete();
                }

            });
        }

        public void yourMovies() throws IOException {
            textfilemovie = new File(
                    org.apache.commons.io.FileUtils.getUserDirectory()
                            + "/yourmovies.txt");
            textfilemovie.createNewFile();
            setSearch = new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent arg0) {
                    movieinfo.setText(apicall.getMovieInfo(((String) listofmovies
                            .getSelectedValue()).replaceAll(" ", "%20")));
                }
            };
        }
    }

As first tip: line != (String) listofmovies.getSelectedValue() isn't the appropriate way to compare strings. 作为第一个提示: line != (String) listofmovies.getSelectedValue()不是比较字符串的合适方法。 Always use String.equals() instead: 始终使用String.equals()代替:

!line.equals((String)listofmovies.getSelectedValue())

The only method I could do to get it to refresh, is to recall the GUI method, which isn't that efficient because that literally just opens a second window. 我可以做的唯一一个让它刷新的方法是回忆一下GUI方法,这种效率并不高效,因为它只是打开了第二个窗口。

Sorry but I don't know what is that mean. 对不起,但我不知道这是什么意思。

Does anyone have any suggestions to make the add and delete button update the JList automatically? 有没有人有任何建议让添加和删除按钮自动更新JList

Just use the list model to add/remove data from your JList within actionPerformed code. 只需使用列表模型在actionPerformed代码中添加/删除JList中的数据。 You already have a reference to lmodel which is an instance of DefaultListModel . 您已经有了对lmodel的引用,它是DefaultListModel一个实例。 Take a look to DefaultListModel.addElement() and DefaultListModel.removeElement() . 看一下DefaultListModel.addElement()DefaultListModel.removeElement()

This is all explained in How to Use Lists tutorial. 这些都在如何使用列表教程中进行了解释。

I get a null pointer when I try adding elements from the FileUtils.readLines(textfilemovie).toArray(). 当我尝试从FileUtils.readLines(textfilemovie).toArray()添加元素时,我得到一个空指针。

What is the name of the file you are attempting to read? 您尝试阅读的文件的名称是什么?

Why does your code structure still not follow the examples from the Swing tutorial??? 为什么你的代码结构仍然没有遵循Swing教程中的例子??? How many time to you need to be asked to do this??? 需要多长时间才能被要求这样做? Your code is NOT executing on the EDT which can cause random errors!!! 你的代码没有在EDT上执行,这会导致随机错误!

Post a proper SSCCE when you ask a question. 在提出问题时发布适当的SSCCE。 We don't have access to your FileUtils class. 我们无权访问您的FileUtils类。 The point of asking a question means you have done some basic debugging and have isolated the problem so you create a SSCCE that demonstrates the problem. 提出问题意味着您已经完成了一些基本的调试并将问题隔离开来,因此您创建了一个演示问题的SSCCE。 We are not her to debug your real code. 我们不是她调试你的真实代码。

I am tired of people who continually ask questions, yet fail to follow any of the advice they have been given. 我厌倦了不断提问的人,却没有听从他们提出的任何建议。 We give this advice for a reason. 我们提出这个建议是有原因的。 It is to help you to write better programs so you don't need to continually ask for our help because of poor program design. 这是为了帮助您编写更好的程序,因此您不需要因为程序设计不佳而不断寻求帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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