Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Having trouble adding items to a JList from local JList
    primarykey
    data
    text
    <p>I'm having a problem here where I've created an actionListener which is intended to create a random human and add it to JList to be displayed on JScrollPane. Everything has worked out good so far except that whenever I click the JButton to add a new human, the JList does not add to the current list, instead it replaces it each time again and so only one item is ever displayed on the Jlist. I know where the problem is occurring and you'll immediately see it in the actionevent lines. Anyways, thanks for any help my friends!</p> <pre><code>private static final JTextArea PlayerList = new JTextArea(30, 100); private JList newbie; private List&lt;Human&gt; members = new ArrayList&lt;Human&gt;(); private JTextArea Area; private String[] listString; private String[] newString; private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); private ArrayList&lt;String&gt; zz = new ArrayList&lt;String&gt;(); public JTabbedPaneFrame() throws FileNotFoundException { super("JTabbedPane Demo"); JTabbedPane tabbedPane = new JTabbedPane(); JPanel Gladiator = new JPanel(); getContentPane().add(Gladiator); /////////////Tabbed Pane 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")); Area = new JTextArea(20, 15); Area.setLineWrap(true); Area.setWrapStyleWord(false); final JList newbie = new JList(); JScrollPane PlayerViewer = new JScrollPane(newbie); PlayerViewer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); ListOfPlayers.add(PlayerViewer); Gladiator.add(ListOfPlayers); /////////////Vertical Box between Text and Tabbed Profile////// Box Randomer = Box.createVerticalBox(); Randomer.setBorder(new TitledBorder("Randomize or Add Gladiator")); JButton AddIndividual = new JButton("Add a Player"); Randomer.add(AddIndividual); Gladiator.add(Randomer); AddIndividual.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String x = ""; String y = ""; String z = ""; String ee = ""; ArrayList&lt;String&gt; listx = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; zzx = new ArrayList&lt;String&gt;(); JList newbiex; Human temp; try { temp = new Human(); x = temp.toString(); y = temp.getSurname(); z = temp.getFirstName(); listx.add(x); ee = String.format(y + ", " + z ); zzx.add(ee); listString = new String[zzx.size()]; listString = zzx.toArray(listString); newbiex = new JList(zzx.toArray()); newbie.setModel(newbiex.getModel()); members.add(temp); for(String W: listString) /////testing diff method here//// { Area.append(W); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); add(tabbedPane); /////////////Action Buttons//////////////////// } } </code></pre> <p>class HumanRenderer extends DefaultListCellRenderer </p> <pre><code> { 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