swing jlist, how to print out once instread of twice when selection changed

Discussion in 'Java' started by John_Woo, Oct 2, 2006.

  1. John_Woo

    John_Woo Guest

    Hi,

    I got two questions for the following codes

    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.*;

    public class Test {

    public static final void main( String args[] ) throws Exception {

    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );

    String[] items = {"A", "B", "C", "D"};

    JList list = new JList(items);

    list.addListSelectionListener(new
    javax.swing.event.ListSelectionListener(){
    public void valueChanged(javax.swing.event.ListSelectionEvent e)
    {
    int[] k = ((JList)e.getSource()).getSelectedIndices();

    for (int i = 0; i < k.length; i++)
    System.out.println(k);

    }
    }) ;


    JScrollPane scrollingList = new JScrollPane(list);

    frame.setContentPane( scrollingList );
    frame.setSize(300,150);
    frame.pack(); //line A
    frame.setVisible( true );

    }
    }

    1. why every time selection changed, print out twice, how to make it
    print once?
    2. with line A, the setSize() didn't apply, it seemed using default
    size. why?

    --
    Thanks
    John
    Toronto
    John_Woo, Oct 2, 2006
    #1
    1. Advertising

  2. Re: swing jlist, how to print out once instread of twice when selectionchanged

    John_Woo schrieb:
    > Hi,
    >
    > I got two questions for the following codes
    >

    [code containing JList and ListSelectionListener snipped]

    >
    > 1. why every time selection changed, print out twice, how to make it
    > print once?


    Because there are (most often) two events fired. Have a look at the API
    of ListSelectionEvent it "represents a change in selection status
    between firstIndex and lastIndex inclusive".

    Let me give an example and consider that the list element at index 5 is
    selected. Now select the list element at index 1. There are two changes:
    list element at index 5 is deselected (which is a change in selection
    status between 5 and 5), list element at 1 is selected (which is a
    change in selection status between 1 and 1).

    The solution is to use ListSelectionEvent#getValueIsAdjusting().

    > 2. with line A, the setSize() didn't apply, it seemed using default
    > size. why?


    Due to the pack().

    Bye
    Michael
    Michael Rauscher, Oct 3, 2006
    #2
    1. Advertising

  3. Re: swing jlist, how to print out once instread of twice when selectionchanged

    Michael Rauscher schrieb:

    >> 2. with line A, the setSize() didn't apply, it seemed using default
    >> size. why?

    >
    > Due to the pack().


    Sorry, haven't seen where line A is :)

    So the answer is: RTFM.

    Window#pack():
    Causes this Window to be sized to fit the preferred size and layouts of
    its subcomponents.

    Bye
    Michael
    Michael Rauscher, Oct 3, 2006
    #3
  4. John_Woo wrote:
    .....
    > public class Test {
    >
    > public static final void main( String args[] ) throws Exception {


    Please do not indent code by more than 2-3 spaces for
    each level when posting to usenet..

    > JFrame frame = new JFrame("Test");
    > frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    >
    > String[] items = {"A", "B", "C", "D"};
    >
    > JList list = new JList(items);
    >
    > list.addListSelectionListener(new
    > javax.swing.event.ListSelectionListener(){


    ...and try breaking lines beofre arunf 63-65 chars, as most
    news readers will wrap text around that width.

    > public void valueChanged(javax.swing.event.ListSelectionEvent e)
    > {


    // basic debug statement
    // System.out.println("What is this thing called 'e'? " + e);
    System.out.println(e.getValueIsAdjusting());

    > 1. why every time selection changed, print out twice, how to make it
    > print once?


    Add the statement above (part of a basic debugging strategy to
    find out exactly what caused the event)!

    And comp.lang.java.help is a great group for people learning Java.

    Andrew T.
    Andrew Thompson, Oct 3, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    0
    Views:
    884
  2. Don Sykes
    Replies:
    3
    Views:
    664
    Christophe Vanfleteren
    Feb 25, 2004
  3. steve
    Replies:
    4
    Views:
    4,271
    steve
    Nov 5, 2005
  4. oziris
    Replies:
    0
    Views:
    343
    oziris
    May 9, 2007
  5. keto
    Replies:
    0
    Views:
    898
Loading...

Share This Page