Solved

JList does not show selected value

Posted on 2000-03-28
9
401 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 439
Glassfish admin console not working 1 62
Fast way to search item into Java Array (Rhino compatible) 2 39
junit example issue 2 30
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

697 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