Java Swing How to - Create JTabbedPane within JScrollPane








Question

We would like to know how to create JTabbedPane within JScrollPane.

Answer

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
// w  ww.ja v a 2  s  . c om
public class Main {

  public static void main(String[] args) {
    int MAX_CELLS = 30;

    DefaultListModel<String> listModel = new DefaultListModel<>();
    JList<String> myList = new JList<>(listModel);
    myList.setVisibleRowCount(8);

    for (int i = 0; i < MAX_CELLS; i++) {
      listModel
          .addElement("label "+ i);
    }

    JTabbedPane jTabbedPane = new JTabbedPane();

    jTabbedPane.add("Test", new JScrollPane(myList));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(jTabbedPane);
    frame.pack();
    frame.setVisible(true);
  }
}