Search code examples
javaswinglayout-managerjscrollbarnull-layout-manager

JScrollBar Doesn't Do Anything


I'm trying to put a vertical JScrollBar on top of a JPanel. I can add the scoll bar easily enough, but when I run the program, my scroll bar doesn't have any effect. I can drag it up and down, but the contents of the JPanel don't move. I've read all what I can find about how to do this, and it seems very straightforward, but I'm obviously missing something.

Please note that my JPanel layout is set to NULL. Here is the relevant code. Thanks!

public JDlgResults(ArrayList<AnsweredProblem> probList) {
    initComponents();
    pnlResults.setLayout(null); //allows free form placing of JLabels

    /* Iterate over the ArrayList and add each problem to the results table */
    Iterator iter = probList.iterator();
    int row = 1;

    while(iter.hasNext()) {
        addRow(row, iter.next());
        row++;
    }

    JScrollBar jScrollResults = new javax.swing.JScrollBar();
    pnlResults.add(jScrollResults);
    jScrollResults.setBounds(590, 0, 17, 196);
}

Solution

  • I assume that your initComponents() method sets up a bunch of components and adds them to your contentPane and establishes your dialog (whether it be dialog or a frame).

    Simply adding your JScrollPane to that panel wont do the trick.

    Instead, add your pnlResults to the JScrollPane instance and make that your contentPane.