0

I have JList code that I want to add to a scroll button on the side since the list is longer than the text box window. that's the code:

final JList list = new JList();
        list.setBounds(36, 23, 366, 241);
        contentPane.add(list);
File folder = new File(FILETOSTART);
                        File[] listOfFiles = folder.listFiles();

                        for (int i = 0; i < listOfFiles.length; i++) {
                            if (listOfFiles[i].isFile()) {
                                System.out.println("File " + listOfFiles[i].getName());
                                list.setListData(listOfFiles);                              
                            } else if (listOfFiles[i].isDirectory()) {
                                System.out.println("Directory " + listOfFiles[i].getName());
                            }
                        }
                    }
                    in.close();
                }
                catch (Exception exception) {
                    exception.printStackTrace();

How can I add this scroll side button?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 03 '16 at 08:40

1 Answers1

2

If by 'button' you mean 'scroll bar' then it can be1 as simple as:

contentPane.add(new JScrollPane(list));

Instead of:

contentPane.add(list);
  1. Except when using absolute positioning, as seems might be the case here.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I tried to add it but it didnt work... I wanted to use this object - JScrollBar – Stevie Sommers May 04 '16 at 11:50
  • *"I tried to add it but it didnt work..."* 1) What do you expect us to do about that when you have not posted an MCVE/SSCCE as I suggested in my 2nd comment? Read both documents. 2) But the reason it did not work is most likely revealed in my first comment - using `null` layouts. 3) *"I wanted to use this object - JScrollBar"* Yes, a `JScrollPane` usually includes a vertical scroll bar for a `JList`, but it needs to work with **layout managers** to do its job properly. – Andrew Thompson May 04 '16 at 12:06