Create and use JList, fill string value to JList - Java Swing

Java examples for Swing:JList

Description

Create and use JList, fill string value to JList

Demo Code

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main extends JFrame {
  String[] subs = { "A", "B", "C","D", "E", "F", "G", "H", "I","J" };
  JList<String> subList = new JList<>(subs);

  public Main() {
    super("Subscriptions");
    setSize(150, 335);//from   w  w  w.  j  a v a 2  s  .  com
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    JLabel subLabel = new JLabel("RSS Subscriptions:");
    panel.add(subLabel);
    subList.setVisibleRowCount(8);
    JScrollPane scroller = new JScrollPane(subList);
    panel.add(scroller);
    add(panel);
    setVisible(true);
  }

  public static void main(String[] arguments) {

    Main app = new Main();
  }
}

Related Tutorials