简体   繁体   中英

Having trouble adding objects to JList to display on scrollpane

I've worked at creating a defaultlistcellrender for an object that i have made but so far it's proving very difficult to add objects to JList. I am attaching the code for any suggestions. Thanks!

public class JTabbedPaneTester extends JFrame 
{
    private List<Human> members = new ArrayList<Human>();
    private JList newbie = new JList();
    private DefaultListModel model = new DefaultListModel();

    public JTabbedPaneTester() throws FileNotFoundException
    {
        super("JTabbedPane Demo");
        JTabbedPane tabbedPane = new JTabbedPane(); 
        JPanel gladiator = new JPanel();
        getContentPane().add(gladiator); 
        tabbedPane.addTab("Gladiator", null, Gladiator, "");
        Box listOfPlayers = Box.createVerticalBox();
        listOfPlayers.add(Box.createRigidArea(new Dimension(100,100)));
        listOfPlayers.setBorder(new TitledBorder("List of Players"));
        JScrollPane playerViewer = new JScrollPane();
        playerViewer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        listOfPlayers.add(playerViewer);
        JButton AddIndividual = new JButton("Add a Player");
        listOfPlayers.add(addIndividual);
        gladiator.add(listOfPlayers);
    final HumanListModel modelx = new HumanListModel();
    final JTable newbiex = new JTable(modelx);
    newbiex.setDefaultRenderer(Human.class, new HumanRenderer());
    playerViewer.setViewportView(newbiex);

    addIndividual.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {
                    Human temp;
                try {
                    temp = new Human();     
                    modelx.addHuman(temp);
                        } 
                    catch (FileNotFoundException e) 
                        {
                            e.printStackTrace();
                        }       
                    }
        });
        add(tabbedPane); 
    }
}       

Here is the renderer someone on here nicely helped me with

class HumanRenderer extends DefaultListCellRenderer

        {
           public Component getListCellRendererComponent(JList list, Object value,
              int index, boolean isSelected, boolean cellHasFocus) 
           {


              JLabel label = new JLabel(); 
              if (value != null) 
              {
                 Human human = (Human) value;
                 label.setText(human.getSurname() + ", " + human.getFirstName());
              }

              return label;
           }
        }

You need to add objects to the model and not to the jlist panel. The add the you use for components. Try to get the model from jlist and use addElement of the model.

You are using the DefaultListModel which uses a DefaultListCellRenderer . I don't see any part in your code that actually makes use of your HumanRenderer . You have to write your own Model.

public class HumanListModel extends DefaultListModel
{
    private ArrayList<Human> data;

    public HumanListModel()
    {
        super();
        data = new ArrayList<Human>();
    }

    public void addHuman(Human h)
    {
       // add new human to the model
       data.add(h);
       fireTableStructureChanged();
    }

    public void removeHuman(Human h)
    {
        data.remove(h);
        fireTableStructureChanged();
    }

    @Override
    public int getColumnCount()
    {
        // the number of columns you want to display
        return 1;
    }

    @Override
    public int getRowCount()
    {
        return data.size();
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        return (row < data.size()) ? data.get(row) : null;
    }

    @Override
    public String getColumnName(int col)
    {
        return "Human";
    }

    @Override
    public Class getColumnClass(int col)
    {
        return Human.class;
    }
}

For your JTable you just have to set the HumanListModel and define your renderer. All changes to your data should afterwards directely made at the model. Ergo use: model.addHuman() and model.removeHuman() . They fire the necessary Events which the JTable listens for in order to repaint.

HumanListModel model = new HumanListModel();
JTable newbies = new JTable(model);
newbies.setDefaultRenderer(Human.class, new HumanRenderer());

I hope it works...

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