Eric Lang
Eric Lang

Reputation: 274

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. 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?

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

Upvotes: 0

Views: 269

Answers (2)

camickr
camickr

Reputation: 324078

I get a null pointer when I try adding elements from the 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??? 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!!!

Post a proper SSCCE when you ask a question. We don't have access to your FileUtils class. 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. 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.

Upvotes: 0

dic19
dic19

Reputation: 17971

As first tip: line != (String) listofmovies.getSelectedValue() isn't the appropriate way to compare strings. Always use String.equals() instead:

!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.

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?

Just use the list model to add/remove data from your JList within actionPerformed code. You already have a reference to lmodel which is an instance of DefaultListModel. Take a look to DefaultListModel.addElement() and DefaultListModel.removeElement().

This is all explained in How to Use Lists tutorial.

Upvotes: 2

Related Questions