- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display row count in Java Swing JList
Use JList getVisibleRowCount() method to display the row count in a JList:
String sports[]= {"Tennis", "Archery","Football","Fencing","Cricket","Squash","Hockey","Rugby"}; Jlist list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount);
The following is an example to display row count in JList:
Example
import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String sports[]= {"Tennis", Archery","Football","Fencing","Cricket","Squash","Hockey","Rugby"}; list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); frame.add(scrollPane); frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); frame.setSize(400,100); frame.setVisible(true); } }
Output
The output in the console will display the total row count:
Row Count = 8
- Related Articles
- How can we clear all selections in Java Swing JList?
- How to highlight a row in a table with Java Swing?
- How to display a value when select a JList item in Java?
- Java Program to append a row to a JTable in Java Swing
- How to add a new row to JTable with insertRow() in Java Swing
- Create JList and always display the scroll bar in Java?
- How to display the JList items from top to bottom and left to right in Java?
- How to add ScrollBar to JList in Java?
- Move the first row to the end of the JTable in Java Swing
- How to store string array in Java JList?
- How to display the count from distinct records in the same row with MySQL?
- How to add JList to Scroll pane in Java?
- How to maximize JFrame in Java Swing
- How to select the second index in Java JList?
- How to change Button Border in Java Swing

Advertisements