2014-03-06 66 views
0

我有一个JList设置为使用SamplePerson对象的ArrayList的内容。每个SamplePerson会在创建时自动创建一个随机名称。我试图让JList显示JList中的示例人员的名称,但是列表显示为空白。 Oracle教程已经有所帮助,但我仍然无法弄清楚如何让它显示出来。在自定义对象中使用jlist时遇到问题

package main; 

import java.awt.Component; 

class SamplePerson{ 
private String firstName, lastname; 
private static final String[] firstNameChoices = {"Mark", "Eric", "Henry", "Carl", "Watson"}; 
private static final String[] lastNameChoices = {"Smith", "Castle", "Baldwin", "Anderson"}; 

// A bunch of other fields that would go here are omitted for clarity. 

String personDescription; 
Random rand = new Random(); 

public SamplePerson() { 
    initalizeName(); 
    personDescription = "This is a placeholder"; 

    // Other fields are initialized here. 
} 

private void initalizeName(){ 
    firstName = firstNameChoices[rand.nextInt(firstNameChoices.length)]; 
    lastname = lastNameChoices[rand.nextInt(lastNameChoices.length)]; 
} 

public String getFullName() { 
    return firstName +" " + lastname; 
} 

public String getFullNameLastNameFirst() { 
    return lastname + ", " + firstName; 
} 

public String getPersonDescription() { 
    return personDescription; 
} 
} 

public class ListAppTest implements ListSelectionListener{ 

private JFrame frame; 
private JTextPane textPane; 
private JList<SamplePerson> list; 

private ArrayList<SamplePerson> arrayListOfPeople; 
private ListModel<SamplePerson> listMod; 
private ListCellRenderer<SamplePerson> listRender; 
private JLabel lblListOfPeople; 
private JLabel lblDescription; 

private String description; 
/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       ListAppTest window = new ListAppTest(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public ListAppTest() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 

    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    listRender = new ListCellRenderer<SamplePerson>() { 

     @Override 
     public Component getListCellRendererComponent(
       JList<? extends SamplePerson> list, SamplePerson value, int index, 
       boolean isSelected, boolean cellHasFocus) { 

      JLabel thisLabel = new JLabel(value.getFullName()); 
      thisLabel.setBounds(0, 0, 61, 16); 
      return thisLabel; 
     } 
    }; 

    arrayListOfPeople = new ArrayList<SamplePerson>(); 

    addToArrayListOfPeople(6); 

    listMod = new DefaultListModel<SamplePerson>(); 

    list = new JList<SamplePerson>(listMod); 
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    list.setSelectedIndex(1); 
    list.setCellRenderer(listRender); 

    list.setBounds(30, 30, 120, 220); 
    list.addListSelectionListener(this); 

    frame.getContentPane().add(list); 
    if(list.getSelectedIndex() != -1){ 
     description = list.getSelectedValue().getPersonDescription(); 
    }else{ 
     description = ""; 
    } 

    textPane = new JTextPane(); 
    textPane.setText(description); 
    textPane.setEditable(false); 
    textPane.setBounds(230, 50, 160, 170); 

    frame.getContentPane().add(textPane); 

    lblListOfPeople = new JLabel("List of People"); 
    lblListOfPeople.setBounds(30, 22, 90, 16); 
    frame.getContentPane().add(lblListOfPeople); 

    lblDescription = new JLabel("Description"); 
    lblDescription.setBounds(230, 22, 80, 16); 
    frame.getContentPane().add(lblDescription); 

} 

private void addToArrayListOfPeople(int peopleToAdd){ 
    for (int i = 0; i < peopleToAdd; i++) { 
     arrayListOfPeople.add(new SamplePerson()); 
    } 
} 

@Override 
public void valueChanged(ListSelectionEvent e) { 
    description = list.getSelectedValue().getPersonDescription(); 
    textPane.setText(description); 
} 
} 
+2

您不应该在'getListCellRendererComponent'方法内创建渲染器组件。声明是否在方法之外,并在方法内改变它的状态。 –

回答

3

你永远不添加任何东西到ListModel ...

addToArrayListOfPeople(6); 

listMod = new DefaultListModel<SamplePerson>(); 

您需要从ArrayList到模型中添加元素...

addToArrayListOfPeople(6); 

listMod = new DefaultListModel<SamplePerson>(); 
for (SamplePerson person : arrayListOfPeople) { 
    listMod.addElement(person); 
} 

您应该避免使用因为您无法控制字体大小,DPI,屏幕分辨率,渲染管道或其他可能影响fon效果的因素的差异t被渲染。 null布局将使您的工作增加10倍,并降低应用程序的可移植性。你也有少“怪诞”的摇摆旨在与approriate布局管理器使用

你也应该加入你的JListJScrollPane

看看:

+0

感谢您的帮助。空布局只是为了当我测试某些东西或试图让它工作时,我个人更喜欢使用springLayout(或者当我使用多个面板时使用cardLayout)以获得更多的工作。 – user3249872

相关问题