Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Adding a value to a DefaultListModel via ListSelectionListener (JList)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Adding a value to a DefaultListModel via ListSelectionListener (JList)

    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?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Adding a value to a DefaultListModel via ListSelectionListener (JList)

    Use the getValueIsAdjusting method of ListSelectionEvent. If it is adjusting, one may wish to skip the event and wait for the another in which the value is not adjusting (eg the user has finished making the selection).

  3. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (August 1st, 2012)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Adding a value to a DefaultListModel via ListSelectionListener (JList)

     public void valueChanged(ListSelectionEvent e) {
     
                    if (e.getValueIsAdjusting()) {
     
                        continentListModel.addElement(players.getSelectedValue().toString());
                    }
                }

    would you mind to check if what i did is what you were trying to say? i mean.. i had a hard time understanding the thought of the statements(the grammar thing), what i did was, first i printed the source value(e) if it is adjusting(e.getValueIsAdjusting). when the method executes, one is true and one is false, so i did that statement above and it worked as i want it to.. but.. what is the thing that is adjusting? and why did it make a duplicate if it is not adjusting? excuse me for my grammar,

    i also made another approach to deal with this one.. instead of using a listSelectionListener, i registered a mouseListener using a mousePressed event. But I dont get it, im just clicking, how come that there is something that was adjusting,
    Last edited by chronoz13; August 1st, 2012 at 11:14 AM.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Adding a value to a DefaultListModel via ListSelectionListener (JList)

    In the above code I'd recommend negating the e.getValueIsAdjusting() - the result is probably the same, but to be clear you only want events after adjustments have been made - the is adjusting value is true if the user is still manipulating the selection.

  6. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (August 1st, 2012)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Adding a value to a DefaultListModel via ListSelectionListener (JList)

    but to be clear you only want events after adjustments have been made
    ahh that would be enough, that makes sense now though, so instead, ill negate the value of it, a better approach

Similar Threads

  1. adding varibles to array and using Jlist
    By Fantasy in forum Collections and Generics
    Replies: 1
    Last Post: November 24th, 2011, 05:19 AM
  2. Custom JList
    By daghost in forum AWT / Java Swing
    Replies: 3
    Last Post: November 17th, 2011, 04:57 PM
  3. Updating JList
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 3
    Last Post: October 6th, 2011, 07:17 AM
  4. How to use ListSelectionListener in Jtable?
    By Vignesh Karthick in forum AWT / Java Swing
    Replies: 1
    Last Post: February 10th, 2011, 07:58 AM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 07:22 AM