Solved

JList does not show selected value

Posted on 2000-03-28
9
391 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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