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

    Exclamation How to print JTable elements using DefaultTableModel

    Greetings,

    I have created a JTable with a DefaultTableModel class as its model.
    I wanted to print the elements of the JTable when the element has been updated. Is there a way to resolve this using DefaultTableModel as its model instead of AbstractTableModel?
    In my code below, the listselection doesn't work - I think.

    Here's the code of the implementation of the JTable

    Java Code:
    Object[][] detail = {{"", "", "", "", "", ""}, {"", "", "", "", "", ""}};
    String[] detailHeader = {"Item", "Price", "Qty", "Less", "Due", "Pay"};
    detailTable.setModel(new DefaultTableModel(detail, detailHeader) {
    	public boolean isCellEditable(int rowIndex, int columnIndex) {
               	boolean[] editable = {true, false, true, true, false, true};
               	return editable[columnIndex];
            }
    });
    Here's the listener add to the JTable:
    Java Code:
    detailTable.setCellSelectionEnabled(true);
    ListSelectionModel cellSelection = detailTable.getSelectionModel();
    cellSelection.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    cellSelection.addListSelectionListener(new DetailTableListener());
    Here's the DetailTableListener class
    Java Code:
    private class DetailTableListener implements ListSelectionListener {
    		
    	int rowCount = detailTable.getModel().getRowCount();
    	int columnCount = detailTable.getModel().getColumnCount();
    		
    	@Override
    	public void valueChanged(ListSelectionEvent event) {
    		for (int row = 0; row < rowCount; row++) {
    			for (int column = 0; column < columnCount; column++) {
    				System.out.println(detailTable.getValueAt(row, column) + ", ");					
    			}
    			System.out.println();
    		}
    	}
    }
    Thanks.
    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
    25

    Default

    If you're listening for changes to the table model, not selections, would you be better off using a TableModelListener added to the model rather than a ListSelectionListener?

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

    Default

    Thanks for the fast reply,

    How would I set the TableModelListener to the JTable?
    I should change the ListSelectionListener implemented on the DetailTableListener to a TableModelListener?
    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
    25

    Default

    Quote Originally Posted by chyrl View Post
    How would I set the TableModelListener to the JTable?
    You add the listener to the table's model. So either you have a reference to the model or you get the model from the JTable via the getModel() method.

    I should change the ListSelectionListener implemented on the DetailTableListener to a TableModelListener?
    As far as I understand it, a ListSelectionListener is used when you want to listen for changes in row selection while a TableModelListener is used for changes in the model's data via insertion, deletion or update. Which you use depends on what you are listening for.

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

    Default

    I see.
    But I don't know how the listener works with the JTable.
    I am having an error on line 4 of this code.
    I have change the implemented interface of DetailTableListener to TableModelListener.

    Java Code:
    1   detailTable.setCellSelectionEnabled(true);
    2   ListSelectionModel cellSelection = detailTable.getSelectionModel();
    3   cellSelection.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);       
    4   cellSelection.addListSelectionListener(new DetailTableListener());
    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

    I may have solve the listening implementation to the JTable using this code
    Java Code:
    detailTable.setCellSelectionEnabled(true);
    detailTable.getModel().addTableModelListener(new DetailTableListener());
    And the DetailTableListener class:
    Java Code:
    private class DetailTableListener implements TableModelListener {
    		
    	int rowCount = detailTable.getModel().getRowCount();
    	int columnCount = detailTable.getModel().getColumnCount();				
    
    	@Override
    	public void tableChanged(TableModelEvent event) {
    		for (int row = 0; row < rowCount; row++) {
    			for (int column = 0; column < columnCount; column++) {
    				System.out.println(detailTable.getValueAt(row, column) + ", ");					
    			}
    			System.out.println();
    		}			
    	}
    }
    But still it didn't work. Have I missed something?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    To get better help sooner, post a SSCCE
    SSCCE : Java Glossary

    db

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

    Default

    Topic solved,
    I have re-initialized the TableModelListener into the JTable after re-setting the model of the JTable.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. Replies: 2
    Last Post: 02-07-2011, 03:01 PM
  2. DefaultTableModel vs AbstractTableModel
    By chyrl in forum AWT / Swing
    Replies: 4
    Last Post: 05-02-2010, 12:10 PM
  3. DefaultTableModel get the updated values
    By stylian in forum AWT / Swing
    Replies: 3
    Last Post: 11-01-2009, 12:26 AM
  4. DefaultTableModel problem
    By stylian in forum AWT / Swing
    Replies: 2
    Last Post: 10-31-2009, 04:23 PM
  5. RequestFocus on DefaultTableModel Particular Cell
    By yernikumar in forum AWT / Swing
    Replies: 1
    Last Post: 03-01-2009, 04:58 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
  •