Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
John_Woo - 02 Oct 2006 22:59 GMT
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[i]);

            }
        })    ;

        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
Michael Rauscher - 03 Oct 2006 03:43 GMT
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 - 03 Oct 2006 03:52 GMT
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
Andrew Thompson - 03 Oct 2006 08:02 GMT
....
> 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 );
[quoted text clipped - 5 lines]
>         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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.