简体   繁体   English

使用带有Vector的JList不打印任何内容

[英]using JList with Vector not printing anything

I have the following class ReviewPanel in which i have created a JList and trying to populate it with Vector passed to it. 我有以下类ReviewPanel ,其中我创建了一个JList并尝试使用传递给它的Vector填充它。 the vector already has its values filled but when i add it to JList it doesn't shows anything. 向量已经填充了它的值但是当我将它添加到JList时它没有显示任何内容。

following is my class 以下是我的课

public class ReviewPanel extends JPanel


{
 private Vector bookList;
 private JList jl;
 private JRadioButton poor,fair,avg,good,exlnt;
 private JButton jb;


public ReviewPanel(Vector bookList)
 {

     this.bookList = bookList;
     jl = new JList<String>(this.bookList); 
     String tmp=null;

        for(int i=0;i<bookList.size();i++){
             tmp = tmp + bookList.get(i) + "\n"; 
                System.out.println(tmp); //here it is showing the values in booklist vector
        }   

        add(jl);
        ButtonGroup bg =new ButtonGroup();
        poor = new JRadioButton("1 Poor");
        fair = new JRadioButton("2 Fair");
        avg = new JRadioButton("3 Average");
        good = new JRadioButton("4 Good");
        exlnt = new JRadioButton("5 Excellent");
        bg.add(poor);  
        bg.add(fair);
        bg.add(avg);
        bg.add(good);
        bg.add(exlnt);
        add(poor);
        add(fair);
        add(avg);
        add(good);
        add(exlnt);
        jb = new JButton("Submit Review");
        add(jb);

  }
  //more code

}

below is the pic for what it looks like. 下面是它看起来像的图片。 even though i have added the Jlist already. 即使我已经添加了Jlist。 it doesn't show anything 它没有显示任何东西 审查小组图像

not savvy to jlist and vectors, any help appreciated. 不了解jlist和向量,任何帮助赞赏。

In the past I have had simular issues with the FlowLayout , which is the default layout for a JPanel . 在过去,我遇到了FlowLayout类似问题,这是JPanel的默认布局。

It sometimes does not show one of the items added (in my case it was often the last). 它有时不会显示添加的项目之一(在我的情况下,它通常是最后一个)。

You can test if the use of your layout manager is your problem by starting you function with setLayoutManager(null) . 您可以通过使用setLayoutManager(null)启动功能来测试布局管理器的使用是否是您的问题。 This turns is off, which will place the elements on their default locations and sizes. 此关闭关闭,这将元素放在其默认位置和大小。 If the JList does show this is your problem. 如果JList确实显示这是你的问题。

A very good example for your program is the Button Demo . Button程序是一个非常好的例子。 This also shows the use of SwingUtils to make sure you create your program on the 'Event Dispatch Thread', which is required for all interaction with Swing objects. 这也显示了使用SwingUtils来确保在'Event Dispatch Thread'上创建程序,这是与Swing对象进行所有交互所必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM