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
Solved

Adding text to jlist

Posted on 2000-04-12
10
1,057 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
Arrays.asList  VS  ArrayList 4 71
java stored proc example 9 44
Setup GlassFish 4 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This video teaches viewers about errors in exception handling.

856 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