Question

    import java.sql.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import java.awt.*;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class Ui2 extends JFrame {
        JLabel l, l1;
        JFrame f;
        JPanel panel;
        JScrollPane scrollpane;
        int r;
        JList list;
        Ui2() {
            super("HADOOP GUI");
            panel = new JPanel();
            setContentPane(panel);
            panel.setSize(500, 500);
            SpringLayout x = new SpringLayout();
            panel.setLayout(x);
            DefaultListModel listmodel = new DefaultListModel();
            for (int i = 1; i <= 10; i++) {
                listmodel.addElement("Record" + i);
            }
            list = new JList(listmodel);

            list.setVisibleRowCount(5); // here is problem
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollpane = new JScrollPane(list);
            x.putConstraint(SpringLayout.WEST, scrollpane, 150, SpringLayout.WEST, panel);
            x.putConstraint(SpringLayout.NORTH, scrollpane, 30, SpringLayout.NORTH, panel);
            panel.add(scrollpane);
            Dimension d = list.getPreferredSize();
            d.width = 200;
            scrollpane.setPreferredSize(d);
            l1 = new JLabel("Default");
            x.putConstraint(SpringLayout.WEST, l1, 150, SpringLayout.WEST, panel);
            x.putConstraint(SpringLayout.NORTH, l1, 300, SpringLayout.NORTH, panel);
            panel.add(l1);
            list.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent arg0) {

                }

            });

        }
    }
Was it helpful?

Solution

It seems that the method: setVisibleRowCount(int) can't be used for JLists which are added to a JPanel which has set a Layoutmanager.

I rebuild your application a bit, since I couldn't get it working by using a SpringLayout (I admit, I never used it before). But with the BorderLayout it is working fine. You just have to wrap the Scrollbar, which contains the JList into a foreign JPanel for which you don't define a Layoutmanager. I think the reason is that the Layoutmanager will layout it components according to the available space, hence the setVisisbleRowCount will be ignored! But remember, components may be displayed differntly by using another LayoutManager. I personally suggest you to use the BorderLayout for simple arrangement (North, south, ... ), FlowLayout for a series of parallel components and the GridbagLayout for advanced arangements. This should be sufficient for almost all applications.

import java.awt.BorderLayout;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Ui2 extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 2464942276900547953L;
    private JLabel l1;
    private JPanel panel;
    private JScrollPane scrollpane;
    private JList<String> list;

    public Ui2() {
        super("HADOOP GUI");
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        setContentPane(panel);
        panel.setSize(500, 500);

        DefaultListModel<String> listmodel = new DefaultListModel<String>();
        for (int i = 1; i <= 10; i++) {
            listmodel.addElement("Record" + i);
        }

        list = new JList<String>(listmodel);
        list.setVisibleRowCount(5); // here is problem
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        scrollpane = new JScrollPane(list);

        JPanel p = new JPanel();
        p.add(scrollpane);
        panel.add(p, BorderLayout.CENTER);
        l1 = new JLabel("Default");
        panel.add(l1, BorderLayout.SOUTH);
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent arg0) {

            }

        });

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top