简体   繁体   中英

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. the vector already has its values filled but when i add it to JList it doesn't shows anything.

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. it doesn't show anything 审查小组图像

not savvy to jlist and vectors, any help appreciated.

In the past I have had simular issues with the FlowLayout , which is the default layout for a 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) . This turns is off, which will place the elements on their default locations and sizes. If the JList does show this is your problem.

A very good example for your program is the Button Demo . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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