Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble adding objects to JList to display on scrollpane
    primarykey
    data
    text
    <p>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!</p> <pre><code>public class JTabbedPaneTester extends JFrame { private List&lt;Human&gt; members = new ArrayList&lt;Human&gt;(); 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); } } </code></pre> <p>Here is the renderer someone on here nicely helped me with</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload