java 向 JList 添加滚动条,JList 被添加到 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10052621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:24:13  来源:igfitidea点击:

Adding a scrollbar to JList , JList being added to a JPanel

javaswingjpaneljscrollpane

提问by gauravHI5

i am having a list which i want to populate dynamically , hence scrollbar is required. I have added a scrollbar to list. The problem is that when i try to add the list to a panel. the scrollbar becomes visible on the list but they dont work even when the list's element gets bigger in size.

我有一个我想动态填充的列表,因此需要滚动条。我在列表中添加了一个滚动条。问题是当我尝试将列表添加到面板时。滚动条在列表中可见,但即使列表元素变大,它们也不起作用。

JPanel p4=new JPanel();
Container c=getContentPane();
myList=new JList(model);
myList.setVisibleRowCount(5);
myList.setFixedCellWidth(200);

p4.add(new JScrollPane(myList,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS));
c.add(p4);

回答by Robin

Works just fine here. I used the SplitPaneDemo.javafile for this SSCCE, and stripped away all the unnecessary stuff

在这里工作得很好。我为此SSCCE使用了SplitPaneDemo.java文件,并去除了所有不必要的东西

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import java.awt.BorderLayout;

public class SplitPaneDemo extends JPanel {
  private JList<String> list;
  private String[] imageNames = { "Bird", "Cat", "Dog", "Rabbit", "Pig", "dukeWaveRed",
      "kathyCosmo", "lainesTongue", "left", "middle", "right", "stickerface"};
  public SplitPaneDemo() {
    setLayout( new BorderLayout(  ) );

    list = new JList<>(imageNames);
    list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    list.setSelectedIndex( 0 );

    add( new JScrollPane(list) );
  }

  /**
   * Create the GUI and show it.  For thread safety,
   * this method should be invoked from the
   * event-dispatching thread.
   */
  private static void createAndShowGUI() {

    //Create and set up the window.
    JFrame frame = new JFrame("SplitPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SplitPaneDemo splitPaneDemo = new SplitPaneDemo();
    frame.getContentPane().add(splitPaneDemo);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        createAndShowGUI();
      }
    });
  }
}

回答by Richante

Similar question here: here

类似的问题在这里:here

I don't think the answer is the same in your case though. Maybe the problem happens when you add stuff to the list; you could try doing:

不过,我认为答案与您的情况不同。也许当您向列表中添加内容时会出现问题;你可以尝试这样做:

myList.revalidate();

after it changes.

改变之后。