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

J

John_Woo

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?
 
M

Michael Rauscher

John_Woo said:
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
 
M

Michael Rauscher

Michael said:
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
 
A

Andrew Thompson

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top