Results 1 to 5 of 5
- 12-27-2011, 11:24 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 380
- Blog Entries
- 24
- Rep Power
- 10
Why can't I add both a JList and a JScrollPane to my JFrame?
Hi everyone, here is the code:
Java Code:public class ListFrame extends JFrame { JList myJList; String[] colorrs; public ListFrame() { super("This is a List Frame"); this.setLayout(new FlowLayout()); colorrs = new String[6]; colorrs[0] = "Red"; colorrs[1] = "Blue"; colorrs[2] = "Green"; colorrs[3] = "Black"; colorrs[4] = "White"; colorrs[5] = "Purple"; myJList = new JList(); myJList.setListData(colorrs); myJList.setVisibleRowCount(4); JScrollPane scrollPane = new JScrollPane(myJList); this.add(scrollPane); //this.add(myJList); }// end Constructor }//end Class
But:
If I remove the comment slashes from last statement I have(//this.add(myJList);), I am expecting to see both the JScrollPane and myJList.
However I only get an ugly JList with no scrolling option. JScrollPane gets invisible.
Any help?
Thanks.
-
Re: Why can't I add both a JList and a JScrollPane to my JFrame?
A component can only be added once to a visible GUI. If you try to add it twice, the last component only is seen. To remedy the situation, use two JLists that share the same model.
For example,
Java Code:import java.awt.FlowLayout; import javax.swing.*; public class ListFrame extends JFrame { JList myJList; String[] colorrs; public ListFrame() { super("This is a List Frame"); this.setLayout(new FlowLayout()); colorrs = new String[6]; colorrs[0] = "Red"; colorrs[1] = "Blue"; colorrs[2] = "Green"; colorrs[3] = "Black"; colorrs[4] = "White"; colorrs[5] = "Purple"; myJList = new JList(); myJList.setListData(colorrs); myJList.setVisibleRowCount(4); JScrollPane scrollPane = new JScrollPane(myJList); this.add(scrollPane); JList newList = new JList(myJList.getModel()); this.add(newList); } public static void main(String[] args) { ListFrame listFrame = new ListFrame(); listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); listFrame.pack(); listFrame.setLocationRelativeTo(null); listFrame.setVisible(true); } }
Last edited by Fubarable; 12-27-2011 at 11:30 PM.
- 12-27-2011, 11:28 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 380
- Blog Entries
- 24
- Rep Power
- 10
Re: Why can't I add both a JList and a JScrollPane to my JFrame?
Thanks for the quick and very clear answer.
-
- 12-29-2011, 09:23 AM #5
Similar Threads
-
JList inside JScrollPane is not displaying
By _SAS in forum AWT / SwingReplies: 2Last Post: 07-31-2011, 09:45 PM -
Link one JList to another JList
By mib1bee in forum AWT / SwingReplies: 1Last Post: 12-31-2010, 08:10 PM -
Link one JList to another JList
By mib1bee in forum Advanced JavaReplies: 1Last Post: 12-30-2010, 07:35 PM -
Link one JList to another JList
By mib1bee in forum New To JavaReplies: 1Last Post: 12-30-2010, 07:24 PM -
jList - add/remove items and query - using jFrame Form
By ArneBassez in forum AWT / SwingReplies: 2Last Post: 12-01-2010, 01:42 PM
Bookmarks