Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default scrollbar not coming on jlist

    hi all.. i am trying to attach jscrollbar to jlist
    here is my code
    Java Code:
    JDialog search = new JDialog(frame);
    JList list = new JList(cname);
    JScrollPane pane = new JScrollPane(list);
    JPanel listPanel = new JPanel(new BorderLayout());
    listPanel.add(pane);
    JButton button = new JButton("Select");
    search.add(listPanel,BorderLayout.CENTER);
    search.add(button,BorderLayout.SOUTH);
    search.setVisible(true);
    but it is not working out...only list is appearing and but not scrollpane

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Is the JList the same size or larger than the viewport of the JScrollPane that holds it? If not, then the scrollbars won't appear.

    If this doesn't help you, then your best bet is to create a small compilable program that we can run and that demonstrates your problem so that we can see with our own eyes the issue and can manipulate your code for a solution.

    Suerte!

  3. #3
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Is the JList the same size or larger than the viewport of the JScrollPane that holds it? If not, then the scrollbars won't appear.

    If this doesn't help you, then your best bet is to create a small compilable program that we can run and that demonstrates your problem so that we can see with our own eyes the issue and can manipulate your code for a solution.

    Suerte!
    then how to adjust the viewport of JScrollPane...
    i am getting one element in the list still the scroll bar isn't coming

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    public class ScrollBar{
    public void go(){
    JFrame frame=new JFrame("example");
    JDialog dialog=new JDialog(frame);
    dialog.modal(true);
    dialog.setSize(300,400);
    String[] cname={"abc","def","ghi","xyz"};
    JList list=new JList(cname);
    JScrollpane scroll=new  JScrollPane(list);
    JPanel holdingPanel=new JPanel(new BorderLayout());
    holdingPanel.add(scroll);
    dialog.add(holdingPanel);
    dialog.setVisible(true);
    frame.setVisible(true);
    }
    public static void main(String args[])
    {
    ScrollBar sc = new ScrollBar();
    sc.go();
    }
    }
    is it ok sir...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your use of BorderLayout will stretch the JScrollPane and its viewport to be the same size as your dialog and much bigger than the JList held by the JScrollPane, so it stands to reason that the scroll bars will not be shown. You can either make your JScrollPane smaller, or add more data, or you can construct the JScrollPane to always show vertical scrollbars no matter by using the appropriate constants in your JScrollPane constructor.

  7. #7
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your use of BorderLayout will stretch the JScrollPane and its viewport to be the same size as your dialog and much bigger than the JList held by the JScrollPane, so it stands to reason that the scroll bars will not be shown. You can either make your JScrollPane smaller, or add more data, or you can construct the JScrollPane to always show vertical scrollbars no matter by using the appropriate constants in your JScrollPane constructor.
    how to make JScrollPane smaller because data will be variable in number...
    should i use setBounds for JScrollPane????

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by silversurfer2in View Post
    how to make JScrollPane smaller because data will be variable in number...
    should i use setBounds for JScrollPane????
    Never use setBounds as you should always be using a layout manager. You may sometimes use setPreferredSize, but should even try to avoid this. Here's an example modified from code I posted earlier that shows layout managers and JScrollPanes where the vertical scroll pane is always shown. Resize this to see what I mean:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class QuickLayoutDemo {
      
      private static String[] cname = {
          "abcdefghijkl", "def", "ghi", "xyz",
          "abcdefghijkl", "def", "ghi", "xyz",
          "abcdefghijkl", "def", "ghi", "xyz",
          "abcdefghijkl", "def", "ghi", "xyz",
      };
      
      private static void createAndShowUI() {
        int gap = 10;
        JPanel centerPanel = new JPanel(new GridLayout(1, 2, gap/2, 0));
        
        centerPanel.add(createConvertPane("Left Title"));
        centerPanel.add(createConvertPane("Right Title"));
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(new JButton("Do it!"));
        
        JPanel mainPanel = new JPanel(new BorderLayout(gap, gap));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        mainPanel.add(bottomPanel, BorderLayout.SOUTH);
    
        JFrame frame = new JFrame("QuickLayoutDemo");
        frame.getContentPane().add(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      private static JPanel createConvertPane(String title) {
        ButtonGroup btnGroup = new ButtonGroup();
        JRadioButton rBtn1 = new JRadioButton("Button 1");
        JRadioButton rBtn2 = new JRadioButton("Button 2");
        JPanel radioPanel = new JPanel(new GridLayout(1, 0));
        radioPanel.add(rBtn1);
        radioPanel.add(rBtn2);
        btnGroup.add(rBtn1);
        btnGroup.add(rBtn2);
        JList list = new JList(cname);
        JScrollPane pane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        
        JPanel convertPane = new JPanel(new BorderLayout());
        convertPane.setBorder(BorderFactory.createTitledBorder(title));
        convertPane.add(new JScrollPane(pane), BorderLayout.CENTER);
        convertPane.add(radioPanel, BorderLayout.SOUTH);
        return convertPane;
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

  9. #9
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

Similar Threads

  1. ScrollBar in a JList
    By lupo in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2010, 11:42 PM
  2. JList with ScrollBar
    By Psyclone in forum AWT / Swing
    Replies: 5
    Last Post: 02-12-2010, 05:02 AM
  3. Please help output not coming
    By majin_harish in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:45 AM
  4. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:10 AM
  5. How can i know from which jsp request is coming?
    By vishnujava in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2008, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •