java - 按下从文件加载的 JButton JList 时不会刷新

标签 java swing jbutton actionlistener jlist

当我触发按钮的 Action 监听器时,JList 无法刷新。 我唯一能做的让它刷新的方法是调用 GUI 方法,这种方法效率不高,因为它实际上只是打开了第二个窗口。 有没有人有任何建议让添加和删除按钮自动更新 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")));
                }
            };
        }
    }

最佳答案

第一个提示:line != (String) listofmovies.getSelectedValue() 不是比较字符串的合适方法。始终使用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.

抱歉,我不知道这是什么意思。

Does anyone have any suggestions to make the add and delete button update the JList automatically?

只需使用列表模型在 actionPerformed 代码中的 JList 中添加/删除数据。您已经拥有对 lmodel 的引用,它是 DefaultListModel 的实例。看看DefaultListModel.addElement()DefaultListModel.removeElement() .

这一切都在 How to Use Lists 中有解释。教程。

关于java - 按下从文件加载的 JButton JList 时不会刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543524/

相关文章:

java - 计算与流的正则表达式匹配

java - Swing - 如何模仿为什么我的外观会绘制禁用图标?

java - 更改 JTable 中某些行的颜色但不是全部

java - 如何通过鼠标输入事件更改标签的图标?

java - 将自身设置为 JFrame 的默认按钮的 JButton 子类

java - 使用递归从数组中获取计数

java - 借鉴其他应用问题 |开关变灰

java - Kafka Streams - 按时间戳/序列保存消息?

java jButton.doClick() 不执行该功能(但单击按钮)

java - TreeCellRenderer 吃 CPU