Zhou
Zhou

Reputation: 771

How can I know the number of rows can be shown in a JList?

I want to use a JList to display records retrieved from a database.

Because a database table may have a large number of records, paging is better than scroll bar, which means to put 4 buttons "First", "Prev", "Next", "Last" below the list.

To implement paging, I must know how many records I should request from the database for one page. When the window is resized by the user, I also need to recalculate the "page size" and access the database again.

The question is, how can I know how many rows can be completely displayed in the JList control? (suppose a row to be displayed in the JList is just a simple text line. Rows are identical in height).

Thank you!

Upvotes: 1

Views: 189

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You can display as many rows as needed in the JList. Simply

  • Put the JList into a JScrollPane, and add the JScrollPane into the GUI
  • Set the JList's visible row property: .setVisibleRowCount(...)
  • Set its .setPrototypeCellValue(...) to a value that shows an adequate width
  • Do not set its width, height, size, preferred size or bounds

Regarding:

Because a database table may have a large number of records, paging is better than scroll bar

But that is not how JLists have been built to work, and so you will be fighting against the GUI library design if you try to implement this in the GUI directly. Perhaps better to implement a paging construct on the database data acquisition side of things.

Upvotes: 1

Related Questions