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.