Adding a ScrollBar to JList in Swing

心不动则不痛 提交于 2019-12-12 04:53:14

问题


I'm currently dealing with Swing based desktop application in Java. I have come across this problem below.

I have been trying to add scrollbars into my ListArea (JList type) but I couldn't do so albeit so many things there were I intended. Here is the code snippet.. I'm writing this code in MainFrame extending JFrame. Any help would be appreciated. Thanks...

super(title)
Jpanel panel = new Panel()
panel(add)
panel.setlayout(null)

final JList<String> listArea = new JList<String>(labels);
    listArea.setBounds(50, 180, 700, 300);
    listArea.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listArea.setFont(new Font("Serif", Font.ITALIC, 14));
    listArea.setVisibleRowCount(-1);
    JScrollPane listScroller = new JScrollPane();
    listScroller.setViewportView(listArea);
    listArea.setLayoutOrientation(JList.VERTICAL);
    panel.add(listArea); 
    panel.add(listScroller);

回答1:


Get rid of panel.add(listArea);, it's removing the listArea from the JScrollPane

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify.

JScrollPane (and JViewport) have there own layout routines, which you don't control. Setting the size of the JList will have no effect once you pass it to the JScrollPane.

See Why is it frowned upon to use a null layout in SWING? for more details

I don't know what you expect listArea.setVisibleRowCount(-1); to do, but I'd advise against it.

Updated with example

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestList {

    public static void main(String[] args) {
        new TestList();
    }

    public TestList() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JPanel panel = new JPanel(new BorderLayout());

                List<String> labels = new ArrayList<>(25);
                for (int index = 0; index < 100; index++) {
                    labels.add("Item " + index);
                }

                final JList<String> listArea = new JList<String>(labels.toArray(new String[labels.size()]));
                listArea.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                listArea.setFont(new Font("Serif", Font.ITALIC, 14));
                JScrollPane listScroller = new JScrollPane();
                listScroller.setViewportView(listArea);
                listArea.setLayoutOrientation(JList.VERTICAL);
                panel.add(listScroller);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}


来源:https://stackoverflow.com/questions/26621349/adding-a-scrollbar-to-jlist-in-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!