import java.awt.*; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class HighScoreWindow { private JFrame frame; public HighScoreWindow() { frame = new JFrame(); } public void showWindow(Component parent) { String[] value = {"A", "B", "C", "D", "E"}; JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); final DefaultListModel playerListModel = new DefaultListModel(); final DefaultListModel continentListModel = new DefaultListModel(); for (int x = 0; x < value.length; x++) { playerListModel.addElement(value[x]); } final JList players = new JList(playerListModel); final JList continentScores = new JList(continentListModel); JScrollPane pListScroll = new JScrollPane(players); JScrollPane cScoreScroll = new JScrollPane(continentScores); continentScores.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); continentScores.setBorder(BorderFactory.createLoweredBevelBorder()); continentScores.setFont(new Font("Monospaced", Font.BOLD, 16)); players.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); players.setBorder(BorderFactory.createLoweredBevelBorder()); players.setFont(new Font("Monospaced", Font.BOLD, 16)); players.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { continentListModel.addElement(players.getSelectedValue().toString()); } }); gc.gridx = 0; gc.gridy = 0; pListScroll.setPreferredSize(new Dimension(100, 400)); panel.add(pListScroll, gc); gc.gridx = 1; gc.gridy = 0; gc.insets.left = 50; gc.anchor = GridBagConstraints.SOUTH; cScoreScroll.setPreferredSize(new Dimension(200, 200)); panel.add(cScoreScroll, gc); frame.getContentPane().add(panel); frame.pack(); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new HighScoreWindow().showWindow(null); } }
how can i avoid duplicate values when im appending a value from a JList to another JList using its DefaultListModel? what method should i call to avoid duplicating an element added to a DefaultListModel?