Scroll in JTable not Scrolling

I have a simple JTable whom i give Vectors as cols, and rows.
I have a Container con = getContentPane(); // JFrame container
I have a JPanel panel = new JPanel();
i add the JTable to the panel as: panel.add(new JScrollPane(table));
this panel is the add to the container as con.add(panel, BorderLayout.CENTER);

i then pack() the JFrame and show() it.

Every thing looks good, there is the ScrollBar too. However when I resize the JFrame to a smaller size, the scrollBars also resize.

The screen shot is located here of the problem i mean:
[URL] [/URL]
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.

Try adding the scroll pane directly to the content pane instead:

con.add(new JScrollPane(table), BorderLayout.CENTER);
Mayank SAssociate Director - Product EngineeringCommented:
Try adding the JTable to a JScrollPane and add the JScrollPane to the Container aS:

JScrollPane scrollPane = new JScrollPane ( table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS ) ;
con.add ( scrollPane ) ;


That's basically what I have suggested.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Mayank SAssociate Director - Product EngineeringCommented:
Ya.... almost. I just specified the scrollbars too.

glottisAuthor Commented:
Thanks for the help, but i found out my problem.

somehow the JTable was not correctly setting the TableColumn widths, i played around a little and did some work.

Now no matter in how many containers, panel i add the JTable everything is working perfectly.
Please post your code.
glottisAuthor Commented:
// my customa made JTable
public class JTables extends JTable {
  public JTables(Vector<String> cols,Vector<Vector> rows){
    tableModel = new JTablesModel(this, cols, rows);
    tableRender = new JCellRenders();
  private void setSize(Vector<String> cols) {
    int total = cols.size();
    TableColumnModel tcModel = getColumnModel();
    FontMetrics fm = getFontMetrics(getFont());
    for (int i=0; i<total; i++) {
      TableColumn tc = tcModel.getColumn(i);
        int length = fm.stringWidth(cols.elementAt(i))+10;
  public JScrollPane getScrollPane() {
    JScrollPane scrollTable = new JScrollPane(this); {
      Dimension tableDim = getPreferredSize();
      int w = tableDim.width + 3;
      int h = 64;
      Dimension scrolDim = new Dimension(w, h);
    return scrollTable;

// in main application:
Container con = getContentPane();
JPanel topPanel = new JPanel(new BorderLayout());
con.add(topPanel, BorderLayout.NORTH);
topPanel.add(getTopPanel(), BorderLayout.NORTH);
topPanel.add(getBotPanel(), BorderLayout.SOUTH);
pack(); show(); setDefaultCloseOperation(3);

private JPanel getTopPanel() {
  JPanel panel = new JPanel(new BorderLayout());
  panel.add(getTable(), BorderLayout.SOUTH);
  return panel;

private JComponent getTable() {
  Vector<String> columns = new Vector<String>(); {
    // add all the column names in the following format
  Vector<Vector> rows = new Vector<Vector>(); {
    execute a query and populate rows
  JTables table = new JTables(columns, rows);
  return table.getScrollPane();
Dear expert(s),

A request has been made to close this Q in CS:

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.


Community Support Moderator
Experts Exchange
Saving this Q as a PAQ and refunding the 50 points to the questionner


Community Support Moderator
Experts Exchange

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
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
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.