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 9 of 9

Thread: JTable problem

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JTable problem

    When i create a JTable in this way:

    JTable tab = new JTable(a,b);

    where a is an String [][] and b String[]

    The problem is set the TableModel, when i insert this line:

    tab.setModel(new TabellaDati(a,b));

    The table appears empty

    This is the Table Model
    public class DataTable extends TableModel{
        public static final int N_COL = 8;
        public static final int N_RIG = 20;
        private String[] heading;
        private String[][] data;
     
        public DataTable (String[] h, String[][] data){
            this.heading = h;
            this.data = data;
        }
        @Override
        public boolean isCellEditable(int rowIndex, int columnsIndex){
            return (rowIndex == 0) ? true : false;
        }
     
        public void removeTableModelListerner(TableModelListener s){
     
        }
     
        @Override
        public int getRowCount() {
            return data.length;
        }
     
        @Override
        public int getColumnCount() {
           return heading.length;
        }
     
        @Override
        public String getColumnName(int columnIndex) {
            return heading[columnIndex];
        }
     
        @Override
        public Class<?> getColumnClass(int columnIndex) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return data[rowIndex][columnIndex];
        }
     
        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
             //To change body of generated methods, choose Tools | Templates.
            data[rowIndex][columnIndex] =(String) aValue;
        }
     
        @Override
        public void addTableModelListener(TableModelListener l) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
     
        }
     
        @Override
        public void removeTableModelListener(TableModelListener l) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: JTable problem

    It might help to post a simple example that compiles and illustrates the problem.

    ---

    private String[] heading;
    private String[][] data;
     
    public DataTable (String[] h, String[][] data){
        this.heading = h;
        this.data = data;
    }

    This code from the model could cause problems. The constructor sets data to the reference that is sent as an argument. Other methods will access this data array. But what if the caller of the constructor does something to the contents of the data array using its own reference to the array? Then the table's data will not "persist".

    I don't know if it's related to what you're (not) seeing, but it might be a good idea to copy the data from the arrays used as arguments into newly created arrays in the model. (Also, that way you would actually be using the static final int values for some purpose.)

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable problem

    Ok, i found the error, the method public Class<?> getColumnClass(int columnIndex) was not implemented (throw new UnsupportedOperationException("Not supported yet."); ) but the program compiled and the table showed anyway.

    Once i had implement the method getColumnClass:
    public Class<?> getColumnClass(int columnIndex) {
             for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, columnIndex);
     
                    if (o != null)
                    {
                        return o.getClass();
                    }
                }
     
                return Object.class;
        }

    The table finally shows the data in it.
    So it seems that first the program shows the table and only after fills it with data.

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable problem

    I got another question, i have to implement a Event Listener that catch the event of a edited cell in the tab.
    Where and how i can implements this?

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: JTable problem

    Listening for changes (strictly speaking, changes in the underlying data model not it's visual representation: the table cell) is covered in the Listening for Data Changes section of "How to Use Tables" in Oracle's Tutorial.

    The section following that one, describes how to organise things so that the model fires such events. As mentioned there, you get that behaviour for free when you use a DefaultTable but in your case (a TableModel subclass) they suggest subclassing AbstractTableModel.

    They illustrate this approach in the TableDemo.java example. Notice how the table model is a subclass of AbstractTabkeModel whose setValueAt() fires events when the table's contents change. Actually responding to such changes is illustrated in the "Listening for Data Changes" section itself.

    ---

    Because of the many-way split between model and view (data, selection, how cells are rendered vs edited, etc) tables are a rather complex example of a gui component. (the most complex of the standard ones?). You might find it useful to go through the whole "How to Use Tables" page and see how they build up each element of the complexity in their example.

  6. #6
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable problem

    My table model implements:
    public boolean isCellEditable(int rowIndex, int columnsIndex);
    public void removeTableModelListerner(TableModelListener s);
    public void removeTableModelListerner(TableModelListener s);
    public int getRowCount();
    public int getColumnCount();
    public Class<?> getColumnClass(int columnIndex);
    public Object getValueAt(int rowIndex, int columnIndex);
    public void setValueAt(Object aValue, int rowIndex, int columnIndex);
    public void addTableModelListener(TableModelListener l);
    private void fireTableCellUpdated(int rowIndex, int columnIndex);

    the problem that i have with this jtable is that when i write something inside the table, the method fireTableCellUpdated is called, this method get a new matrix and overwrite the data[][] of the table,
    but the table do not change, is not refreshed

    Write inside a cell -> call fireTableCellUpdated -> get new data to fill the table -> new data in not showed, the table remains the same

    But when i move a side or i click on a certain cell, the table is refreshed and the new data is visible.

    What could be the problem?

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: JTable problem

    Check that you are invoking fireTableCellUpdated() with the right arguments.

    If you continue to have problems post a small example that illustrates the problem. It needn't have all the rest of your program logic, but should compile and show the table not visually keeping up with the new data.

  8. #8
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable problem

    public class MyTable extends TableModel{
        public static final int C = 12;
        public static final int R = 25;
        public String[][] data;
     
        public MyTable(String[] h, String[][] data) {
            this.data = data;
        }
     
        public boolean isCellEditable(int rowIndex, int columnsIndex) {
                 if (columnsIndex == 0) 
                 return true;
             else 
                return false;      
        }
     
        public void removeTableModelListerner(TableModelListener s) {
        }
     
        public int getRowCount() {
            return data.length;
        }
        public int getColumnCount() {
            return heading.length;
        }
     
        public Class<?> getColumnClass(int columnIndex) {
            for (int row = 0; row < getRowCount(); row++) {
                Object o = getValueAt(row, columnIndex);
     
                if (o != null) {
                    return o.getClass();
                }
            }
     
            return Object.class;
        }
     
        public Object getValueAt(int rowIndex, int columnIndex) {
            return data[rowIndex][columnIndex];
        }
     
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
     
            data[rowIndex][columnIndex] = (String) aValue;
            try {
                fireTableCellUpdated(rowIndex, columnIndex);
            } catch (TException ex) {
                Logger.getLogger(TabellaDati.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
     
        public void addTableModelListener(TableModelListener l) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
     
        public void removeTableModelListener(TableModelListener l) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        private void fireTableCellUpdated(int rowIndex, int columnIndex){
     
                    String[][] newMatrix = Matrix.getMatrix();
                    for(int i=0,j=0; i<N_RIG; i++,j++)
                        System.arraycopy(newMatrix[j], 0, data[i], 0, data[i].length);
       }   
    }

    in fireTableCellUpdated a new matrix is created and overwrite data[][]

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable problem

    up

Similar Threads

  1. JTable Problem
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 27th, 2013, 01:23 PM
  2. Problem with JTable and MySQL
    By mija in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 17th, 2012, 03:29 PM
  3. problem with JTable
    By deependeroracle in forum AWT / Java Swing
    Replies: 3
    Last Post: March 17th, 2012, 09:41 AM
  4. [SOLVED] JTable Problem
    By aussiemcgr in forum Java Theory & Questions
    Replies: 0
    Last Post: October 15th, 2010, 04:33 PM
  5. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 02:06 AM