2014-09-24 71 views
0

我一直想弄清楚如何使用JList,我似乎無法讓它顯示一個對象到我的GUI。JList不顯示任何對象?

有一個叫拉絲,我試圖添加到JList和它只是似乎沒有表現出類.. 任何幫助,將不勝感激

這裏是我的代碼:

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

回答

3

要了解問題,您需要了解組件只能位於單個容器(它只能有一個父容器)中。

如果您嘗試將該組件添加到另一個容器中,則該組件將從第一個容器中刪除,然後再添加到第二個容器中。

所以,在你的代碼做什麼...

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

...所以,基本上,你已經開始進行的順利,但最終從scrollPane取出fileList並將其添加到fileOptions相反,則增加了(現在是空的)scrollPanefileOptions以及...

刪除fileOptions.add(fileList);,它應該工作,你希望它...

您可能還需要看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

您可以通過使用像setVisibleRowCount和相應的單元格渲染器控制JScrollPane的大小...

+0

OMG你是天才!我一直在試圖解決這個問題的牆上碰到我的頭......謝謝! – Pixelidiot 2014-09-24 03:43:47

+1

其實,我注意到''Test''已被顯示出來,只是不在應該在哪裏......這導致我去查看'JList'和'JScrollPane'在哪裏被使用......並且是的,我自己做了這個:P – MadProgrammer 2014-09-24 03:45:55

+0

嘿,當我嘗試添加一個繪圖對象它不顯示。只有「測試」似乎正確顯示。哦,我確實改變了「fileList.add(drawing);」到「listModel.add(drawing);」 – Pixelidiot 2014-09-24 04:02:06

0

添加到ListModel的對象的默認呈現是爲了簡單顯示對象的toString()

如果要添加自定義對象,則需要提供自定義渲染器。請閱讀Swing教程How to Use Lists中的部分,特別是關於Writing a Custom Cell Renderer的部分,以獲取有關此概念的更多信息。