Solved

JList does not show selected value

Posted on 2000-03-28
9
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

626 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