Solved

JTable not refreshing

Posted on 2003-11-18
15
753 Views
Last Modified: 2008-02-01
Hi

I have a JTable and whenever I update it, it doesnt refresh properly. Also Initially it does not display and I have to click somewhere on the table in order for it to show up.

Anyone have any ideas?
0
Comment
Question by:illucid
  • 9
  • 5
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9775997
Please post code
0
 
LVL 92

Expert Comment

by:objects
ID: 9776066
You need to ensure your model fires the appropriate TableModelEvent when it is updated.
AbstractTableModel provides methods for firing events.
0
 

Author Comment

by:illucid
ID: 9776110
Ive nested the JTable inside a JPanel then added the JPanel

public class mytable2 extends JPanel {
    private boolean DEBUG = true;
      final LinkedList[] ldata;
      final int len; //variable for how many rows...
      lsearch2 lsch2;
      MyTableModel myModel;
      public JTable table;
      

  public mytable2(LinkedList[] a, int lnth, lsearch2 lschh) {
            super(new BorderLayout());
            ldata = a;
            len = lnth;
            lsch2 = lschh;
        myModel = new MyTableModel();
        table = new JTable(myModel);
        myModel.fireTableDataChanged();

      table.setPreferredScrollableViewportSize(new Dimension(500, 70));
             JScrollPane scrollPane = new JScrollPane(table);
             add(scrollPane, BorderLayout.CENTER);
 
               ...

0
 
LVL 92

Expert Comment

by:objects
ID: 9776115
how do you update it?
0
 
LVL 92

Expert Comment

by:objects
ID: 9776126
> myModel.fireTableDataChanged();

Thats not necessary there.

You just need to fire an event when a change is made to the mode, and it's probablt also best done from within your table model.
0
 

Author Comment

by:illucid
ID: 9776191
heres the rest of it..      


      class MyTableModel extends AbstractTableModel {
            final String[] columnNames = {"cust_id", "cust_contact", "business_name", "cust_email", "Select?"};

        public int getColumnCount() {
            return columnNames.length;
        }
     
        public int getRowCount() {
            return len;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return ldata[col].get(row);
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            if (col < 1) {
                return false;
            } else {
                return true;
            }
        }

            public void setValueAt(Object value, int row, int col) {
                  if (DEBUG) {
                        //System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")");
                  }
                  ldata[col].remove(row);
                  ldata[col].add(row, value);
                  //fireTableCellUpdated(row, col);
            if (DEBUG) {
                printDebugData();
                ud(row, col);
            }
        }
       
        public void fire() {
                  if (DEBUG) {
                        //System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")");
                  }
                  Object temp = ldata[1].get(0);
                  ldata[1].remove(0);
                  ldata[1].add(0, temp);
                  fireTableCellUpdated(0, 1);
            if (DEBUG) {
                printDebugData();
                //ud(row, col);
            }
        }
       
        private void ud(int row, int col) {
              if (col != 4) {
                    lsch2.update("UPDATE customer SET " + getColumnName(col) + " = '" + ldata[col].get(row) + "' WHERE cust_id = '" + ldata[0].get(row) + "';");       
                        lsch2.loadselected((String)ldata[0].get(row));
                        ldata[4].remove(row);
                        ldata[4].add(0, new Boolean(true));
                        fireTableCellUpdated(row, 4);
                  }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

         for (int i=0; i < numRows; i++) {
                //System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    //System.out.print("  " + ldata[j].get(i));
                }
                //System.out.println();
            }
//            System.out.println("--------------------------");
        }
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9776210
>  //fireTableCellUpdated(row, col);

why is this commented out?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 9776216
Looks perhaps like you update ladat outside of MyTableModel. If you are doing this then you need to ensure you fire the appropriate event.
0
 
LVL 92

Expert Comment

by:objects
ID: 9776221
Can you post the code that actually adds data to the table.
0
 
LVL 92

Expert Comment

by:objects
ID: 9776228
> ldata[col].remove(row);
> ldata[col].add(row, value);

More efficient to do:

ldata[col].set(row, value);
0
 

Author Comment

by:illucid
ID: 9776233
yup that fixed it.. =)

Its still not showing initially tho =(
0
 

Author Comment

by:illucid
ID: 9776243
     private void getnewmytable2() {
            
            //System.out.println("accessing info from lsch");
            LinkedList[] d = lsch2.getobjects();

            if (d != null && lsch2.getnumber() != -1) {
                  //System.out.println("d not null");
                  if (tables.get(0) != null) {
                        remove((mytable2)tables.get(0));
                        repaint();
                        tables.remove(0);
                  }
                  final mytable2 mt = new mytable2(d, lsch2.getnumber(), lsch2);
                  tables.add(0, mt);
                  add((mytable2)tables.get(0), BorderLayout.CENTER);

                  mt.show();  //??
                  mt.repaint(); //??
                  mt.myModel.fireTableDataChanged();
                  //mt.table.selectAll(); //??
                  //mt.myModel.fire();  //??
                  //mt.table.setValueAt(mt.table.getValueAt(0,1), 0, 1); //??
            
                  
            }
            else {
                  //System.out.println("not getting d from lsch");
                  JOptionPane.showMessageDialog(null, "");
            }

      }      
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9776257
Try:

...
add((mytable2)tables.get(0), BorderLayout.CENTER);
revalidate();
0
 

Author Comment

by:illucid
ID: 9776263
Thank you!
0
 
LVL 92

Expert Comment

by:objects
ID: 9776270
No worries ;-)

http://www.objects.com.au
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 81
advertisement module in core php 4 144
javap bin 2 24
configure dependency in POM for new database 3 11
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now