A JList in a ScrollPane not showing
Hello,
I have a slight problem with making a JList scrollable. Here's a piece of the code:
private JScrollPane getCdList()throws SQLException, NoDBConnectionException, IllegalSQLException, DBException{
ArrayList<String> nameList =new ArrayList<String>();
ArrayList<int[]> cdList = order.getCds();
int size = cdList.size();
for(int i = 0; i < size; i++){
int cdId = cdList.get(i)[0];
nameList.add(DatabaseUtility.Search.getCd(cdId).getTitle());
}
JList list =new JList(nameList.toArray());
JScrollPane p =new JScrollPane(list);
p.setBounds(0,0,100,50);
p.add(list);
return p;
}
As you see at the bottom, a new JList is made. I know the JList works because it just displayed all it's content distorted. But now I put it in a ScrollPane and the contents of the JList just won't show. I can give it colors and stuff with p.setBackground() etc. but it won't show.
Any ideas?
Thanks!
[1488 byte] By [
Roekemoes] at [2007-11-26 12:17:17]

# 1
You are already adding the list to the scrollpane when you pass it to the constructor. If you want to change it, call setViewportView(Component)
from the API
/**
* Creates a viewport if necessary and then sets its view. Applications
* that don't provide the view directly to the <code>JScrollPane</code>
* constructor
* should use this method to specify the scrollable child that's going
* to be displayed in the scrollpane. For example:
* <pre>
* JScrollPane scrollpane = new JScrollPane();
* scrollpane.setViewportView(myBigComponentToScroll);
* </pre>
* Applications should not add children directly to the scrollpane.
*
* @param view the component to add to the viewport
* @see #setViewport
* @see JViewport#setView
*/
public void setViewportView(Component view)
~Tim
# 2
Thanks but the only issue is that it is still not showing. It adds the ScollPane appearantly but won't display the List inside of it.
# 3
This works for me:
public static void main(String[] args) {
List<String> nameList = Arrays.asList("Larry", "Curly", "Moe");
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList list = new JList(nameList.toArray());
list.setVisible(true);
JScrollPane p = new JScrollPane(list);
frame.setBounds(150,150,350,450);
p.setBounds(0,0,200,250);
frame.getContentPane().add(p);
frame.pack();
frame.setVisible(true);
}
~Tim
Edited to fix code tags
# 4
Did you get rid of the p.add(list) line? that is causing the problem. ~Tim
# 5
or even this without the silly 1.5 code ;)
import javax.swing.*;
import java.awt.*;
public class KelVarnson extends JDialog
{
public KelVarnson()
{
super ();
setTitle("Soft Pretzels from da boulevard");
setLayout(new FlowLayout());
setUp();
setSize(200, 200);
setLocationRelativeTo(null);
setVisible(true);
}
public void setUp()
{
String data[] = {"dizzy", "duffymo", "yawmark", "JosAH", "DrClap", "camickr"};
JList jlist = new JList(data);
jlist.setVisibleRowCount(1);
JScrollPane spane = new JScrollPane(jlist);
this.getContentPane().add(spane);
}
public static void main (String[] args)
{
new KelVarnson();
}
}
# 6
Yeah I got rid of that. The thing is the Panel is part of a whole Gui.The there's the main frame:Mainframe(JFrame)|_MainPanel(JPanel, in the middle of the Gui) |_CartLine(JPanel) |_JScrollPaneso it's nested quite a bit. Could that be it?
# 7
I doubt it. I certainly could have placed a JPanel within a JPanel within a JPanel within a JDialog and have all components realized.
There are many tutorials out there.
Also, this is a Swing question which rightly belongs in the Swing forum.
Message was edited by:
filestream
# 8
> Yeah I got rid of that. The thing is the Panel is
> part of a whole Gui.
> The there's the main frame:
>
> Mainframe(JFrame)
> |_MainPanel(JPanel, in the middle of the
> Gui)
> |_CartLine(JPanel)
>|_JScrollPane
> quite a bit. Could that be it?
I agree with filestream that thisis most likely not the problem. Post the code where you are calling this function, and adding the JScrollPane to the panel.
~Tim