Solved

JTable in JScrollPane

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now