Solved

JList does not show selected value

Posted on 2000-03-28
9
398 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap not working 8 44
java jdbc batch example 8 32
Configure a Bean in an XML file 4 31
Java: How do I open the default windows program for a given file type 3 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

786 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