Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > swing jlist, how to print out once instread of twice when selection changed

Reply
Thread Tools

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

 
 
John_Woo
Guest
Posts: n/a
 
      10-02-2006
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

 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      10-03-2006
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
 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      10-03-2006
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
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      10-03-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Problem - I want to print Current Output of Pdf file and shouldprint once.I get print dialog box but it is not working Lew Java 0 05-30-2007 01:31 PM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Printer will only print once but not twice? John Brown Computer Support 6 07-10-2006 11:09 PM
help with getting selection from wxChoice with out after it has changed '@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) Python 2 03-29-2005 10:15 AM



Advertisments