Last active
December 23, 2015 01:29
-
-
Save kaydell/6560588 to your computer and use it in GitHub Desktop.
This is a study of getting scroll bars to work in Java's Swing API, using a JScrollPane
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Dimension; | |
import java.awt.FlowLayout; | |
import javax.swing.JFrame; | |
import javax.swing.JList; | |
import javax.swing.JScrollPane; | |
import javax.swing.SwingUtilities; | |
/** | |
* This class is a study in learning how to get scroll bars to work for a JList using a JScrollPane. | |
* | |
* The numbers have been "fudged" to get things to work. But it does work. | |
* | |
* @author kaydell | |
* | |
*/ | |
public class JScrollPaneStudy extends JFrame { | |
public JScrollPaneStudy() { | |
// use a FlowLayout LayoutManager because it is just simple | |
setLayout(new FlowLayout()); | |
// create the top left JList | |
String[] array2 = new String[50]; | |
for (int i = 0; i < 50; i++) { | |
array2[i] = Integer.toString(i); | |
} | |
JList<String> topLeftList = new JList<String>(array2); | |
// topLeftList.addListSelectionListener(this); | |
JScrollPane jScrollPane = new JScrollPane(topLeftList); | |
// usually, we want to set the preferred sizes of things. There | |
// is also size, minimum size, and maximum size | |
topLeftList.setPreferredSize(new Dimension(300, 2000)); // make room in the JList for many lines. This should really grow as items are added | |
jScrollPane.setPreferredSize(new Dimension(300, 300)); | |
// add the scroll pane to this window | |
add(jScrollPane); | |
// finish initializing this window | |
pack(); // size this window | |
setLocationRelativeTo(null); // center this window | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new JScrollPaneStudy().setVisible(true); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment