Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JComboBox with scroll bars.

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default JComboBox with scroll bars.

    Hello,

    I have to make a graphical application, which has a JComboBox and scroll bars. I tried quite a few things, nothing worked and I don't know why. Here is the code I currently have.

    package Java24;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class HR16ComboScroll extends JFrame{
    	//Hour sixteen, activity one.	
     
     
    	public HR16ComboScroll(){
    		super("Combo Scroll");
    		setSize(400,600);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
    		FlowLayout flo = new FlowLayout();
    		setLayout(flo);
     
    		String[] contents = new String[]{"Alem", "Habtam", "Desta", "Almaz", 
    				"Zewdit", "Abraham", "Tsehaye"};
    		JScrollBar pane = new JScrollBar();
    		JComboBox list = new JComboBox(contents);
    		list.addItem(pane);
    		add(list);
     
    		setVisible(true);
    	}
     
    	//run
    	public static void main(String[] args){
    		HR16ComboScroll pane = new HR16ComboScroll();
    	}
    }
    Last edited by Melawe; October 9th, 2011 at 07:04 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JComboBox with scroll bars.

    I moved your post to the Swing forum - perhaps get more attention there. And ++ for the SSCCE.

    I'm not sure what you mean when you say "which has a JComboBox and scroll bars". How do you want these components organized?

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: JComboBox with scroll bars.

    Quote Originally Posted by copeg View Post
    And ++ for the SSCCE.
    Sorry not sure what you mean.

    Quote Originally Posted by copeg View Post
    I'm not sure what you mean when you say "which has a JComboBox and scroll bars". How do you want these components organized?
    Quote from book: "Create a GUI that includes a combo box in a scroll pane.".

    Edit:
    It does compile but the list doesn't have scroll bars, rather a long string, I don't know what the string means though(looks kinda like an exception).
    Last edited by Melawe; October 9th, 2011 at 11:25 PM.

  4. #4
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: JComboBox with scroll bars.

    Updated code:
    public class HR16ComboScroll extends JFrame{
     
    	public HR16ComboScroll(){
    		super("Combo Scroll");
    		setSize(200,50);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
    		FlowLayout flo = new FlowLayout();
    		setLayout(flo);
     
    		String[] contents = new String[]{"Alem", "Habtam", "Desta", "Almaz", 
    				"Zewdit", "Abraham", "Tsehaye"};		
    		JComboBox list = new JComboBox(contents);
    		JScrollPane pane = new JScrollPane(list,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    		JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		//list.addItem(pane);
    		add(pane);
     
    		setVisible(true);
    	}
     
    	//run
    	public static void main(String[] args){
    		HR16ComboScroll pane = new HR16ComboScroll();
    	}
    }

    The drop down list currently has the names in the contents array only. Problem: scroll bars should be within the combo box; not the other way around.
    Last edited by Melawe; October 9th, 2011 at 11:53 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JComboBox with scroll bars.

    Sorry not sure what you mean.
    Just a thanks for the short, self contained, complable example (sscce).

    JComboBox by default already has a JScrollPane capability within the popup window. You can use the method setMaximumRowCount() to set the number of rows displayed you wish to display - the rest are viewable by scrolling

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: JComboBox with scroll bars.

    Quote Originally Posted by copeg View Post
    Just a thanks for the short, self contained, complable example (sscce).
    Ah, yourwelcome.

    Quote Originally Posted by copeg View Post
    JComboBox by default already has a JScrollPane capability within the popup window. You can use the method setMaximumRowCount() to set the number of rows displayed you wish to display - the rest are viewable by scrolling
    Oh, hmm, I think I've completed the assignment. If so, the scroll bar is useless, do you think I completed it?

  7. #7
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: JComboBox with scroll bars.

    I guess I completed it. Thanks for you help!

Similar Threads

  1. [SOLVED] JScrollPane cant paint correctly when scroll
    By fuweng in forum AWT / Java Swing
    Replies: 4
    Last Post: December 6th, 2011, 01:27 PM
  2. JAVA netbeans - GUI how to get my scroll bar to work?!
    By camel in forum AWT / Java Swing
    Replies: 5
    Last Post: April 18th, 2011, 07:15 AM
  3. 1 vertical scroll bar fro 2 jtextareas
    By fortune2k in forum AWT / Java Swing
    Replies: 8
    Last Post: March 4th, 2011, 02:04 PM
  4. Replies: 1
    Last Post: May 21st, 2009, 03:41 AM
  5. Java Program to add scroll bars to JTextArea using JScrollPane
    By Flash in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 21st, 2009, 03:41 AM