Results 1 to 10 of 10
  1. #1
    KneeDeep is offline Member
    Join Date
    Jun 2014
    Posts
    6
    Rep Power
    0

    Default Implementing back end code to front in GUI - Java Swing

    Hi all.

    I am messing around with a Swing application and am a bit stuck as to how to join the front and back ends.

    This is the code for the DataManager class which is responsible for loading and saving objects and interfacing with rest of the system. The data I need displayed comes from a .txt file in the applications directory. I need to show certain aspects of the data in the JComboBoxes in the GUI class.

    Java Code:
    package programdataapplication;
    
    
    import java.io.*;
    import java.util.*;
    
    public class DataManager {
    
        private static final String DELIMITER = ",";
    
        private EntityFactory personFactory;
        private EntityFactory courseFactory;
        private EntityFactory majorFactory;
        private EntityFactory programFactory;
    
        public DataManager() {
            Person initialisePerson = new Person();
            personFactory = new EntityFactory(initialisePerson);
            Course initialiseCourse = new Course();
            courseFactory = new EntityFactory(initialiseCourse);
            Major initialiseMajor = new Major();
            majorFactory = new EntityFactory(initialiseMajor);
            Program initialiseProgram = new Program();
            programFactory = new EntityFactory(initialiseProgram);
    
            Map<String, EntityDataObject> primaryPersons = readPrimary(initialisePerson.getFileName());
            updateWithSecondary(primaryPersons, initialisePerson.getSecondaryTableNames());
            addPersons(primaryPersons.values());
    
            Map<String, EntityDataObject> primaryCourses = readPrimary(initialiseCourse.getFileName());
            updateWithSecondary(primaryCourses, initialiseCourse.getSecondaryTableNames());
            addCourses(primaryCourses.values());
    
            Map<String, EntityDataObject> primaryMajors = readPrimary(initialiseMajor.getFileName());
            updateWithSecondary(primaryMajors, initialiseMajor.getSecondaryTableNames());
            addMajors(primaryMajors.values());
    
            Map<String, EntityDataObject> primaryPrograms = readPrimary(initialiseProgram.getFileName());
            updateWithSecondary(primaryPrograms, initialiseProgram.getSecondaryTableNames());
            addPrograms(primaryPrograms.values());
        }
    
        public void addPerson(EntityDataObject entityDataObject) {
            personFactory.add(new Person(entityDataObject));
        }
    
        public void addPersons(Collection<EntityDataObject> entityDataObjects) {
            for (EntityDataObject entityDataObject : entityDataObjects) {
                addPerson(entityDataObject);
            }
        }
    
        public void addCourse(EntityDataObject entityDataObject) {
            courseFactory.add(new Course(personFactory, entityDataObject));
        }
    
        public void addCourses(Collection<EntityDataObject> entityDataObjects) {
            for (EntityDataObject entityDataObject : entityDataObjects) {
                addCourse(entityDataObject);
            }
        }
    
        public void addMajor(EntityDataObject entityDataObject) {
            majorFactory.add(new Major(courseFactory, personFactory, entityDataObject));
        }
    
        public void addMajors(Collection<EntityDataObject> entityDataObjects) {
            for (EntityDataObject entityDataObject : entityDataObjects) {
                addMajor(entityDataObject);
            }
        }
    
        public void addProgram(EntityDataObject entityDataObject) {
            programFactory.add(new Program(majorFactory, courseFactory, personFactory, entityDataObject));
        }
    
        public void addPrograms(Collection<EntityDataObject> entityDataObjects) {
            for (EntityDataObject entityDataObject : entityDataObjects) {
                addProgram(entityDataObject);
            }
        }
    
        private List<String> findAttributesFromFile(String string) {
            List<String> attributes = new ArrayList<String>();
            while (string.contains(DELIMITER)) {
                attributes.add(string.substring(0, string.indexOf(DELIMITER)));
                string = string.substring(string.indexOf(DELIMITER) + 1);
            }
            return attributes;
        }
    
        private EntityDataObject parseAttributes(List<String> attributeNames, String line) {
            List<String> attributesFromFile = findAttributesFromFile(line);
            EntityDataObject entityDataObject = new EntityDataObject();
            for (String attribute: attributeNames) {
                entityDataObject.add(attribute, attributesFromFile.get(attributeNames.indexOf(attribute)));
            }
            return entityDataObject;
        }
    
    
        public Map<String, EntityDataObject> readPrimary(String fileName) {
            Map<String, EntityDataObject> primary = new HashMap<String, EntityDataObject>();
            EntityDataObject entityDataObject;
            try {
                List<String> attributes;
                File inputFile = new File(fileName + ".txt");
                Scanner scanner = new Scanner(inputFile);
                String line;
    
                //Header from 1st line
                if (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                } else {
                    return primary;
                }
                attributes = findAttributesFromFile(line);
    
                while (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                    entityDataObject = parseAttributes(attributes, line);
                    primary.put(entityDataObject.find("id"), entityDataObject);
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return primary;
        }
    
        public void updateWithSecondary(Map<String, EntityDataObject> newEntityDataObjects, List<String> secondaryTables) {
            Map<String, EntityDataObject> entityDataObjects = newEntityDataObjects;
            for (String secondaryTable : secondaryTables) {
                try {
                    List<String> attributeNames;
                    List<String> attributes;
                    File inputFile = new File(secondaryTable + ".txt");
                    Scanner scanner = new Scanner(inputFile);
                    HashMap<String, List<String>> secondaryList = new HashMap<String, List<String>>();
                    String line;
    
                    //Header from 1st line
                    if (scanner.hasNextLine()) {
                        line = scanner.nextLine();
                    } else {
                        break;
                    }
                    attributeNames = findAttributesFromFile(line);
    
                    while (scanner.hasNextLine()) {
                        line = scanner.nextLine();
                        attributes = findAttributesFromFile(line);
                        if (entityDataObjects.containsKey(attributes.get(0))) {
                            entityDataObjects.get(attributes.get(0)).addItemToList(attributeNames.get(1),attributes.get(1));
                        }
                    }
    
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    
        private void write(Table table) {
            try {
                FileWriter fileWriter = new FileWriter(table.getFileName() + "2.txt");
                String newLine = System.getProperty("line.separator");
                for (String string : table.getData()) {
                    fileWriter.write(string + newLine);
                }
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void writePrimary() {
            write(personFactory.getPrimaryTable());
            write(courseFactory.getPrimaryTable());
            write(majorFactory.getPrimaryTable());
            write(programFactory.getPrimaryTable());
        }
    
        public void writeSecondary() {
            Map<String, Table> secondaryTables = new HashMap<String, Table>();
            secondaryTables.putAll(personFactory.getSecondaryTables());
            secondaryTables.putAll(courseFactory.getSecondaryTables());
            secondaryTables.putAll(majorFactory.getSecondaryTables());
            secondaryTables.putAll(programFactory.getSecondaryTables());
            for (Map.Entry<String, Table> entry : secondaryTables.entrySet()) {
                write(entry.getValue());
            }
    
        }
    
    
    
    
    }
    This is the GUI class I need the above class to interface with:

    Java Code:
    package programdataapplication;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.border.EmptyBorder;
    
    public class ReportSelectGUI extends JFrame{
        
        private JButton viewAllReports = new JButton("View All Program Details");
        private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course"); 
        private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches"); 
        private JLabel courseLabel = new JLabel("Select a Course: ");
        private JLabel examinerLabel = new JLabel("Select an Examiner: "); 
        private JPanel panel = new JPanel(new GridLayout(6,2,4,4));
          
       
         
        
            public ReportSelectGUI(){   
                  reportInterface();            
                  allReportsBtn();  
                  comboBoxes();
         }     
            
            
       
            
            
        private void reportInterface(){         
              setTitle("Choose Report Specifications");                   
              setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              JPanel panel = new JPanel(new FlowLayout());        
              add(panel, BorderLayout.CENTER);
              setSize(650,200);
              setVisible(true);    
              setResizable(false);
              setLocationRelativeTo(null);
        
    }
         private void allReportsBtn(){
            JPanel panel = new JPanel(new GridLayout(1,1)); 
            panel.setBorder(new EmptyBorder(70, 50, 70, 25));
            panel.add(viewAllReports);        
            viewAllReports.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){               
                    new AllDataGUI();
                }
            });         
            add(panel, BorderLayout.LINE_END);
        }       
         
             private void comboBoxes(){     
    
            panel.setBorder(new EmptyBorder(0, 5, 5, 10));
          
            JComboBox comboBox1 = new JComboBox();          
            panel.add(examinerLabel);
            panel.add(comboBox1);          
            panel.add(viewTaughtCourses);
             viewTaughtCourses.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                 
                     new SelectedExaminerGUI();
                }
            });  
          
             JComboBox comboBox2 = new JComboBox();
             panel.add(courseLabel);         
             panel.add(comboBox2); 
             panel.add(viewPrograms);
             viewPrograms.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                 
                     new SelectedCourseGUI();             
                }
            });  
             add(panel, BorderLayout.LINE_START); 
    
        }   
    
    }

    Other classes in the application include ProgramDataDriver (driver class), Course, Entity, EntityFactory, Major, Person, PRogram, Table, ProgramDataApplication (Application class which has main() method), MainGUI, AllDataGUI, ReportSelectGUI, SelectedExaminerGUI.

    Any suggestions as to how I can join back-end and GUI classes would be great.

    Thanks.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Implementing back end code to front in GUI - Java Swing

    Have you created a DateManager object somewhere in your GUI? You will need it in order to get the data and display them in the components.

  3. #3
    KneeDeep is offline Member
    Join Date
    Jun 2014
    Posts
    6
    Rep Power
    0

    Default Re: Implementing back end code to front in GUI - Java Swing

    DateManager? I assume you mean DataManger?

    I have not.

    How could I go about doing this?

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Implementing back end code to front in GUI - Java Swing

    DataManager yes, excuse the typo.

    You need to create the object that you interface the logic with the GUI. You need a single instance of DataManager throughout your application.
    A way to accomplish this is to make the DataManager a singleton. Then you can access that instance easily anywhere in your application, retrieve the data you want and display them in your JComponents.
    As I see it this JFrame here is only a navigation screen. So, the panels where you display data, you need a Datamanager.

  5. #5
    KneeDeep is offline Member
    Join Date
    Jun 2014
    Posts
    6
    Rep Power
    0

    Default Re: Implementing back end code to front in GUI - Java Swing

    To be honest I am a little lost. While I instantiate DataManager, I am not sure how to call anything from the class without getting Null Pointer Exceptions.

    I am instantiating it like this in the ReportGUI class:

    Java Code:
    private final DataManager dataManage = new DataManager();

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Implementing back end code to front in GUI - Java Swing

    You said that you need to display some data from .txt files. Where do you store this data?

  7. #7
    KneeDeep is offline Member
    Join Date
    Jun 2014
    Posts
    6
    Rep Power
    0

    Default Re: Implementing back end code to front in GUI - Java Swing

    It is only in the .txt files in the applications directory. You are supposed to be able to read and write to it from the GUI. There are Factory classes that should store it I believe. My understanding is that all interfacing is handled by the DataManager class though.

    This is a Factory class I am using:

    Java Code:
    import java.util.*;
    
    public class EntityFactory <T extends Entity> {
    
        private Map<String, Entity> cache;
        private final String fileName;
        private final List<String> secondaryTableNames;
        private final List<String> secondaryFieldNames;
        private final List<String> primaryColumns;
    
        public EntityFactory (Entity entity) {
            cache = new HashMap<String, Entity>();
            fileName = entity.getFileName();
            primaryColumns = entity.getPrimaryColumns();
            secondaryTableNames = entity.getSecondaryTableNames();
            secondaryFieldNames = entity.getSecondaryFieldNames();
        }
    
        public void add(Entity entity) {
            cache.put(entity.getID(), entity);
        }
    
        public void remove(String id) {
            cache.remove(id);
        }
    
        public boolean contains(String id) {
            return cache.containsKey(id);
        }
    
        public Table getPrimaryTable() {
    
            Table primary = new Table(fileName, primaryColumns);
            for (Map.Entry<String, Entity> entry : cache.entrySet()) {
                primary.add(entry.getValue().getPrimaryTableRecord());
            }
            return primary;
        }
    
        public Map<String, Table> getSecondaryTables() {
            HashMap<String, Table> secondary = new HashMap<String, Table>();
            for (String secondaryTableName : secondaryTableNames) {
                secondary.put(secondaryTableName, new Table(secondaryTableName, secondaryFieldNames.get(secondaryTableNames.indexOf(secondaryTableName))));
            }
    
            Table table;
            for (Map.Entry<String, Entity> cacheEntry : cache.entrySet()) {
                Entity entity = cacheEntry.getValue();
                Map<String, List<String>> secondaryTableRecords = entity.getSecondaryTableRecords();
                for (Map.Entry<String, List<String>> secondaryEntry : secondaryTableRecords.entrySet()) {
                    table = secondary.get(entity.getFileName() + "." + secondaryEntry.getKey());
                    table.add(cacheEntry.getKey(), secondaryEntry.getValue());
                }
            }
            return secondary;
        }
    
    }

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Implementing back end code to front in GUI - Java Swing

    that's fine. Your datamanager encapsulates all the data you need for your application. What is the problem then? You just need to get it and output it. Why should you get NPEs?

  9. #9
    KneeDeep is offline Member
    Join Date
    Jun 2014
    Posts
    6
    Rep Power
    0

    Default Re: Implementing back end code to front in GUI - Java Swing

    Yeah all i need is for the GUI to talk to the DataManager, but I am so inept that I don't know how to do. What is an NPE?

  10. #10
    KneeDeep is offline Member
    Join Date
    Jun 2014
    Posts
    6
    Rep Power
    0

    Default Re: Implementing back end code to front in GUI - Java Swing

    Quote Originally Posted by KneeDeep View Post
    Yeah all i need is for the GUI to talk to the DataManager, but I am so inept that I don't know how to do. What is an NPE?
    Right, null pointer exception...

    I am guessing an NPE would occur if there was an issue with any data contained (for example if it wasn't there). I am still working on this if anyone can assist in the get and output.

Similar Threads

  1. Replies: 1
    Last Post: 07-10-2012, 11:33 PM
  2. Swing tree java code
    By bibingigin in forum AWT / Swing
    Replies: 2
    Last Post: 02-10-2012, 08:18 AM
  3. Replies: 0
    Last Post: 11-30-2011, 06:46 PM
  4. Data Exchange Format between Web Front End and Web Back End
    By jeanjiang in forum Reviews / Advertising
    Replies: 0
    Last Post: 06-25-2011, 02:37 PM
  5. Help me on this code of Java swing
    By gauravfzd in forum New To Java
    Replies: 16
    Last Post: 10-16-2010, 06:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •