I am trying to make a list of items, with a title at the top. I have created a frame, with an embedded panel (using BoxLayout, Y_AXIS), with an embedded JLabel, and a JScrollPane with a JList:
JFrame-
-JPanel
-JLabel
-JScrollPane
-JList
For some reason, the JLabel starts about 1/4 of the way to the right, and not left-justified. The JScrollPane starts on the left. I tried creating the JLabel and specifying JLabel(title, horizontal alignment) with SwingConstants.LEFT, but it doesnt' change anything. HELP!
here is the code:
import javax.swing.*;
import java.awt.*;
public class PanelTest {
public static void main(
String[] args) {
Dimension winsize = new Dimension(250, 400);
String frametitle = new String("Panel Test");
String listtitle = new String("Item List");
String[] listcontents = new String[] {"List Item 1", "List Item 2", "List Item 3", "List Item 4"};
MyFrame frame = new MyFrame(winsize, frametitle, listtitle, listcontents);
}
}
class MyFrame extends JFrame {
MyPanel panel;
public MyFrame(Dimension winsize, String frametitle, String listtitle, String[] listcontents) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(frametitle);
setSize(winsize);
panel = new MyPanel(listtitle, listcontents);
add(panel);
setVisible(true);
}
}
class MyPanel extends JPanel {
JLabel label;
MyList list;
public MyPanel(String title, String[] listcontents) {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
label = new JLabel(title);
list = new MyList(listcontents);
add(label);
add(list);
}
}
class MyList extends JScrollPane {
String[] listcontents;
public MyList(String[] listcontents) {
super(new JList(listcontents));
}
}