JTable not refreshing


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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Please post code
Mick BarryJava DeveloperCommented:
You need to ensure your model fires the appropriate TableModelEvent when it is updated.
AbstractTableModel provides methods for firing events.
illucidAuthor Commented:
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);

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

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Mick BarryJava DeveloperCommented:
how do you update it?
Mick BarryJava DeveloperCommented:
> 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.
illucidAuthor Commented:
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].add(row, value);
                  //fireTableCellUpdated(row, col);
            if (DEBUG) {
                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].add(0, temp);
                  fireTableCellUpdated(0, 1);
            if (DEBUG) {
                //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) + "';");       
                        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("--------------------------");
Mick BarryJava DeveloperCommented:
>  //fireTableCellUpdated(row, col);

why is this commented out?
Mick BarryJava DeveloperCommented:
Looks perhaps like you update ladat outside of MyTableModel. If you are doing this then you need to ensure you fire the appropriate event.
Mick BarryJava DeveloperCommented:
Can you post the code that actually adds data to the table.
Mick BarryJava DeveloperCommented:
> ldata[col].remove(row);
> ldata[col].add(row, value);

More efficient to do:

ldata[col].set(row, value);
illucidAuthor Commented:
yup that fixed it.. =)

Its still not showing initially tho =(
illucidAuthor Commented:
     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) {
                  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.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, "");

Mick BarryJava DeveloperCommented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
illucidAuthor Commented:
Thank you!
Mick BarryJava DeveloperCommented:
No worries ;-)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.