SizingSamples.java Source code

Java tutorial

Introduction

Here is the source code for SizingSamples.java

Source

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class SizingSamples {
    public static void main(String args[]) {
        String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };

        JFrame frame = new JFrame("Sizing Samples");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();

        JList jlist1 = new JList(labels);
        jlist1.setVisibleRowCount(4);
        DefaultListModel model = new DefaultListModel();
        model.ensureCapacity(1000);
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 10; j++) {
                model.addElement(labels[j]);
            }
        }
        JScrollPane scrollPane1 = new JScrollPane(jlist1);
        contentPane.add(scrollPane1, BorderLayout.NORTH);

        JList jlist2 = new JList(model);
        jlist2.setVisibleRowCount(4);
        jlist2.setFixedCellHeight(12);
        jlist2.setFixedCellWidth(200);
        JScrollPane scrollPane2 = new JScrollPane(jlist2);
        contentPane.add(scrollPane2, BorderLayout.CENTER);

        JList jlist3 = new JList(labels);
        jlist3.setVisibleRowCount(4);
        contentPane.add(jlist3, BorderLayout.SOUTH);

        frame.setSize(300, 350);
        frame.setVisible(true);
    }
}