Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Scroll in JTable not Scrolling

Posted on 2003-03-23
9
Medium Priority
?
302 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
  • 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

580 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