Solved

Adding text to jlist

Posted on 2000-04-12
10
1,048 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
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
 

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

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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

15 Experts available now in Live!

Get 1:1 Help Now