Solved

JTable in JScrollPane

Posted on 2002-05-10
2
1,045 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
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 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