I am trying to create a GUI in java which contains a JComboBox and JList whose content changes depending on the selected item in the JComboBox.
I want to add a scroolPane to the JList. I'll show you the code and images to clarify the problem:
JPanel listPanel = new JPanel();
Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();
JComboBox<String> tendina;
JList<String> subList;
GridBagConstraints c;
String ricevuti[] = {"Alex", "Ben", "Claire", "Dana", "Ellen", "Felicia", "Gary", "Hailey", "Imogen", "Jay", "Kate", "Alex", "Ben", "Claire", "Dana", "Ellen", "Felicia", "Gary", "Hailey", "Imogen", "Jay", "Kate"};
JList ricList = new JList(ricevuti);
String inviati[] = {"Lara", "Megan", "Nikole", "Oscar", "Paige", "Quentin", "Ralph", "Sara", "Thelma", "Victoria", "William"};
JList invList = new JList(inviati);
String ricInv[] = {"Seleziona un item..", "Ricevuti", "Inviati"};
Inbox() {
//...
listPanel.setLayout(new GridBagLayout());
listPanel.setBackground(Color.BLUE);
/*** ComboBox ***/
tendina = new JComboBox<String>(ricInv);
tendina.addActionListener(this);
tendina.setEditable(false);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(-295, 10, 0, 0); //top, left, bottom, right
c.weightx = 1;
listPanel.add(tendina, c);
/*** Lista ***/
subList = new JList<String>();
subList.setPreferredSize(new Dimension(80, 280));
subList.setVisibleRowCount(10);
subList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollList = new JScrollPane(subList);
subItems.put(ricInv[1], ricevuti);
subItems.put(ricInv[2], inviati);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(30, 10, 0, 0); //top, left, bottom, right
c.weightx = 1;
**[1]**listPanel.add(subList, c);
**[2]**listPanel.add(scrollList, c);
//...
}
The problem is that when I add the scrollList[2] to the listPanel, the layout is messed up.
If I add the sublist [1] to listPanel, then the layout is right but obviously I don't have scrollList.
How can I add the scrollList without the layout is changed?
This is what happens if I add the subList to listPanel[1]:
http://www.mediafire.com/view/daye4rkx1tyhwbq/sublist.png
This is what happens if I add the scrollList to listPanel[2]:
http://www.mediafire.com/view/jfiky6j686e2h58/scroll.png
Thanks
As regards the height I realized that the problem depended from subList.setVisibleRowCount(16);
.
But how can I adjust the width?