1

I have a JProgressBar on a JPanel as a JList components.The JList components are supposed to update the JProgressBar in every two seconds.But the problem is, I don't know how to pass the value of progress into the JProgressBar. here is my code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class download {
    download(){}
}
class myPanelListCellRenderer extends DefaultListCellRenderer{
    private static final long serialVersionUID = 1L;
    private JPanel downloadPanel,labelStack;
    private JProgressBar downloadProgress;
    private JLabel fileTypeIconLabel,fileNameLabel,downloadInfoLabel,freeLabel;
    public myPanelListCellRenderer(){
        setLayout(null);
        downloadPanel=new JPanel(new BorderLayout(10,20));
        labelStack=new JPanel(new GridLayout(0,1,2,2));
        downloadProgress=new JProgressBar() {
            @Override
            public Dimension getPreferredSize() {
                Dimension d = super.getPreferredSize();
                return new Dimension(400,d.height);
            }
        };
        fileTypeIconLabel=new JLabel("test", SwingConstants.CENTER);
        fileNameLabel=new JLabel("fileNameLabel", SwingConstants.CENTER);
        downloadInfoLabel=new JLabel("downloadInfoLabel", SwingConstants.CENTER);
        freeLabel=new JLabel("freeLabel", SwingConstants.CENTER);
        fileTypeIconLabel.setFont(fileTypeIconLabel.getFont().deriveFont(60f));
        labelStack.add(fileNameLabel);
        labelStack.add(downloadInfoLabel);
        labelStack.add(downloadProgress);
        labelStack.add(freeLabel);  
        labelStack.setOpaque(false);
        downloadPanel.add(fileTypeIconLabel,BorderLayout.LINE_START);
        downloadPanel.add(labelStack,BorderLayout.CENTER);
    }
    @Override
    public Component getListCellRendererComponent(JList<?> list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
        JLabel l = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        downloadPanel.setBackground(l.getBackground());
        fileTypeIconLabel.setText("" + (index + 1));
        return downloadPanel;
    }
}

public class JListPanel implements ActionListener{
    private JPanel btnPanel;
    private JButton jbtAdd,jbtDelete;
    private JFrame jf;
    private download dl;
    DefaultListModel<download> myListModel;
    JList<download>myList;
    public JListPanel(){
        myListModel=new DefaultListModel<download>();
        myList=new JList<download>(myListModel);
        myList.setCellRenderer(new myPanelListCellRenderer());
        myList.setVisibleRowCount(8);
        btnPanel=new JPanel();
        jbtAdd=new JButton("addJpanel");
        jbtDelete=new JButton("delJpanel");
        btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
        btnPanel.add(jbtAdd);
        btnPanel.add(jbtDelete);
        jf=new JFrame("hello");
        for(int i=0;i<6;i++){
            dl=new download();
            myListModel.add(0, dl);
        }
        jf.setLocationByPlatform(true);
        jf.add(btnPanel,BorderLayout.NORTH);
        jf.add(new JScrollPane(myList),BorderLayout.CENTER);
        jbtAdd.addActionListener(this);
        jbtDelete.addActionListener(this);
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public static void main(String args[]){
        new JListPanel();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==jbtAdd){
            addMyPanel();
        }
        else if(e.getSource()==jbtDelete){
            delMyPanel();
        }
    }
    public void addMyPanel(){
        myListModel.add(0,new download());
    }
    public void delMyPanel(){
        int index = myList.getSelectedIndex();
        if (index < 0) {
            JOptionPane.showMessageDialog(
                    myList,
                    "Select a download to delete!",
                    "Select Download",
                    JOptionPane.ERROR_MESSAGE);
        } else {
            myListModel.removeElementAt(index);
        }
    }
}
icook
  • 71
  • 8

1 Answers1

1

Instead of passing the progress value to the JProgressBar directly, let the bar listen for changes from whatever process updates the list's data model. SwingWorker, seen here, is especially convenient for this, as the worker's setProgress() notifies listeners automatically.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • The problem is that the `JProgressBar` is in the `ListCellRenderer` – icook Feb 11 '14 at 14:27
  • 1
    Your `ListModel` should include a `List`, where `DownloadWorker` extends `SwingWorker`. – trashgod Feb 11 '14 at 17:56
  • You may mistake my question,the key point is to change the value of `JProgressBar` in runtime.Concurrency is not my key point right now.The JProgressBar is in the Renderer,But I don't know how to change the value of JProgressBar in a `JList Renderer` – icook Feb 13 '14 at 08:24
  • The `JProgressBar` should be listening for changes; a `PropertyChangeListener` is convenient for this; `SwingWorker` happens to fire a suitable `PropertyChangeEvent`; you can manage your own events, but you'll have to solve the concurrency problem sooner or later; I vote for sooner. – trashgod Feb 13 '14 at 13:59