Solved

JTable in JScrollPane

Posted on 2002-05-10
2
1,047 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 100 total points
ID: 7028981
Hi Sean.

Are You still there ?

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



0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

734 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