0

I have been doing a small program to test save and load data by jtable in java. there is no problem in saving but can't load the data. when I load the data table shows empty, nothing in there. May I have any suggestions! here is the Code:

       public class NewOne extends JFrame {

       public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                NewOne frame = new NewOne();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

            public NewOne() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton Button = new JButton("save");
    Button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             saveTable();
        }
    });
    Button.setBounds(10, 11, 89, 23);
    contentPane.add(Button);

    JButton Button1 = new JButton("load");
    Button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            loadTable();
        }
    });
    Button1.setBounds(150, 11, 89, 23);
    contentPane.add(Button1);

    table = new JTable();
    table.setModel(new DefaultTableModel(
        new Object[][] {
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
        },
        new String[] {
            "New column", "New column", "New column", "New column", "New column"
        }
    ));
    table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    table.setBounds(20, 45, 372, 80);
    contentPane.add(table);
}

private JFileChooser myJFileChooser = new JFileChooser(new File("."));

private DefaultTableModel tableModel = new DefaultTableModel();
      private JTable myJTable = new JTable(tableModel);
      private JTable table;

private void saveTable() {
    if (myJFileChooser.showSaveDialog(this) ==
            JFileChooser.APPROVE_OPTION ) {
        saveTable(myJFileChooser.getSelectedFile());
    }
}

private void saveTable(File file) {
    try {
        ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(file));
            out.writeObject(tableModel.getDataVector());
            out.writeObject(getColumnNames());
            out.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
}

private Vector<String> getColumnNames() {
    Vector<String> columnNames = new Vector<String>();
    for (int i = 0; i < myJTable.getColumnCount(); i++)
        columnNames.add(myJTable.getColumnName(i) );
        return columnNames;
}

private void loadTable() {
    if (myJFileChooser.showOpenDialog(this) ==
            JFileChooser.APPROVE_OPTION )
        loadTable(myJFileChooser.getSelectedFile());
}

private void loadTable(File file) {
    try {
        ObjectInputStream in = new ObjectInputStream(
        new FileInputStream(file));
        Vector rowData = (Vector)in.readObject();
        Vector columnNames = (Vector)in.readObject();
        tableModel.setDataVector(rowData, columnNames);
        in.close();
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}
 }
Latif
  • 125
  • 1
  • 4
  • 16
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Sep 10 '15 at 10:30

1 Answers1

1

I guess your problem lies here:

 private void saveTable(File file) {
        try {
            ObjectOutputStream out = new ObjectOutputStream(
                    new FileOutputStream(file));
                out.writeObject(tableModel.getDataVector());
                out.writeObject(getColumnNames());
                out.close();
            }

Your tableModel is a new DefaultTableModel(); if i haven't missread your code. per documentation : "DefaultTableModel() Constructs a default DefaultTableModel which is a table of zero columns and zero rows." therefore you put an empty file to your OutputSream.

Maybe you just used he wrong table after all do you want to get the content of your myJTable ? If that is true the solution should look like:

private void saveTable(File file) {
            try {
                ObjectOutputStream out = new ObjectOutputStream(
                        new FileOutputStream(file));
                    out.writeObject(myJTable.getDataVector());
                    out.writeObject(getColumnNames());
                    out.close();
                }

Correct me if I am wrong. Its difficult to look at foreign code :P

Tom Wellbrock
  • 2,982
  • 1
  • 13
  • 21
  • Thanks, I have changed out.writeObject(tableModel.getDataVector()); to out.writeObject(myJTable.getDataVector()); but it gave me error of The method getDataVector() is undefined for the type JTable – Latif Sep 10 '15 at 10:46
  • to get the value at a Index try:`int selectedRowIndex = myJTable.getSelectedRow(); int selectedColumnIndex = myJTable.getSelectedColumn(); Object selectedObject = (Object) myJTable.getModel().getValueAt(selectedRowIndex, selectedColumnIndex);` – Tom Wellbrock Sep 10 '15 at 10:58