?
Solved

Adding text to jlist

Posted on 2000-04-12
10
Medium Priority
?
1,069 Views
Last Modified: 2008-02-26
how do i add text to a jlist and make it visible?

an example please
0
Comment
Question by:j_baxter99
[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
10 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 2706899
hi,
 try this
 String [] str = {"aa","bb"};
 JList list = new JList(str);
 
mb...
0
 

Author Comment

by:j_baxter99
ID: 2707275
Yep i can do that but i want to add to how do i add contents to it dynamically.
e.g

String [] str = {"aa","bb"};
 JList list = new JList(str);

and now i want to add "cc" to my list

how do i do it?
cheers
0
 

Author Comment

by:j_baxter99
ID: 2707292
also how do i update it when it is added, i.e refresh the list.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:j_baxter99
ID: 2707327
ok i have it but how do i get it to scroll
0
 
LVL 2

Expert Comment

by:muraliram
ID: 2707384
Dear j_baxter99,

JList list = new JList(str);
list.addItem("How are you?");

It will get automatically scrolled when the list is growing.

Bye

Murali
0
 

Author Comment

by:j_baxter99
ID: 2707411
thats is only for a awt.List it doesnt work that way for JList`s muralirm
0
 
LVL 6

Expert Comment

by:expertmb
ID: 2707462
hi,
 inorder to get scrollbar add the Jlist to JScrollPane u will get the scroll bar.

mb...
0
 

Author Comment

by:j_baxter99
ID: 2707641
cant get the scroll pane to become visible ???
0
 
LVL 3

Accepted Solution

by:
ovidiucraciun earned 80 total points
ID: 2707687
package noel.tests;

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class JListExample extends JPanel
{
    private JList       list;
    private JScrollPane jsp;
   
    public JListExample()
    {
        super();
        InitComponents();
    }
    public static void main(String s[])
    {
        JListExample listPanel = new JListExample();
        JFrame fr = new JFrame("String to JList example");
        fr.setContentPane( listPanel );
        fr.setBounds( 300, 300, 300, 300 );
        fr.setVisible( true );
    }
    private void InitComponents( )
    {
        setLayout( new BorderLayout() );
        list = new JList();
        list.setModel(new DefaultListModel());
        for ( int k = 0; k < 25; k ++ )
        {
            addStringToList( "Element nomber " + k );
        }
        jsp = new JScrollPane( list );
        add( jsp, BorderLayout.CENTER );
        //File f = new File(".");
        //System.out.println(f. getAbsolutePath());
    }
    private void addStringToList( String str )
    {
        if ( list != null )
        {
            ((DefaultListModel)list.getModel()).addElement( str );
        }
    }
}
0
 
LVL 6

Expert Comment

by:expertmb
ID: 2707891
 jscrollpane.getViewport().add(list, null).
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

649 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