?
Solved

JTable in JScrollPane

Posted on 2002-05-10
2
Medium Priority
?
1,049 Views
Last Modified: 2008-02-01
Hi, I am using Java 1.4 swing package.
How can I make sure that a constant number of rows
(let's say 10) would be visible in the JTable?
I also need to set vertical scroll bar visible all the
time.

Thanks.
Sean
0
Comment
Question by:xzhang4
[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
  • 2
2 Comments
 
LVL 1

Expert Comment

by:MirecXP
ID: 7003159
Hi Sean,

You must create Your own class inherited from JTable and then override method getPreferredScrollableViewportSize(), which returns preffered size of scrollable object. Typically it returns getPrefferedSize of component, so you must return your size. In this case we can return height of 10 rows.

Here is the class code:

class MyTable extends JTable {

  int rowsVisible = 10;

  MyTable (Object[][] rowData, Object[] columnNames) {
    super(rowData, columnNames);
  }

  public Dimension getPreferredScrollableViewportSize() {
    Dimension d = super.getPreferredScrollableViewportSize();
    d.height = rowsVisible * getRowHeight();
    return d;
  }

  public void setRowsVisible(int r) {
    rowsVisible = r;
  }

  public int getRowsVisible() {
    return rowsVisible;
  }

}


Now you can use it:
     ...
    Object[] myOptions = {
      "1","2","3"," 4"," 5"," 6"," 7"," 8"," 9"," 10",
      "11","12"," 13"," 14"," 15"," 16"," 17"," 18"," 19"," 20",
    };

    MyTable t = new MyTable(new Object[][] {
        {"1a", "1b"},
        {"2a", "2b"},
        {"3a", "3b"},
        {"4a", "4b"},
        {"5a", "5b"},
        {"6a", "6b"},
        {"7a", "7b"},
        {"8a", "8b"},
        {"9a", "9b"},
        {"10a", "10b"},
        {"11a", "11b"},
        {"12a", "12b"},
        {"13a", "13b"},
        {"14a", "14b"},
        {"15a", "15b"},
      }, new Object[] {"COL a", "COL b"}
    );

    //force vertical scrollbar using JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
    JScrollPane sp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sp.getViewport().add(t);
     ..
    insert sp into some panel...

I'm sure this helps.

Mirec
0
 
LVL 1

Accepted Solution

by:
MirecXP earned 300 total points
ID: 7028981
Hi Sean.

Are You still there ?

I think I've answered Your question, haven't I ?



0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

765 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