-3

When I call the method updateTable() the table is displayed correctly but when I scroll the table horizontally some of the headers become distorted(wrong headers/headers overlap each other.).

I am using Netbeans what shall I do?

public void  updateTable(String query)
{        
   try
   {     
     String sql=query;            
     PreparedStatement pst=connectionVariable.prepareStatement(sql);
     ResultSet res=pst.executeQuery();               
     myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
     JScrollPane scrollpane = new JScrollPane(myTable);
     scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
     myTable.setModel(DbUtils.resultSetToTableModel(res));
     pst.close();
     res.close();
   }
   catch(Exception e) { JOptionPane.showMessageDialog(null,""+e+"","Error Updating Table",JOptionPane.ERROR_MESSAGE);  }
}

public loadTableAsPerSelection() 
{
  initComponents();
  String load_table="Select columnnames from tablenames";
  updateTable(load_table);
 }
Akki
  • 1,221
  • 3
  • 14
  • 33
  • 5
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) `catch(Exception e) { }` Don't ignore errors, change that to `catch(Exception e) { e.printStackTrace(); }` 3) `UpdateTable()` Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. 4) *"I know it is due to large number of columns."* How do you know that? – Andrew Thompson Jan 27 '13 at 12:50
  • If i specify less no. of columns upto 7 columns in select statement then this problem doesn't occurs,currently i am having 40 columns. – Akki Jan 27 '13 at 12:58
  • You noticing one out of 4 of the comments isn't bad, ..it's woeful. – Andrew Thompson Jan 27 '13 at 13:00
  • _I scroll the table horizontally_? How did you arrange [that](http://stackoverflow.com/q/2452694/230513)? – trashgod Jan 27 '13 at 13:47
  • I have enclosed the table in JScrollPane and set the `autoResizeMode=Off` @trashgod – Akki Jan 27 '13 at 17:03
  • @Akki: Include this in your [sscce](http://sscce.org/). – trashgod Jan 27 '13 at 18:10
  • Sorry for such a stupid question.The problem is being solved,don't know what was the error i just deleted my entire form in netbeans IDE and recreated it from scratch. – Akki Feb 03 '13 at 15:33

1 Answers1

3

OK, so here is an example with 40 columns and 2000 rows which works as expected:

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestTable2 {

    protected void initUI() {
        DefaultTableModel model = new DefaultTableModel();
        for (int i = 0; i < 40; i++) {
            model.addColumn("Col-" + (i + 1));
        }
        for (int i = 0; i < 2000; i++) {
            Vector<Object> row = new Vector<Object>();
            for (int j = 0; j < 40; j++) {
                row.add("Cell " + (i + 1) + "," + (j + 1));
            }
            model.addRow(row);
        }
        JTable table = new JTable(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JFrame frame = new JFrame(TestTable2.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollpane = new JScrollPane(table);
        scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        frame.add(scrollpane, BorderLayout.CENTER);
        frame.setSize(1000, 800);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable2().initUI();
            }
        });
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • +1 for a good counter-example. Would `pack()` be warranted before `setSize()`? – trashgod Jan 27 '13 at 18:12
  • @trashgod not sure to understand what you mean by "pack() be warranted before setSize()". If you call pack, eventually the JScrollPane will return a preferred size based on the preferredScrollableViewportSize which is 450, 400 by default. – Guillaume Polet Jan 27 '13 at 19:36
  • That makes sense; thanks for commenting. I habitually invoke `pack()` to catch other components, but in this example there's only the scroll pane & table. – trashgod Jan 28 '13 at 00:15
  • @GuillaumePolet This code works. But when I modified it as per my requirements to load the table contents using SQL query the same header problem occurs. – Akki Jan 28 '13 at 10:49
  • @Akki good - so you have to search your problem in the differences between yours and this :-) – kleopatra Jan 28 '13 at 11:06
  • i am trying to search this problem but not able to understand where i am going wrong. I have just edited the 1st `for loop` and everything else is same. The changes are as follow **ResultSetMetaData resMetaData=resSet.getMetaData(); stringColumnName=resMetaData.getColumnName(i+1); model.addColumn(columnname);** `resMetaData` is defined outside for loop – Akki Jan 28 '13 at 11:54
  • @akki try to post an [SSCCE](http://sscce.org) then – Guillaume Polet Jan 28 '13 at 14:57
  • @Akki I talked about an [SSCCE](http://sscce.org), not some part of your code where you think the problem is. I am pretty sure that the problem actually lies somewhere else in your code (maybe initComponents(), but that's just a wild guess). Btw, why do you initiate your columns only if you have results? – Guillaume Polet Jan 29 '13 at 09:40
  • @Akki, that is already the case. What are you trying to achieve? – Guillaume Polet Jan 29 '13 at 12:47
  • @GuillaumePolet I mean to say, creating a Jlabel for each column and using it as columnheader instead of using JTableheader – Akki Jan 29 '13 at 12:50
  • @GuillaumePolet My actual code is working dont know what was the error, I deleted my entire form and recreated it from scratch. – Akki Jan 29 '13 at 13:14
  • @Akki Why not using JTableHeader? What benefit would that provide? Besides, you could use your JLabel's and all but that would require to ensure correct alignement and all which would be a PITA. – Guillaume Polet Jan 29 '13 at 13:23
  • @GuillaumePolet I thought that JtableHeader was creating a problem in my case so i was thinking of using Jlabel's as JTableHeader. – Akki Jan 29 '13 at 13:40