- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 set Row Header View for JScrollPane in Java?
Set Row Header View using the setRowHeaderView() method. Let us first create a KScrollPane and set a list −
List<String> myList = new ArrayList<>(10); for (int index = 0; index < 20; index++) { myList.add("List Item " + index); } final JList<String> list = new JList<String>(myList.toArray(new String[myList.size()])); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(list);
Now, set the row header view −
scrollPane.setRowHeaderView(new JLabel("All List Items "));
The following is an example to set row header view for JScrollPane in Java −
Example
package my; import java.awt.BorderLayout; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); List<String> myList = new ArrayListlt;>(10); for (int index = 0; index lt; 20; index++) { myList.add("List Item " + index); } final JListlt;String> list = new JListlt;String>(myList.toArray(new String[myList.size()])); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(list); scrollPane.setRowHeaderView(new JLabel("All List Items ")); list.setLayoutOrientation(JList.VERTICAL); panel.add(scrollPane); JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setSize(500, 250); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
Output
Advertisements