JList performance in JScrollPane

Hi!
I've got a really odd problem with JList handling inside a JScrollPane.
What happens is that I populate the JList with elements from a vector, and it displays them allright, but when I add or remove any element after creating the jList for first time, the CPU usage goes crazy until I move the scrollbar knob of the ScrollPane it's in. After moving the knob, the elements display ok, and cpu usage goes down to normality.. I can reproduce this phenomenon every time.
At first I thought it was a performance issue, but after implementing a custom minimal cell renderer (which I actually don't use anymore) and adjusting the prototypecell and fixed size properties, this kept happening... Any ideas are welcome!
Thanks!
P.S:The code is a little large, but I'll post it if it helps!

Seems to work for meimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Test2 extends JFrame {
  Vector data = new Vector(10000);
  DefaultListModel dlm = new DefaultListModel();
  JList jl = new JList();
  public Test2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();
    for (int i=0; i<10000; i++) dlm.addElement("Stuff-"+i);
    jl.setModel(dlm);
    JButton jb = new JButton("Remove");
    content.add(jb, BorderLayout.SOUTH);
    jb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        int index = jl.getSelectedIndex();
        if (index>=0) dlm.remove(index);
    content.add(new JScrollPane(jl), BorderLayout.CENTER);
    setSize(300,300);
  public static void main(String[] args) { new Test2().setVisible(true); }
}

Similar Messages

  • How can I get the selected row's index of a Jlist wrapped with JScrollpane?

    the problem is that I can use getSelectedIndex() only if the JList is not wrapped in a JScrollPane.
    but in my program the JList IS wrapped in a JScrollPane.
    when I select one item of the jscrollpane I can't catch the event.
    please F1 me.
    Lior.

    What difference does it make if the list is inside a scroll pane or not? If you have a member varialbe that references the list you can call getSelectedIndex when ever you need.
        myList = new JList (...)
        someComponent.add (new JScrollPane (myList));
        //  Then later
        int sel = myList.getSelectedIndex ();And you can always be up on any selection chagnes in the list by supplying it with a ListSelecitonListener.

  • JList(Vector) inside JScrollPane vanishes!

    Hi all, I have a problem thats driving me crazy, Im really stumped on this one;
    Im trying to update the GUI on a packet sniffer I wrote a while ago,
    I have a GUI class below as part of a larger program, this class containes a JList(Vector) inside a JScrollPane inside a JPanel inside a JFrame, I want this JList to be on the screen all the time, and grow dynamically as new elements are added to the Vector.
    Elements are added via an accessor method addPacket(Capture), you can assume the other classes in my program work fine.
    The problem is, the JFrame periodically goes blank, sometimes the scrollbars are present, sometimes not. After a while (sometimes) the JList reappears, containing the list of packets as it should, but it never lasts very long. Often it will disappear permanently.
    The class below is pretty short and simple, I really hope this is a simple and obvious mistake Ive made that someone can spot.
    Thanks,
    Soothsayer
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.InputStream;
    public class GUI extends JFrame
         Dimension screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
         private InputStream fontStream = this.getClass().getResourceAsStream("console.ttf");
         Font outputFont;
         static final Color FIELD_COLOR = new Color(20, 20, 60);
         static final Color FONT_COLOR = new Color(150, 190, 255);
         private static JPanel superPanel = new JPanel();
         private static Vector<Capture> packetList = new Vector<Capture>(1000, 500);
         private static JList listObject = new JList(packetList);
         private static JScrollPane scrollPane = new JScrollPane(listObject);
         public GUI()
              super("LineLight v2.1 - Edd Burgess");
              this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              this.setLocation(50, 10);
              try { outputFont = Font.createFont(Font.TRUETYPE_FONT, fontStream); }
              catch(Exception e) { System.err.println("Error Loading Font:\n" + e); }
              outputFont = outputFont.deriveFont((float)10);
              listObject.setFont(outputFont);
              superPanel.setPreferredSize(new Dimension(screenResolution.width - 100, screenResolution.height - 100));
              superPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
              scrollPane.setPreferredSize(new Dimension(screenResolution.width - 100, screenResolution.height - 100));
              scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
              scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
              scrollPane.setWheelScrollingEnabled(true);
              superPanel.add(scrollPane);
              this.add(superPanel);
              this.pack();
         public void addPacket(Capture c)
              this.packetList.add(c);
              this.listObject.setListData(packetList);
              this.superPanel.repaint();
    }

    I'm having basically the same problem, And how is your code different than the example in the tutorial???
    You can also read the tutorial section on "Concurrency".
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
    Start your own posting if you need further help.

  • Adding a JList to a JScrollPane

    Searching through the archives, this appears to be a pretty frequent question, but so far, none of the answers have worked for me. Behold, what I have:
    Filelist = new JList(Filelistmodel);
    FilePane = new JScrollPane();
    FilePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    FilePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    FilePane.setMaximumSize(new Dimension(650,150));
    FilePane.setMinimumSize(new Dimension(650,150));
    FilePane.setPreferredSize(new Dimension(650,150));
    FilePane.setBounds(150, 0, 650, 150);
    FilePane.setViewportView(Filelist);
    panel.add(FilePane);     Message was edited by:
    break_the_chain

    Ok, here's the context. It's not exactly simple, but it should compile and the rest of the code is more or less outside of the problem.
    package formshow;
    //import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import java.io.*;
    import java.util.*;
    public class Sync extends JApplet
         JButton bttn1;
         JComboBox Cbox_drives, Cbox_folders;
         JPanel panel;
         JFrame aframe;
         JScrollPane FilePane;
         DefaultListModel Filelistmodel;
         JList Filelist;
         class OnlyPRE implements FilenameFilter
           Vector PRE;
           public OnlyPRE(Vector PRE)
           {this.PRE = PRE;}
           public boolean accept(File dir, String name)
                              boolean accept = false;
              for (int i=0; i<PRE.size(); i++)
              if (name.startsWith((String)PRE.get(i)))
              accept = true;
           return accept;
         public void init()
              //applet listener
              this.addFocusListener( new FocusListener()
                public void focusGained(FocusEvent evt)
                                   getContentPane().invalidate();
                   getContentPane().validate();
              public void focusLost(FocusEvent evt)
                 getContentPane().invalidate();
                 getContentPane().validate();
                             //this applet
              this.setSize(800,150);
              //jpanel
              panel = new JPanel(null);
              //Button init stuff
              bttn1 = new JButton("Get Files");
              bttn1.setSize(150,50);
              bttn1.setLocation(0,100);
              bttn1.addActionListener( new ActionListener()
                 public void actionPerformed(ActionEvent evt)
                 {PopList();}
              panel.add(bttn1);
              //Cbox_drives init stuff
              int offset = 'a';                    
              Cbox_drives = new JComboBox();
              for (int i=0; i<26; i++)
              Cbox_drives.addItem((char)(offset+i)+":\\");               
              Cbox_drives.setSize(150,50);
              Cbox_drives.setLocation(0,0);
                              Cbox_drives.addActionListener( new ActionListener()
                public void actionPerformed(ActionEvent evt)
                   try                                  {
                    getPatFolders();
                  catch (Exception e)Cbox_drives.setBackground(Color.RED);}
              panel.add(Cbox_drives);
              //Cbox_folders init stuff
              Cbox_folders = new JComboBox();
              Cbox_folders.setLocation(0,50);
              Cbox_folders.setSize(150,50);
              Cbox_folders.addItem("Pick the drive letter you have mapped to 1416_a");
              panel.add(Cbox_folders);
              //Filelist init stuff
              Filelist = new JList();
              Filelist.setVisibleRowCount(5);
              //FilePane init stuff
              FilePane = new JScrollPane(Filelist);
              FilePane.setLocation(150,0);
              FilePane.setSize(650,150);
              panel.add(FilePane);
              getContentPane().add(panel);                              
              private int getPatFolders () throws Exception
                   String drive = (String) Cbox_drives.getSelectedItem();
                   String files[];
                   Vector prelist = new Vector();
                   prelist.addElement("PAT");
                   prelist.addElement("REL");
                   prelist.addElement("EME");
                   FilenameFilter onlyget = new OnlyPRE(prelist);
                   Cbox_folders.removeAllItems();
                   try{
                   File rootFolder = new File(drive);
                   files = rootFolder.list();
                   catch (Exception E)
                   files = new String[]{"Error Here"};
                   for (int i=0; i<files.length; i++)
                        Cbox_folders.addItem((String)files);               
                   return (1);
              private int PopList()
                   File StartFolder = new File(((String)Cbox_drives.getSelectedItem())+
    (String)Cbox_folders.getSelectedItem()));
                   PopList(getFiles(StartFolder));
                   Filelist.removeAll();
                   Filelist = new JList(Filelistmodel);
                   FilePane = new JScrollPane();
                   FilePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                   FilePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                   FilePane.setMaximumSize(new Dimension(650,150));
                   FilePane.setMinimumSize(new Dimension(650,150));
                   FilePane.setPreferredSize(new Dimension(650,150));
                   FilePane.setBounds(150, 0, 650, 150);
                   FilePane.setViewportView(Filelist);
                   panel.add(FilePane);     
                   getContentPane().invalidate();
                   getContentPane().validate();
                   return(1);
              private int PopList(Vector transmute)
                   Filelistmodel = new DefaultListModel();
                   for (int i=0; i<transmute.size(); i++)
                        Filelistmodel.addElement(transmute.elementAt(i));
                   return(1);
              private Vector getFiles(File ThisPath)
                   Vector ThisLevel = new Vector();               
                   String FileList[] = ThisPath.list();
                   for (int i=0; i<FileList.length; i++)
                             File afile = new File(ThisPath.getAbsolutePath()+"\\"+FileList[i]);
                             if (afile.isFile())
                                  ThisLevel.addElement(afile.getAbsolutePath());
                             else
                                  append(ThisLevel,getFiles(afile));                              
                   return ThisLevel;
              private Vector append(Vector Left, Vector Right)
                   for (int i=0; i<Right.size();i++)
                        Left.addElement(Right.get(i));
                   return Left;
    Ok, basically this is for windows users. Pick a drive letter, then pick a folder from the next combo box.
    Hit the button and a list of all of the files in the folders and subfolders shows up in the JList on the right.
    DO NOT try this when the second list box is pointing to something like windows or program files.
    I'd recommend creating your own folder that goes maybe one or two levels deep with just a bunch of empty text files.
    I will <3 anyone that can help me with this forever and bestow upon them rediculous numbers of dukestars.
    Message was edited by:
    break_the_chain

  • Java Turduckin: JScrollPane in a JList in a JScrollPane

    Probably a quick answer for someone: I have a JScrollPane containing a JList. The JList has a custom renderer that returns a subclass of JScrollPane: the idea is a scrollable list of individual panels of scrollable text.
    Is this possible? When I run it, it looks ok (the inner scroll bar hilights correctly, etc), but, even after a fair amount of investigation, I can't get the inner scrollbar to respond to the mouse. Can I get JList to forward clicks?
    Many thanks,
    -bmeike

    Renderers return a picture, not something you can interact with.
    Try setting your inner JScrollPane as the editor.

  • Paint performance with JScrollPane very slow in jdk 1.4?

    I got a simple program that overrides paintComponent on a JPanel. Then draws lots of lines, rectangles and some strings. The panel is then added to a scrollpane.
    The scrolling is very smooth in java 1.3.1, but very slow in 1.4.2
    the paintComponent takes between 16ms and 30ms with java 1.3.1 but 70-200ms with java 1.4.2.
    I tried turning of antialising etc.. but no help. Whats the "improvement" they made in jdk 1.4?

    Ok I made a simple example, which draws around 5000 elements.
    Sourcecode is here: http://www.mcmadsen.dk/files/ScrollPaneTest.java
    I did several testruns on java 1.4.2 and java 1.3.1, heres the "avarage" result:
    Java 1.4.2:
         Current: 140ms High: 203ms Avg: 144ms Low: 125ms
    Java 1.3.1:
         Current: 62ms High: 219ms Avg: 68ms Low: 47ms
    The paintComponent() looks like this:
    public void paintComponent(Graphics g)
    super.paintComponent(g);
    long offset=System.currentTimeMillis();
    PaintElement paintElementTmp;
    for(int i=0;i<paintElements.size();i++)
    paintElementTmp=(PaintElement)paintElements.elementAt(i);
    g.setColor(paintElementTmp.getBackground());
    g.fillRect(paintElementTmp.getX(),paintElementTmp.getY(),paintElementTmp.getWidth(),paintElementTmp.getHeight());
    g.setColor(paintElementTmp.getForeground());
    g.drawString(paintElementTmp.getText(),paintElementTmp.getX(),paintElementTmp.getY());
    long done=System.currentTimeMillis();
    long current=done-offset;
    sum+=current;
    if(current>high)high=current;
    if(low>current)low=current;
    count++;
    System.out.println("Current: "+current+"ms High: "+high+"ms Avg: "+(sum/count)+"ms Low: "+low+"ms");
    I tried all the renderinghints, but no difference (from the default settings). Also the scrolling is very slow and stops all the time in java 1.4.
    Any ideas on how to get java 1.4 to perform as java 1.3.1?
    Thanks

  • Problems to show a Vertical JScrollPane with JList

    I have a JList within a JScrollPane, and update the JList with the output from a process.
    This means that the JList is updated very quickly with a lot of data.
    The scrollpane policy is set using this :
    myScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    The problem happens as the list is being updated - sometimes the slider on the scrollbar disappears. It can reappear again, but this behaviour seems completely random.
    This means that if the process completes and the JList stops being updated, the lack of a slider prevents me from reviewing the JList contents.
    Does anyone know why this happens and what I can do to prevent it?

    JList displays the contents of a Vector. The Java version is JDK 1.2.2. And I'm using Windows 2000.

  • Adding JScrollPane to a JList problems

    Hi,
    I am having problem in trying to add a JScrollPane to a JList, it doesnt appear i don't understand what i am doing wrong.
    private String [] mainMenu = {"Phonebook", "Messages", "User Options", "Phone Status"};
    private JList main = new JList(mainMenu);
    private JScrollPane scrolling = new JScrollPane(main);
    public Mobile()
             main.setLocation(60, 110);
             main.setSize(90, 50);
             add(scrolling);
             scrolling.setVisible(true);
    }

    The code to run the app is:
    import java.awt.*;
    import javax.swing.*;
    class MobileGUI {
         public static void main(String[] args) {
              JFrame frame = new JFrame("Mobile Phone Simulation");
              Container pane = frame.getContentPane();
              pane.add(new Mobile());
              frame.setSize(340, 625);
              frame.show();
    }The class that contains the components is:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    class Mobile extends JComponent {
    private String[] mainMenu = {"Phonebook", "Messages", "User Options", "Phone Status"};
    private JList main = new JList(mainMenu);
    private JScrollPane scrolling = new JScrollPane(main);
    public JTextArea display = new JTextArea(6, 30);
    public MobileGUI()
              //Displays
              setLayout(null);
              //Display for messages
              display.setLocation(120, 60);
              display.setSize(90, 50);
              add(display);
              display.setVisible(false);
              //MainMenu Jlist Display
              main.setLocation(120, 60);
              main.setSize(90, 50);
              add(scrolling);
              scrolling.setVisible(true);
         public void paint(Graphics g) {
         g.setColor(Color.gray.brighter());
         g.fillRoundRect(30, 30, 270, 530, 40, 20);
         g.setColor(Color.black);
         g.fillArc(30, 30, 270, 320, 0, 180);
        g.fillArc(30, -180, 270, 740, 180, 180);
         g.setColor(Color.gray);     
         super.paint(g);       
    }The JList does not appear neither does the JScrollPane

  • JList in JScrollPane never shows horizontal scroll bar

    I have a JList inside a JScrollPane. When I add a lot of cells to the list, the vertical scroll bar appears as expected. However, if I add a cell that would require lots of horizontal space in which to render, the horizontal scroll bar never appears.
    Is there a way to make the JList take on the width of the widest rendered cell?
    Josh

    Here's some code that will demonstrate the problem. Run the code, then resize the window so that the horizontal scroll bar should appear.
    Code for jds.toys.Main:
    package jds.toys;
    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    public class Main extends JFrame {
         protected DefaultListModel model;
         public Main()
              super("JList in JScrollPane");
              model = new DefaultListModel();
              model.addElement("A very very very very very very long String");
              model.addElement("A very very very very very very very long String");
              model.addElement("A very very very very very very very very long String");
              model.addElement("A very very very very very very very very very long String");
              model.addElement("A very very very very very very very very very very long String");
              model.addElement("A very very very very very very very very very very very long String");
                      JList list = new JList(model);
                      list.setSize(300,300);
                      list.setFixedCellHeight(30);
                      list.setCellRenderer(new MyCellRenderer());
                      JScrollPane scrollPane = new JScrollPane(list);
                      add(scrollPane);
                      setSize(400,400);
        public static void main(String[] args) throws InterruptedException {
             final Main m = new Main();
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                     m.setVisible(true);
    Code for jds.toys.MyCellRenderer:
    package jds.toys;
    import java.awt.Component;
    import java.awt.Graphics;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.ListCellRenderer;
    public class MyCellRenderer extends JPanel implements ListCellRenderer
         private Object currentValue;
         private JList currentList;
         public MyCellRenderer()
         public Component getListCellRendererComponent(     JList list,
                                             Object value,
                                       int index,
                                       boolean isSelected,
                                       boolean cellHasFocus)
              currentValue = value;
              currentList = list;
              return this;
         public void paintComponent(Graphics g)
              int stringLen = g.getFontMetrics().stringWidth(currentValue.toString());
              int ht = currentList.getFixedCellHeight();
              g.setColor(getBackground());
              g.fillRect(0,0,stringLen,ht);
              g.setColor(getForeground());
              g.drawString(currentValue.toString(),0,ht/2);
    }

  • JLists in JScrollPane in JFrame

    im writing a form class it extends JFrame has a number of jtextfields and labels
    im trying to add a JList in a JScrollPane
    the list works alone
    everytime i try adding it to a scrollpane it just doesnt show
    im not getting any errors either
    any ideas
    jstfsklh211

    import java.awt.*;
    import java.sql.*;
    import java.lang.Object;
    import javax.swing.*;
    import java.util.*;
    public class addevent extends JFrame{
         //input fields with options
         JComboBox eventType, salesman;
         JCheckBox flrplan;
         //textfields for input
         JTextField date, location, count, ctlstart, cerstart,
              dinstart, valnum, valstart, npkn, kipot, textbox,
              custinfoID;
         JList ctllist,dinlist, buflist;
         JButton Add, menu;
         //labels for customer info
         JLabel custinfof, custinfol, custinfop, custinfoc;
         //connection variables
         startup bridge = new startup();
         Connection con = null;
         Statement stmt = null;
         ResultSet rs = null;
         Vector vector1 = null;
         private String custID, query;
         int j;
         public addevent(String title){
              this.setTitle(title);
              //header
              JLabel line = new JLabel();
              line.setLocation(0,30);
              line.setSize(900,1);
              line.setBorder(BorderFactory.createMatteBorder(0,0,1,0,Color.black));
              //lists
              itemlist("query");
              ctllist = new JList(vector1);
              ctllist.setSize(200, 250);
              ctllist.setLocation(50, 160);
              itemlist("query");
              dinlist = new JList(vector1);
              dinlist.setSize(200, 250);
              dinlist.setLocation(350, 160);
              itemlist("query");
              buflist = new JList(vector1);
              buflist.setSize(200, 250);
              buflist.setLocation(650, 160);
              textbox = new JTextField();
              textbox.setVisible(false);
              Container contentPane = this.getContentPane();
              JScrollPane ctlpane = new JScrollPane(ctllist);
              JScrollPane dinpane = new JScrollPane(dinlist);
              JScrollPane bufpane = new JScrollPane(buflist);
              contentPane.add(ctlpane);
              contentPane.add(dinpane);
              contentPane.add(bufpane);
              contentPane.add(textbox);
              this.pack();
              this.setSize(900,550);
              this.setLocationRelativeTo(null);
              this.setVisible(true);
              catlist list = new catlist();
         public void salesman(){
              try {
                     query = "select * from salesman;";
                     con = bridge.getConnection();
                     stmt = con.createStatement();
                     rs = stmt.executeQuery(query);
                     while(rs.next())
                          salesman.addItem(rs.getString(1));
              catch(Exception ex){ex.printStackTrace();}
              finally {
                   try {
                        if (rs != null)
                             rs.close();
                   catch(Exception ex) {ex.printStackTrace();}
                   try {
                        if (stmt != null)
                             stmt.close();
                   catch(Exception ex) {ex.printStackTrace();}
         public void eventType(){
              try {
                     query = "select * from eventType;";
                     con = bridge.getConnection();
                     stmt = con.createStatement();
                     rs = stmt.executeQuery(query);
                     while(rs.next())
                          eventType.addItem(rs.getString(1));
              catch(Exception ex){ex.printStackTrace();}
              finally {
                   try {
                        if (rs != null)
                             rs.close();
                   catch(Exception ex) {ex.printStackTrace();}
                   try {
                        if (stmt != null)
                             stmt.close();
                   catch(Exception ex) {ex.printStackTrace();}
         public void itemlist(String query){
              try {
                   vector1 = new Vector();
                   con = bridge.getConnection();
                   stmt = con.createStatement();
                   query = "select distinct type from food;";
                   rs = stmt.executeQuery(query);
                   while(rs.next()) vector1.add(rs.getObject(1));
              catch(Exception ex){ex.printStackTrace();}
              finally {
                   try {
                        if (rs != null)
                             rs.close();
                   catch(Exception ex) {ex.printStackTrace();}
                   try {
                        if (stmt != null)
                             stmt.close();
                   catch(Exception ex) {ex.printStackTrace();}
    }//end classi pulled out about 20 buttons, lables, and textfields
    and mutiple get and sets for the text fields
    Message was edited by:
    jstfsklh211
    Message was edited by:
    jstfsklh211

  • Jscrollpane, jlist and forte

    hi, i putted a jlist into a jscrollpane into my form, but i can see all the elements into the jlist because scrollbars don't appear.
    i already saw some code into this forum, but i neet to know how to do it managing the properties of FORTE, not writing any code.
    can anyone help me?
    thanks
    sandro

    Follow this snippet
    JList list = new JList(model);
    list.setVisibleRowCount(4);
    The rowcount value should be less than the number of items in the list.
    For example,
    the number of items be 10 and visible row count is 4, then the scroll bar will automatically appear.
    Hope this will help you a lot

  • How do i add a Scroll Bar to a  JList Component using absolute positioning?

    I've got a applet whose content pane is set to null. I've create a jlist component on this applet and using absolute positioning set the bounds at
    ListBox1.setBounds(380,10, 500, 500);.
    My problem is creating add a scroll bar to the list box.
    JScrollPane scrollPane = new JScrollPane(ListBox1);
    C.add(scrollPane);
    The above code is what i use and when i run this applet i don't see the list box at all. How do i add a scrollbar to this list box or JList component. Please help.

    You need to setBounds() on the JScrollPane, not the JList.
    The JScrollPane is the component that is being added to the panel.

  • Using jtextpane as jlist cell renderer component

    hi,
    I want to use Jlist (in a Jscrollpane) to list a series of boxes of text. The boxes have to be kept the same width, but the height can vary depending on the amount of text.
    I want to use jtextpane because it wraps automatically on word boundaries... although I am confused by the jtextpane functionality...
    but I just can't seem to crack it: presumably its going to involve
    class MyCellRenderer extends JTextPane implements CellRenderer {
    public Component getListCellRendererComponent( ...
    then what ??? help!
    mike rodent
    PS also, how to make Jlist put a line (a single line) between each of the components in its list... it's no good doing setBorder inside the above method, as you then get 2 lines coalescing between adjacent Jlist elements...

    PS also, how to make Jlist put a line (a single line) between each of
    the components in its list... it's no good doing setBorder inside the
    above method, as you then get 2 lines coalescing between adjacent
    Jlist elements...Who says you need to have a Border with top and bottom lines?

  • How to scroll JList to bottom

    I have a JList on a JScrollPane, and it updates in real-time.
    As the data fills the pane, I want it to scroll to the bottom all the time,
    but it stays at the top. I update as follows:
    String voltString[]=new String[someNumber];
    // Set the strings to stuff--code omitted
    voltList.setListData(voltStrings);
    voltList.ensureIndexIsVisible(voltStrings.length);But it doesn't work. The API for ensureIndexIsVisible says something
    about a JViewPort, but I am not sure what it means--I have it on a
    JScrollPane, not a JViewPort.

    works OK like this
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Testing extends JFrame
      int counter = 0;
      javax.swing.Timer timer;
      public Testing()
        setLocation(200,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        final DefaultListModel lm = new DefaultListModel();
        final JList list = new JList(lm);
        JScrollPane sp = new JScrollPane(list);
        sp.setPreferredSize(new Dimension(100,150));
        getContentPane().add(sp);
        pack();
        setVisible(true);
        ActionListener al = new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            lm.addElement(counter);
            counter++;
            list.ensureIndexIsVisible(lm.size()-1);
            if(counter > 100) timer.stop();}};
        timer = new javax.swing.Timer(100,al);
        timer.start();
      public static void main(String[] args){new Testing();}
    }

  • Illustrating a stack of an unknown size in a JScrollPane

    I designed a maze generation/traversal program and I want to show what happens to the stack in the depth first algorithms that I used. I have a JFrame with a JPanel containing the maze and to the right of the JPanel is a JScrollPane to hold the stack illustration. I have a JPanel inside the JScrollPane and I was wondering how I can start from the bottom of the JPanel in the viewing area of the JScrollPane and continually add JLabels, representing blocks in a stock, to the JPanel, working my way from the bottom up. When a JLabel is added and goes above the top of the current viewspace of the JScrollPane, I'd like for the JScrollPane to scroll up. Also, which layout should I set the JPanel in the JScrollPane to? GridBagLayout?

    Nvm, I used a JList in a JScrollPane. I just have to alter the DefaultListModel so that the top of the list is the end of the vector and not the front.

Maybe you are looking for

  • Macbook Air (2014) Built-in Keyboard and Trackpad Freeze!

    My girlfriend's Macbook Air's keyboard and trackpad got freeze suddenly when she was using Safari. Tried to reboot and they work completely fine in login, but freeze again after login. Then I tried to reset SMC and PRAM, but still didn't fix it. Then

  • Link to an iTunes song?

    Is there a way to post a link from my blog to specific songs in iTunes...some times I want to be able to send people on my blog over to hear the sample of a song I'm blogging about, and who knows, maybe my reco's will lead to sale & make the stock go

  • Error ordering Windows 7 upgrade disc through Arvato

    Hey all, I'm having trouble trying to order a window 7 upgrade disc.  I get through the eligibility page just fine, it's just when I try to enter my billing/shipping address, the site throws an error at me (There was an error with validating your add

  • I want layer or shape width size copy to clipboard for re size my photo before pastit ps cs2 os xp

    i want layer or shape width size copy to clipboard for re size my photo before past into   ps cs2 os xp please give me solution for this [email protected]

  • "connection not available" message on E71

    My Nokia E71 stopped connecting to the internet on 5th of this month and talks have been on with my operator. My phone is able to connect to the internet using another service. I just thought of resetting the phone with a hope that everything can wor