How to set Row Header View for JScrollPane in Java?

Java 8Object Oriented ProgrammingProgramming

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


raja
Published on 04-Jun-2019 07:42:41
Advertisements