Reputation: 37
I've been trying to figure out how to use JList and I cant seem to get it to display an object into my GUI.
there's a class called Drawing that I'm trying to add to the JList and it just doesnt seem to show.. Any help would be greatly appreciated
here's my code:
public class DrawingDisplayer extends JPanel implements ActionListener, ListSelectionListener {
JLabel title;
JButton draw,pause,clear,
open,close,
lines,background;
JSlider speedSlider;
JProgressBar progress;
Drawing drawing;
JFileChooser chooser;
JList fileList;
DefaultListModel listModel;
JPanel drawPanel;
JScrollPane scrollPane;
public DrawingDisplayer(){
title = new JLabel("The Drawing Displayer");
title.setHorizontalAlignment(JLabel.CENTER);
title.setFont(new Font("Serif", Font.BOLD, 24));
draw = new JButton("Draw");
pause = new JButton("Pause");
clear = new JButton("Clear");
speedSlider = new JSlider();
progress = new JProgressBar();
open = new JButton("Open Drawing");
close = new JButton("Close Drawing");
listModel = new DefaultListModel();
fileList = new JList(listModel);
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fileList.addListSelectionListener(this);
fileList.setVisibleRowCount(10);
scrollPane = new JScrollPane(fileList);
scrollPane.setPreferredSize(new Dimension(200,250));
lines = new JButton("Lines");
background = new JButton("Background");
setLayout(new BorderLayout());
//Draw Panel
drawPanel = new JPanel();
drawPanel.setBorder(BorderFactory.createTitledBorder("Drawing Area"));
//Drawing Speed
JPanel drawSpeed = new JPanel();
drawSpeed.setPreferredSize(new Dimension(300,200));
drawSpeed.setBorder(BorderFactory.createTitledBorder("Drawing Speed"));
drawSpeed.add(draw);
drawSpeed.add(pause);
drawSpeed.add(clear);
drawSpeed.add(speedSlider);
drawSpeed.add(progress);
//File Options
JPanel fileOptions = new JPanel();
fileOptions.setPreferredSize(new Dimension(300,350));
fileOptions.setBorder(BorderFactory.createTitledBorder("File Options"));
open.addActionListener(this);
close.addActionListener(this);
fileOptions.add(open);
fileOptions.add(close);
fileOptions.add(fileList);
fileOptions.add(scrollPane);
//Colour Options.
JPanel colourOptions = new JPanel();
colourOptions.setPreferredSize(new Dimension(300,200));
colourOptions.setBorder(BorderFactory.createTitledBorder("Colour Options"));
colourOptions.add(lines);
colourOptions.add(background);
//Control Panel
JPanel controlPanel = new JPanel();
controlPanel.setPreferredSize(new Dimension(325,200));
controlPanel.add(drawSpeed);
controlPanel.add(fileOptions);
controlPanel.add(colourOptions);
chooser = new JFileChooser(".");
add(title, BorderLayout.NORTH);
add(controlPanel,BorderLayout.WEST);
add(drawPanel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == open){
chooser = new JFileChooser(".");
if(chooser.showOpenDialog(null) == chooser.APPROVE_OPTION){
drawing = new Drawing(chooser.getSelectedFile());
fileList.add(drawing);
listModel.addElement("test");
}
}
else if (e.getSource() == close){
}
}
public void valueChanged(ListSelectionEvent e) {
}
public static void main(String[] args){
DrawingDisplayer panel = new DrawingDisplayer();
JFrame frame = new JFrame("drawing");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Upvotes: 0
Views: 1092
Reputation: 347184
To understand the problem you're having you need to understand that a component can only reside within a single container (it can only have a single parent).
If you try and add the component to another container, it is removed from the first before been added to the second.
So, in your code you do...
fileList = new JList(listModel);
//...
// Add fileList as the view for the scrollpane...
scrollPane = new JScrollPane(fileList);
scrollPane.setPreferredSize(new Dimension(200, 250));
//...
// Remove fileList from the scrollpane and add it to fileOptions...
fileOptions.add(fileList);
fileOptions.add(scrollPane);
...So, basically, you've started out well, but ended up removing the fileList
from the scrollPane
and adding it to the fileOptions
instead, then added the (now empty) scrollPane
to the fileOptions
as well...
Remove fileOptions.add(fileList);
and it should work as you expect it...
You might also want to take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?.
You can control the size of the JScrollPane
by using things like setVisibleRowCount
and appropriate cell renderers ...
Upvotes: 3
Reputation: 324108
The default rendering of an object added to the ListModel is to simple display the toString()
of the object.
If you are adding a custom object, then you need to provide a custom renderer. Read the section from the Swing tutorial on How to Use Lists, especially the section on Writing a Custom Cell Renderer
for more information on this concept.
Upvotes: 0