Results 1 to 11 of 11
  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation How to update JTable contents

    I have a JTable in the JInternalFrame
    I have CUD Instruction (Create, Update, Delete actionCommand)

    I want to update the TableModel of the JTable.

    I have used this method (similar with JComboBox)
    Java Code:
    //rowSet is a Vector from the JDBC
    //colNames is a String Array containg the column names of the table
    employeeTable.setModel(new DefaultTableModel(rowSet, colNames));
    Well obviously, I knew that they are different components, yet I thought it would work.

    I am asking in what sort of way can I update the JTable content.

    What JTable method am I suppose to use.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This seems like deja vu all over again.

    The issue and solution are similar to your previous thread: If you want to change everything in the JTable, create a new model and set the JTable's model to the new one. If you want to update the information held in the JTable, then change the data in the model and it will be updated in the JTable.

    Two further issues:
    1) Have you gone through the JTable tutorial? It goes into this, but my main disagreement with the tutorial is that it relies heavily on the AbstractTableModel, and I much prefer to work with the DefaultTableModel.

    2) The specifics of any answer that we give will depend on the specifics of your problem. Right now you've asked about generalities and so a tutorial referral and general advice is about all I can give. If you need more specific help, create an SSCCE (no NetBeans autogenerated code please) and post it. You know the drill.

    Best of luck.

  3. #3
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    1) Have you gone through the JTable tutorial? It goes into this, but my main disagreement with the tutorial is that it relies heavily on the AbstractTableModel, and I much prefer to work with the DefaultTableModel.

    2) The specifics of any answer that we give will depend on the specifics of your problem. Right now you've asked about generalities and so a tutorial referral and general advice is about all I can give. If you need more specific help, create an SSCCE (no NetBeans autogenerated code please) and post it. You know the drill.
    1.) I have gone through the JTable tutorial. But since I you preferred DefaultTableModel, am I suppose to do the same similar with the JComboBox.setModel(new DefaultComboBoxModel(loadTypes());

    2.) As for now I may not be able to work it out. Unless I get severly confused. That the main reason I was to ignorant on posting the SSCCE (with Netbeans autogenerated code)
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by chyrl View Post
    1.) I have gone through the JTable tutorial. But since I you preferred DefaultTableModel, am I suppose to do the same similar with the JComboBox.setModel(new DefaultComboBoxModel(loadTypes());
    Your best bet is to try possible solutions out and see what happens -- that's the beauty of programming: you have this powerful lab right in front of you that allows you to experiment to your heart's content.


    2.) As for now I may not be able to work it out. Unless I get severly confused. That the main reason I was to ignorant on posting the SSCCE (with Netbeans autogenerated code)
    Huh? Again, you can't break anything by trying. Experiment. Play with your code. Try to create an SSCCE, and if it fails, post it here.

  5. #5
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Just want to clarify.
    What the difference between AbstractTableModel and DefaultTableModel?
    And why did you preferred me to use DefaultTableModel?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  6. #6
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation Thread is almost solved!

    I was able to understand now how to use the DefaultTableModel
    Thanks Fubarable.

    But there still lies a bug in my program.
    A java.lang.ArrayIndexOutOfBoundsException: -1 occurs when database.edit(...) and database.delete(id) in the Model class.

    Also I there a code to diselect the rowSelected in the nameTable? Aside from making the nameScroll.setEnabled(flag) to be placed on editMode(boolean flag) method.

    Here is the SSCCE of this thread

    Java Code:
    package demo2;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class View extends JFrame{
    
        private final JLabel lastnameLabel = new JLabel("Last Name", SwingConstants.LEFT);
        private final JLabel firstnameLabel = new JLabel("First Name", SwingConstants.LEFT);
        private final JLabel middlenameLabel = new JLabel("Middle Name", SwingConstants.LEFT);
        
        public JTextField lastnameField = new JTextField(10);
        public JTextField firstnameField = new JTextField(10);
        public JTextField middlenameField = new JTextField(10);
        public JButton createButton = new JButton("Create");
        public JButton editButton = new JButton("Edit");
        public JButton deleteButton = new JButton("Delete");
        public JTable nameTable = new JTable();
        public JScrollPane nameScroll = new JScrollPane(nameTable);
    
        public View() {
            defaultSet(false);       
            setTitle("Employee");
            setResizable(false);
            setSize(new Dimension(400,550));        
        }
    
        private void defaultSet(boolean flag) {
            lastnameField.setEnabled(flag);
            firstnameField.setEnabled(flag);
            middlenameField.setEnabled(flag);
            editButton.setEnabled(flag);
            deleteButton.setEnabled(flag);
        }
    
        protected void loadLayout() {
            JPanel buttonPanel = new JPanel(new GridLayout(1,3,3,3));
            JPanel fieldPanel = new JPanel(new GridLayout(3,3,3,3));
    
            buttonPanel.add(createButton);
            buttonPanel.add(editButton);
            buttonPanel.add(deleteButton);
    
            fieldPanel.add(lastnameLabel);
            fieldPanel.add(lastnameField);
            fieldPanel.add(firstnameLabel);
            fieldPanel.add(firstnameField);
            fieldPanel.add(middlenameLabel);
            fieldPanel.add(middlenameField);
    
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(nameScroll, BorderLayout.NORTH);
            getContentPane().add(fieldPanel, BorderLayout.CENTER);
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    
        }
        
    }
    Java Code:
    package demo2;
    
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    import java.util.Vector;
    
    public class Model extends View{
    
        private Controller database = new Controller();
        private commandListener listener = new commandListener();
        private int id = 0;
        private boolean edit = false;
        private Vector colNames = new Vector();
    
        public Model() {
            load();
            setVisible(true);
        }
    
        private void load() {
            loadTable();
            loadListener();
            loadLayout();        
        }
    
        private void loadTable() {
            Vector rowSet = database.loadTable();
            colNames.addElement(new String("ID"));
            colNames.addElement(new String("Last Name"));
            colNames.addElement(new String("First Name"));
            colNames.addElement(new String("Middle Name"));
            nameTable.setModel(new DefaultTableModel(rowSet,colNames));
        }
    
        private void loadListener() {
            createButton.addActionListener(listener);
            editButton.addActionListener(listener);
            deleteButton.addActionListener(listener);
            
            nameTable.setCellSelectionEnabled(true);
            ListSelectionModel cellSelection = nameTable.getSelectionModel();
            cellSelection.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            cellSelection.addListSelectionListener(new tableListener());
        }
    
        private void loadData(int index) {
            Vector data = (Vector)database.loadData(index);
            this.id = Integer.parseInt(data.elementAt(0).toString());
            lastnameField.setText(data.elementAt(1).toString());
            firstnameField.setText(data.elementAt(2).toString());
            middlenameField.setText(data.elementAt(3).toString());
        }
    
        private void refresh() {
            System.out.println(database.loadTable());
            Vector data = database.loadTable();
            nameTable.setModel(new DefaultTableModel(data, colNames));
        }
    
        private void switchBox() {        
            editButton.setEnabled(false);
            deleteButton.setEnabled(false);
        }
    
        private void editMode(boolean flag) {
            lastnameField.setEnabled(flag);
            firstnameField.setEnabled(flag);
            middlenameField.setEnabled(flag);
            if(!edit) {
                lastnameField.setText("");
                firstnameField.setText("");
                middlenameField.setText("");
            }
            if(flag) {
                deleteButton.setText("Cancel");
            } else {
                switchBox();
                createButton.setText("Create");
                editButton.setText("Edit");
                deleteButton.setText("Delete");
            }
        }
    
        private class commandListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                String action = event.getActionCommand();
                if(action.equals("Create")) {
                    edit = false;
                    editMode(true);
                    editButton.setEnabled(false);
                    deleteButton.setEnabled(true);
                    createButton.setText("Submit");
                } else if(action.equals("Edit")) {
                    edit = true;
                    editMode(true);
                    createButton.setEnabled(false);
                    editButton.setText("Submit");
                } else if(action.equals("Delete")) {
                    database.delete(id);
                    refresh();
                } else if(action.equals("Submit")) {
                    if(!edit) database.create(lastnameField.getText(), firstnameField.getText(), middlenameField.getText());
                    else database.edit(id, lastnameField.getText(), firstnameField.getText(), middlenameField.getText());
                    refresh();
                    edit = false;
                    editMode(false);
                } else if(action.equals("Cancel")) {
                    edit = false;
                    editMode(false);
                }
            }
        }
    
        private class tableListener implements ListSelectionListener {
            public void valueChanged(ListSelectionEvent event) {
                loadData(nameTable.getSelectedRow());
                editButton.setEnabled(true);
                deleteButton.setEnabled(true);
            }
        }
    
        public static void main(String[] args) {
            new Model();
        }
    }
    Java Code:
    package demo2;
    
    import java.util.Vector;
    
    public class Controller {
    
        public Vector data = new Vector();
    
        public Controller() {
            loadTempData();        
        }
    
        private void loadTempData() {
            Vector data1 = new Vector();
            Vector data2 = new Vector();
    
            data1.addElement(new Integer(1));
            data1.addElement(new String("LastName1"));
            data1.addElement(new String("FirstName1"));
            data1.addElement(new String("MiddleName1"));
    
            data2.addElement(new Integer(2));
            data2.addElement(new String("LastName2"));
            data2.addElement(new String("FirstName2"));
            data2.addElement(new String("MiddleName2"));
    
            data.addElement(data1);
            data.addElement(data2);
        }
    
        public void create(String lastname, String firstname, String middlename) {
            Vector data = new Vector();
            data.addElement(new Integer(newID()));
            data.addElement(new String(lastname));
            data.addElement(new String(firstname));
            data.addElement(new String(middlename));
            this.data.addElement(data);
        }
    
        public void edit(int id, String lastname, String firstname, String middlename) {
            Vector data = (Vector)this.data.elementAt(id-1);       
            data.set(1, new String(lastname));
            data.set(2, new String(firstname));
            data.set(3, new String(middlename));
            this.data.set(id, data);
        }
    
        public void delete(int id) {
            this.data.remove(id-1);
        }
    
        public int count() {
            return data.size();
        }
    
        private int newID() {
            Vector lastrow = (Vector)data.elementAt(data.size()-1);
            int id = Integer.parseInt(lastrow.elementAt(0).toString());
            return id+1;
        }
        
        public Vector loadData(int i) {
            Vector data = (Vector)this.data.elementAt(i);
            Vector result = new Vector();
            result.addElement(Integer.parseInt(data.elementAt(0).toString()));
            result.addElement(data.elementAt(1).toString());
            result.addElement(data.elementAt(2).toString());
            result.addElement(data.elementAt(3).toString());
            return result;
        }
    
        public Vector loadTable() {
            return data;
        }
    
        public static void main(String[] args) {
            new Controller();
        }
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Well obviously, I knew that they are different components, yet I thought it would work.
    Yes, it does work. That is the beauty of using models, once you understand it for one component the concept works for all components.

    You still have no idea what I SSCCE is and I will still not look at all the code. We don't care about your real program, only the concept you are trying to understand. It takes one line of code to create a table:

    JTable table = new JTable(3, 5);

    Then it will take two more lines to create a loop and assign an initial value to the first column:

    for (...)
    table.setValueAt(..);

    Now you want to update a value so you create an JButton with an ActionListener. That will be another 5 lines of code.

    So put the table and the button on a frame any you've got a SSCCE. Maybe 20-30 lines of code.

  8. #8
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    But I think debugging my code will only take 30minutes?
    Anyways, that's all I can for the SSCEE.
    Actually, the code is way shorter the original code (honestly speaking).

    Can you fix my code anyway?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  9. #9
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > But I think debugging my code will only take 30minutes?

    no-one gets 30 minutes of my time for free - 1 minute max.

    the compiler tells you what the error is, and the applicable line/s.

    start at that line, then follow the flow backwards, you'll find it.
    a simple error (actually should be obvious from the compiler message)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    We all have lives, work, and families that make demands on our time, and so we'll put more effort into helping someone who has put more effort into making it easier to help them. The bottom line is this: the shorter your compilable code, the greater the chance someone will take the time to look at it.

    Best of luck.

  11. #11
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Sorry for my statement.
    I am rushing the project that needs to be sumbitted

    Anyways. Thanks for the hint.
    I was under pressure to solve this thread fast.

    But anyways. The thread now is solved.
    Last edited by chyrl; 04-17-2010 at 05:32 AM.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. Update JTable data
    By DC200 in forum AWT / Swing
    Replies: 8
    Last Post: 03-30-2010, 10:10 PM
  2. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  3. Update the GUI in JTable
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-12-2009, 12:28 PM
  4. Replies: 6
    Last Post: 12-12-2008, 03:35 PM
  5. Replies: 2
    Last Post: 08-12-2008, 12:26 PM

Posting Permissions

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