swing jlist, how to print out once instread of twice when selection changed
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