简体   繁体   中英

Retrieving filename from Jfilechooser into JList

I'm having some problems with my class that I need some help with. The class is supposed to take retrieve a csv.file from the file chooser, and then put the name of the file into a Jlist, but I can't get the JList to print the arraylist that I put the chosen files into. This is the code I've got, so I'd be grateful if you could take a look at it and tell me what I need to change with it in order to get the JList to print the arraylist.

import dao.UserDAO;
import db.DemoDB;
import model.Activity;
import model.DataPoint;
import model.Statistics;

public class LoginGUI1 {
    DemoDB DemoDBSingleton = null;

    private JTabbedPane tabbedPane = new JTabbedPane();
    UserDAO userDao = new UserDAO();
    JFrame mainFrame = new JFrame("Välkommen till din app");
    JFrame f = new JFrame("User Login");
    JLabel l = new JLabel("Användarnamn:");
    JLabel l1 = new JLabel("Lösenord:");
    JTextField textfieldUsername = new JTextField(10);
    JPasswordField textfieldPassword = new JPasswordField(10);
    JButton loginButton = new JButton("Logga In");
    JFileChooser fc = new JFileChooser();
    JMenuBar mb = new JMenuBar();
    JList listAct = new JList();
    List<Activity> activityList = new ArrayList<Activity>();
    List activityList1 = new Vector();

    JFrame jf;
    JMenu menu;
    JMenuItem importMenu, exitMenu;


    Activity activity = new Activity();



    public LoginGUI1() throws IOException {



        DemoDBSingleton = DemoDB.getInstance();
        ProgramMainFrame();

    }


    private void ProgramMainFrame() throws IOException {

        mainFrame.setSize(800, 600);
        mainFrame.setVisible(true);
        mainFrame.setJMenuBar(mb);
        mainFrame.getContentPane().setLayout(new BorderLayout());;

        tabbedPane.add("Dina Aktiviteter", createViewActPanel());
        mainFrame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
        tabbedPane.add("Diagram för vald aktivitet", createViewDiagramPanel());
        tabbedPane.add("Statistik för vald aktivitet", createViewStatisticsPanel());
        tabbedPane.add("Kartbild över vald aktivitet", createViewMapPanel());


        JMenuBar mb = new JMenuBar();
        menu = new JMenu("Meny");
        importMenu = new JMenuItem("Importera aktivitet");
        importMenu.addActionListener(importActionListener);
        exitMenu = new JMenuItem("Avsluta program");
        exitMenu.addActionListener(exitActionListener);
        menu.add(importMenu);
        menu.add(exitMenu);
        mb.add(menu);
        mainFrame.setJMenuBar(mb);

    /*  JPanel listholder = new JPanel();
        listholder.setBorder(BorderFactory.createTitledBorder("ListPanel"));
        mainFrame.add(listholder);
        listholder.setVisible(true);
        listholder.setSize(500,400);*/



    }

    private JPanel createViewActPanel() {
        JPanel analogM = new JPanel();
        analogM.setBackground(new Color(224, 255, 255));
        return analogM;
    }





    ActionListener importActionListener = new ActionListener() {


        @Override
        public void actionPerformed(ActionEvent e) {
            int returnValue = fc.showOpenDialog(mainFrame);
            if(returnValue == JFileChooser.APPROVE_OPTION)
            {
                File file = fc.getSelectedFile();
                if(file != null)
                {
                    String fileName = file.getAbsolutePath();
                Activity activity = null;
                try {
                    activity = new Activity();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                    try {
                        activity.csvFileReader(fileName);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    activityList.add(activity);
                    listAct.setListData(activityList.toArray());

                }

            }
        }

    };




    public static void main(String[] args) throws IOException {
        new LoginGUI();

    }
}

There's a lot of context missing, so the best I can do is provide a simple running example which works.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test {

    public static void main(String args[]) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JList<File> fileList;
        private DefaultListModel<File> fileListModel;

        public TestPane() {
            fileListModel = new DefaultListModel<>();
            fileList = new JList(fileListModel);

            JButton addButton = new JButton("Add");
            addButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    JFileChooser fileChooser = new JFileChooser();
                    if (fileChooser.showOpenDialog(TestPane.this) == JFileChooser.APPROVE_OPTION) {
                        File file = fileChooser.getSelectedFile();
                        fileListModel.addElement(file);
                    }
                }
            });

            setLayout(new BorderLayout());
            add(new JScrollPane(fileList));
            add(addButton, BorderLayout.SOUTH);
        }

    }
}

I'd highly recommend having a look at How to use lists to get a better understanding of how the API works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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