Solved

JList does not show selected value

Posted on 2000-03-28
9
394 Views
Last Modified: 2008-01-16
I have a program where I use a username and password to get into a database.  When I log in, I have a JList added to a scroll pane.  In that JList I have list of all the users.  I would like it to select automatically the name of the user who is logged in and scroll down to that name.  I used
jlist.setSelectedValue(username, true), but it just selects the name but does not scroll down to it.  I did not use Viewport for this scrollpane.  Any advice?
0
Comment
Question by:Dmitriy
  • 5
  • 3
9 Comments
 
LVL 1

Expert Comment

by:sankars98
ID: 2664852

call ebsureIndexIsVisible() method in JList. That will do the trick.

list.setSelectedValue(...);
                      list.ensureIndexIsVisible( list.getSelectedIndex() );

Sankar S.
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 2669933
I tried to use that command, but it did not work, because I use DefaultListModel, not Viewport; and that command works only with Viewport.
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2683391
Isn't this what you want?
best regards,
Ovidiu
////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class test2 extends JPanel
{
      JList list;
      public test2( )
      {
            JScrollPane jsp = new JScrollPane( list = new JList( new MyModel(1000) ) );
            setLayout( new BorderLayout() );
            add( new JButton("Test A"), BorderLayout.NORTH );            
            add( jsp, BorderLayout.CENTER );            
            add( new JButton("Test B"), BorderLayout.SOUTH );            
            
            //list.setSelectedIndex(999);
            list.setSelectedValue( new String("Index "+999), true );
            list.ensureIndexIsVisible( list.getSelectedIndex() );
      }
      public static void main(String args[])
      {
            test2 t = new test2();
            
            JFrame fr = new JFrame( "test2 frame" );
            fr.setBounds( 10, 10, 800, 600 );
            fr.setContentPane( t );
            fr.setVisible( true );
      }
}
class MyModel extends DefaultListModel
{
      int nSize = 10000;
      MyModel( )
      {
      }
      MyModel( int nSize )
      {
            this.nSize = nSize;
      }
    public int getSize()
    {
          return nSize;
    }
    public Object getElementAt( int nIndex )
    {
          return new String("Index " + nIndex);
    }
}
////////////////////////////////////////////////////////////////////////////////
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 2683975
OVIDIU, when I ran this program, the problem was still there... The selected value was 999, but it did not scroll to that number automatically.  The scroll was still showing me the first 25 indecies.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2684125
how's that?
on my running the scroll was done.
jdk1.2 windows NT 5.0
0
 
LVL 3

Accepted Solution

by:
ovidiucraciun earned 20 total points
ID: 2684142
even with this line
list.ensureIndexIsVisible( list.getSelectedIndex() );
comented it works like you wish.
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 2687338
Do you think something is wrong with my system? I even tried the programs from the java's website with the code that has the line, but it still didn't work? Tomorrow I will post my system stats.  But are you sure that the program works fine on your system?
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 2689722
System:  Microsoft Windows NT

         4.00.1381 (Service Pack 4)

         JDK 1.3
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 2691251
Thanx for trying. I figured out what the problem was. One of the frames was resetting my scrollpane.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 71
Java array passed to SQL where clause 23 64
AWS and Java and Rest - newbie question (Is this an authentication issue) 4 68
ForLoop Example 3 40
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now