0

So I'm in college trying to do a JFrame assignment and I'm trying to be able to load a .txt file through JFileChooser and have the text inside the file to show up in the few JTextFields that are inside this JFrame. Right now it's not working and with all the information I have from my class lectures and such, I can't find a way around this problem.

So here's the full code, the specific section I'm trying to work with is the case: "Load" section.

package assignment3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class SocialMediaList extends JFrame {

private JPanel display;
private JLabel siteNameLabel;
private JTextField siteName;
private JLabel userIDLabel;
private JTextField userID;
private JLabel contactsLabel;
private JTextField contacts;

private JPanel btns;
private JButton loadBtn;
private JButton addBtn;
private JButton saveBtn;
private JButton exitBtn;

private JPanel list;
private JList myList;

private ArrayList<SocialMedia> mediaList;

public SocialMediaList() {

    mediaList = new ArrayList();
    myList = new JList(mediaList.toArray());

    this.setLayout(new GridLayout(2,1,5,5));

    display = new JPanel();
    display.setLayout(new GridLayout(5,2,5,5));

    siteNameLabel = new JLabel("  Site Name: ");
    siteName = new JTextField(10);
    userIDLabel = new JLabel("  User ID: ");
    userID = new JTextField(10);
    contactsLabel = new JLabel("  No. of Contacts: ");
    contacts = new JTextField(10);

    display.add(siteNameLabel);
    display.add(siteName);
    display.add(userIDLabel);
    display.add(userID);
    display.add(contactsLabel);
    display.add(contacts);

    this.add(display);

    btns = new JPanel();
    btns.setLayout(new GridLayout(1,3,5,5));

    loadBtn = new JButton("Load");
    loadBtn.setActionCommand("Load");
    addBtn = new JButton("Add");
    saveBtn = new JButton("Save");
    exitBtn = new JButton("Exit");

    btns.add(loadBtn);
    btns.add(addBtn);
    btns.add(saveBtn);
    btns.add(exitBtn);

    this.add(btns);

    list = new JPanel();
    list.setLayout(new FlowLayout());
    JScrollPane myScrollPane = new JScrollPane(myList);
    list.add(myScrollPane);

    setLayout(new BorderLayout());
    this.add(display,BorderLayout.NORTH);
    this.add(btns,BorderLayout.CENTER);
    this.add(list,BorderLayout.SOUTH);

    ButtonListeners buttonListener = new ButtonListeners();
    loadBtn.addActionListener(buttonListener);
    addBtn.addActionListener(buttonListener);
    saveBtn.addActionListener(buttonListener);
    exitBtn.addActionListener(buttonListener);
}

public static void main(String[] args) {
    SocialMediaList list = new SocialMediaList();
    list.setTitle("Assignment #3");
    list.setSize(600,350);
    list.setLocationRelativeTo(null);
    list.setVisible(true);
    list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class ButtonListeners implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand()) {

            case "Load":
                File file;
                try {

                    JFileChooser chooser = new JFileChooser();

                    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

                        file = chooser.getSelectedFile();

                        Scanner input = new Scanner(file);

                        while (input.hasNextLine()) {
                            SocialMedia account = mediaList.get(myList.getSelectedIndex());
                            siteName.setText(account.getSiteName());
                            userID.setText(account.getUserID());
                            contacts.setText(String.valueOf(account.getContacts()));

                            myList.setListData(mediaList.toArray());
                            add(siteName);
                            add(userID);
                            add(contacts);

                            if (mediaList.size() > 0) {
                                myList.setSelectedIndex(0);
                            }
                        }


                    }

                } catch (FileNotFoundException ex) {
                    Logger.getLogger(SocialMediaList.class.getName()).log(Level.SEVERE, null, ex);
                }


            case "Add": 
                String s = siteName.getText();
                String u = userID.getText();
                int c = Integer.valueOf(contacts.getText());
                SocialMedia mySocialMedia = new SocialMedia();
                mySocialMedia.setSiteName(s);
                mySocialMedia.setUserID(u);
                mySocialMedia.setContacts(c);

                mediaList.add(mySocialMedia);
                myList.setListData(mediaList.toArray());
                break;

            case "Save":
        try {
            FileWriter writer = new FileWriter("socialMedia2.txt");
            writer.write(mediaList.toString());
            writer.write(myList.toString());
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(SocialMediaList.class.getName()).log(Level.SEVERE, null, ex);
        }

            case "Exit":
                System.exit(0);
        }
    }
}

class ExitListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}
}
CSRadical
  • 113
  • 1
  • 2
  • 7
  • share minimal testable code. there are lots of compilation error in your shared code. – Braj Jul 24 '14 at 17:51
  • What is the point of your Scanner code? You check if the file has input but you never read anything from the file. Why is all that other code in the Scanner loop. What does that code have to do with data in the file? – camickr Jul 24 '14 at 17:51
  • I honestly don't know. I'm very new to this and a lot of the work has been referencing on lectures. It's very likely I'm throwing in stuff that doesn't belong there, but I don't have enough experience with this to know what. I'm just trying to find a way to take in a text file through JFileChooser and input the data into the text fields. I'm honestly confused with most of this. – CSRadical Jul 24 '14 at 17:56
  • 1
    *"It's very likely I'm throwing in stuff that doesn't belong there, but I don't have enough experience.."* That's what the JavaDocs and tutorials are for (and SO isn't). – Andrew Thompson Jul 24 '14 at 18:15
  • 1
    The essential idea is shown [here](http://stackoverflow.com/a/16595868/230513). – trashgod Jul 24 '14 at 18:42

0 Answers0