Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: HELP PLEASE! How to save and call back user data input.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP PLEASE! How to save and call back user data input.

    Hi everyone! My project is creating a timetable and the user can input the data his/herself to the timetable which best fits their own schedule. However currently im hardcoding the data in the code. I would like to know how do i get the data input and then be able to save and be able to call back the data whenever i run the program? THANK YOU VERY MUCH!

    import javax.swing.JTable;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.table.TableModel;
     
     
    public class TimeTable extends JFrame implements TableModelListener {
     
     
     
     
     
        public TimeTable(){
        	super("TimeTable");
     
        	Object[][] data = {
                    {"Monday", "", 
                     "", "TCP/IP LECTURE", "TCP/IP LECTURE","TCP/IP Tutorial","Break","OOP","OOP","OOP"},//1
     
                     {"Tuesday", "","", "Network Management Lecture","Network Management Lecture","Break","OOP Lecture","OOP Lecture","WNet Lecture","WNet Lecture"},//2
     
                    {"Wednesday", "TCP/IP Practical",
                     "TCP/IP Practical", "Wnet Tutorial", "","","","","",""},//3
     
                     {"Thursday", "",
                     "", "Network Management Prac", "Network Management Prac","Break","WNet Prac","WNet Prac","Network Management Tutorial",""},//4
     
                     {"Friday", "FYP",
                    	 "FYP", "FYP", "FYP","","","","",""},//5
     
                    	 {"Saturday", "Wake Up",
                         "Prepare Bag and Things for outing", "", "Meet at Jurong East","","","","",""},//6
     
                         {"Sunday", "Lih",
                             "Teaching high school", "", "","","","","",""},//7
     
     
     
                };
     
        	String[] columnNames = {"Day", 
                    "8am-9am",
                    "9am-10am",
                    "10am-11am",
                    "11am-12pm",
                    "12pm-1pm",
                    "1pm-2pm",
                    "2pm-3pm",
                    "3pm-4pm",
                    "4pm-5pm",
     
    };
     
     
     
        	final JTable table = new JTable(data, columnNames);
        	table.getModel().addTableModelListener(this);
            table.setPreferredScrollableViewportSize(new Dimension(900, 500));
            //Set Cell Heigh and Width//
            table.setRowHeight(0,90);  
            table.getColumnModel().getColumn(0).setWidth(90); //1
     
            table.setRowHeight(1,90);  
            table.getColumnModel().getColumn(1).setWidth(90); //2
     
            table.setRowHeight(2,90);  
            table.getColumnModel().getColumn(2).setWidth(90); //3
     
            table.setRowHeight(3,90);  
            table.getColumnModel().getColumn(3).setWidth(90); //4 
     
            table.setRowHeight(4,90);  
            table.getColumnModel().getColumn(4).setWidth(90); //5
     
            table.setRowHeight(5,90);  
            table.getColumnModel().getColumn(5).setWidth(90); //6
     
            table.setRowHeight(6,90);  
            table.getColumnModel().getColumn(6).setWidth(90); //7
     
            table.setRowHeight(7,90);  
            table.getColumnModel().getColumn(7).setWidth(90); //8
     
            table.setRowHeight(8,90);  
            table.getColumnModel().getColumn(8).setWidth(90); //9
     
            table.setRowHeight(10,90);  
            table.getColumnModel().getColumn(9).setWidth(90); //10
            /////////////////////////////////////
     
     
     
            //Create the scroll pane and add the table to it. 
            JScrollPane scrollPane = new JScrollPane(table);
     
            //Add the scroll pane to this window.
            getContentPane().add(scrollPane, BorderLayout.CENTER);
     
     
     
        }	
     
     
     
        public static void main(String[] args) {
            TimeTable frame = new TimeTable();
            frame.pack();
            frame.setVisible(true);
        }
     
     
     
     
     
     
    	@Override
    	 public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            TableModel model = (TableModel)e.getSource();
            String columnName = model.getColumnName(column);
            Object data = model.getValueAt(row, column);
     
     
    }
    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: HELP PLEASE! How to save and call back user data input.

    Use java.io.ObjectOutputStream

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP PLEASE! How to save and call back user data input.

    Oh alright. May i know what exactly does java.io.ObjectOutputStream do in the program? and how do i use it?

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: HELP PLEASE! How to save and call back user data input.

    The Java API docs on java.io.ObjectOutputStream contain a pretty good example with working code - did you try it? In your case, you could write your Object[][] with ObjectOutputStream.writeObject() and read it back in again with ObjectInputStream.readObject(). It's that easy. Whenever I try something new or that I can't remember how it works in Java, I always write a very simple java file that tests only that feature in isolation. I have a folder called 'scratch' that has hundreds of such programs in them. Get into the habit of saving your little test classes, they're worth their weight in gold when your brain fills up and you can't remember every last feature of the Java API. For this one, create a trivial Object[][], write it to a file via ObjectOutputStream, and then assign the same object to another reference via ObjectInputStream and print its contents (try java.util.Arrays.toString()) on the console. Once you're confident you know how Object I/O works, integrate what you've learned into your app.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: HELP PLEASE! How to save and call back user data input.

    Please don't post the same thing twice....in doing so you create a complete mess. I've locked your other thread linked below

    HELP PLEASE! How to save and call back user data input.

Similar Threads

  1. Save data entered upon reopening
    By SHStudent21 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 12th, 2011, 09:24 PM
  2. How and where do you save user settings?
    By snytkine in forum AWT / Java Swing
    Replies: 3
    Last Post: October 12th, 2010, 05:46 AM
  3. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM
  4. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM
  5. How to detect audio input from a telephone call?
    By ces_31 in forum Java Theory & Questions
    Replies: 0
    Last Post: August 12th, 2009, 12:45 AM