Change element layout in JList in Java
Description
The following code shows how to change element layout in JList.
Example
/*from w w w .j a va 2s . c o m*/
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Modifying Model");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList jlist = new JList(new String[]{"A","B","C","v","A","B","C","v",
"A","B","C","v",
"A","B","C","v","a","d","java2s.com"});
// The default layout orientation is JList.VERTICAL
int orient = jlist.getLayoutOrientation();
// Change the layout orientation to left-to-right, top-to-bottom
jlist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
JScrollPane scrollPane1 = new JScrollPane(jlist);
frame.add(scrollPane1, BorderLayout.CENTER);
frame.setSize(640, 300);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »