How to use the JScrollPane in Java

风格不统一 提交于 2021-02-08 07:10:47

问题


How can I get the scroller around my JList component in the code given below? It doesn't seem to work properly :(

 public class JButtonO extends JFrame{

String[] values = {"henry", "Michael","Uche","John","Ullan","Nelly",
                              "Ime","Lekan","Austine","jussi","Ossi","Imam","Empo","Austine","Becky",
                             "Scholar","Ruth", "Anny"};

 public JButtonO()
{
   super("the button");
   this.setSize(400,200);
   JPanel panel =  new JPanel();
   JLabel label = new JLabel("Output Items:");
   label.setAlignmentX(1);
   label.setAlignmentY(1);
   JList conList = new JList(values);
   conList.setVisibleRowCount(3);
   JScrollPane scroller = new JScrollPane(conList);
   panel.add(label);
   panel.add(scroller);
   panel.add(conList);

   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.add(panel);
   this.setVisible(true);


}

回答1:


Look, I may not answering what you need, because I don´t remember to much of swing layout. I don´t work with it a long time ago...

But removing setting a layout (I remember) on your JPanel it works with this code:

public JButtonO() {
    super("the button");
    this.setSize(400, 200);

    // Create a panel with a borderlayout
    JPanel jpanel = new JPanel(new BorderLayout());

    JLabel label = new JLabel("Output Items:");
    label.setAlignmentX(1);
    label.setAlignmentY(1);
    // Add Label to top of layout
    jpanel.add(label, BorderLayout.NORTH);

    JList conList = new JList(values);
    conList.setVisibleRowCount(3);
    JScrollPane scroller = new JScrollPane(conList);
    //AddScroll to center
    jpanel.add(scroller);

    //Add Panel to JFrame
    this.add(jpanel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

  }

I think the problems is the default layoutmaneger of JPanel. Because of how it works your scroll was not "srink" enough to create scrolls...

Hope it helps, even without too much explanation...

ACTUALLY: After I post the answer I saw your mistake. Now I can explain what is wrong. You already added your JList inside your JScrollPane here:

JScrollPane scroller = new JScrollPane(conList);

But after that you put it inside the JPanel:

panel.add(conList);

this changes where yout JList will be displayed, and let the JScroll empty again. Without components it will be displayed with size 0x0 and will not be draw (even being there).

Now I think I helped =D




回答2:


Adding the JScrollPane scroller that includes the JList conList to the JPanel panel is enough. The mistake is that you are adding the JList a second time.

   JScrollPane scroller = new JScrollPane(conList);
   panel.add(label);
   panel.add(scroller);
   panel.add(conList); // <---THIS LINE SHOULD BE DELETED...



回答3:


The JScrollPane has settings called the scrollbar policies which say when the scrollbars are to be displayed. You can set them using JScrolPane(Component,int,int) constructor, or by calling setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy(). The default policies are "as needed", meaning the scrollbar is only displayed if the component is too large to display whole. So if your list fits inside the window, the scrolbars will not be visible, but will become visible when you e.g. make the window smaller using the mouse. You can change one or both policies to "always" using corresponding constants in order to make the scrollbar(s) always visible if that's what you need.



来源:https://stackoverflow.com/questions/9812980/how-to-use-the-jscrollpane-in-java

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