0

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);
}



}

enter image description here

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Pixelidiot
  • 47
  • 1
  • 7

2 Answers2

3

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 ...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • OMG YOU'RE A GENIUS!! I've been hitting my head against a wall trying to figure this out...THANK YOU!!! – Pixelidiot Sep 24 '14 at 03:43
  • 1
    Actually, I noticed that `"Test"` was been displayed, just not where it should be...which lead me to look into how the `JList` and `JScrollPane` where been used...and yes, I've done this myself :P – MadProgrammer Sep 24 '14 at 03:45
  • hey, when I try add a Drawing Object it doesn't show. Only "test" seems to display properly. Oh and I did change "fileList.add(drawing);" to "listModel.add(drawing);" – Pixelidiot Sep 24 '14 at 04:02
  • 1
    What is `Drawing`? Does it have a `toString` implementation? Have you tried using a custom list cell renderer? – MadProgrammer Sep 24 '14 at 04:03
  • Omg I'm so silly, I set my toString to return null as I wasn't finished with it, so it WAS being displayed but with no text. Thanks again :D – Pixelidiot Sep 24 '14 at 04:11
0

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.

camickr
  • 321,443
  • 19
  • 166
  • 288