Results 1 to 9 of 9
Thread: scrollbar not coming on jlist
- 06-21-2010, 03:12 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
scrollbar not coming on jlist
hi all.. i am trying to attach jscrollbar to jlist
here is my code
but it is not working out...only list is appearing and but not scrollpaneJava 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);
- 06-21-2010, 03:23 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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!
- 06-21-2010, 05:11 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
-
- 06-21-2010, 06:06 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
is it ok sir...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(); } }
-
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.
- 06-21-2010, 06:44 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
-
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(); } }); } }
- 06-22-2010, 11:07 AM #9
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
Similar Threads
-
ScrollBar in a JList
By lupo in forum AWT / SwingReplies: 2Last Post: 05-20-2010, 10:42 PM -
JList with ScrollBar
By Psyclone in forum AWT / SwingReplies: 5Last Post: 02-12-2010, 04:02 AM -
Please help output not coming
By majin_harish in forum New To JavaReplies: 2Last Post: 02-05-2009, 09:45 AM -
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:10 AM -
How can i know from which jsp request is coming?
By vishnujava in forum Java ServletReplies: 1Last Post: 08-06-2008, 01:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks