?
Solved

Scroll in JTable not Scrolling

Posted on 2003-03-23
9
Medium Priority
?
300 Views
Last Modified: 2013-11-23
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] http://www.geocities.com/aloochannay/ [/URL]
0
Comment
Question by:glottis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 8193160
Try adding the scroll pane directly to the content pane instead:

con.add(new JScrollPane(table), BorderLayout.CENTER);
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8193307
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 ) ;

Mayank.


0
 
LVL 9

Expert Comment

by:yongsing
ID: 8193338
That's basically what I have suggested.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8193409
Ya.... almost. I just specified the scrollbars too.

Mayank.
0
 
LVL 2

Author Comment

by:glottis
ID: 8194742
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.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 8195556
Please post your code.
0
 
LVL 2

Author Comment

by:glottis
ID: 8200243
// my customa made JTable
public class JTables extends JTable {
  public JTables(Vector<String> cols,Vector<Vector> rows){
    super();
    tableModel = new JTablesModel(this, cols, rows);
    tableRender = new JCellRenders();
   
    setModel(tableModel);
    setRenderers(tableRender);
    setAutoResizeMode(AUTO_RESIZE_OFF);
    setSize(cols);
  }
 
  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;
        tc.setWidth(length);
        tc.setPreferredWidth(length);
      }
    }
  }
 
  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);
      scrollTable.setPreferredSize(scrolDim);
    }
    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
    columns.addElement("");
  }
 
  Vector<Vector> rows = new Vector<Vector>(); {
    execute a query and populate rows
  }
  JTables table = new JTables(columns, rows);
  return table.getScrollPane();
}
0
 

Expert Comment

by:modulo
ID: 8356014
Dear expert(s),

A request has been made to close this Q in CS:
http://www.experts-exchange.com/Community_Support/CleanUp/Q_20588986.html

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.

modulo

Community Support Moderator
Experts Exchange
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 8365684
Saving this Q as a PAQ and refunding the 50 points to the questionner

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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