Java Swing Samples

Download a file in Worker(Background) Thread:

 

public static class DownloadResource extends SwingWorker<Void, Void> {
	
	@Override
	public Void doInBackground() {
		
				
		SearchResultModel model = resultList;			
		ListSelectionModel selectionModel = checkListManager.getSelectionModel();			
		
		for(int i=0; i<model.getSize(); i++) {
			
			if(selectionModel.isSelectedIndex(i) == true) {					
				
				String downloadURL = model.getElementAt(i).address;				    
				if(downloadURL.contains("abc")) {
					downloadFile(model.getElementAt(i).address);
				} else {
					System.out.println("Download url does not contain abc");
				}
			}
		}
		return null;
	}
}

public static void downloadFile(String downloadURL){
	try {
		
		URL url=new URL(downloadURL);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		int filesize = connection.getContentLength();
		float totalDataRead=0;
		java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
		java.io.FileOutputStream fos = new java.io.FileOutputStream("C:/Users/Public/Downloads"+getFileName(downloadURL));
		java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
		byte[] data = new byte[1024];
		int i=0;
		while( (i=in.read(data,0,1024) ) >= 0 )
		{
			//totalDataRead=totalDataRead+i;
			bout.write(data,0,i);
			//float Percent=(totalDataRead*100)/filesize;
			//current.setValue((int)Percent);
		}	
		bout.close();
		in.close();
	}
	catch(Exception e) {
		 javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,e.getMessage(), "Error",
				 javax.swing.JOptionPane.DEFAULT_OPTION);
	}
}

Java Swing custom list with checkbox

public class CheckListCellRenderer extends JPanel implements ListCellRenderer { 
	
    private ListCellRenderer delegate; 
    private ListSelectionModel selectionModel; 
    private JCheckBox checkBox = new JCheckBox(); 
 
    public CheckListCellRenderer(ListCellRenderer renderer, ListSelectionModel selectionModel){ 
        this.delegate = renderer; 
        this.selectionModel = selectionModel; 
        setLayout(new BorderLayout()); 
        setOpaque(false); 
        checkBox.setOpaque(false); 
    } 
 
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
    	
        Component renderer = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
        checkBox.setSelected(selectionModel.isSelectedIndex(index)); 
        removeAll(); 
        add(checkBox, BorderLayout.WEST); 
        add(renderer, BorderLayout.CENTER); 
        return this;
    } 
}


public class CheckListManager extends MouseAdapter implements ListSelectionListener, ActionListener{ 
    
	private ListSelectionModel selectionModel = new DefaultListSelectionModel(); 
    private JList list = new JList(); 
    int hotspot = new JCheckBox().getPreferredSize().width; 
 
    public CheckListManager(JList list){ 
        this.list = list; 
        
        list.setCellRenderer(new CheckListCellRenderer(list.getCellRenderer(), selectionModel)); 
        
        list.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_FOCUSED); 
        list.addMouseListener(this); 
        selectionModel.addListSelectionListener(this); 
    } 
 
    public ListSelectionModel getSelectionModel(){ 
        return selectionModel; 
    } 
 
    private void toggleSelection(int index){ 
        if(index<0) 
            return; 
 
        if(selectionModel.isSelectedIndex(index)) 
            selectionModel.removeSelectionInterval(index, index); 
        else 
            selectionModel.addSelectionInterval(index, index); 
    } 
 
    public void mouseClicked(MouseEvent me){ 
        int index = list.locationToIndex(me.getPoint()); 
        if(indexlist.getCellBounds(index, index).x+hotspot) 
            return; 
        toggleSelection(index); 
    } 
 
    public void valueChanged(ListSelectionEvent e){ 
        list.repaint(list.getCellBounds(e.getFirstIndex(), e.getLastIndex())); 
        System.out.println("list selected index->"+e.getFirstIndex());
        System.out.println("value selected -->"+list.getSelectedValue());
        
        System.out.println("value selected -->"+list.getSelectedValuesList());
        
    } 
 
    public void actionPerformed(ActionEvent e){ 
        toggleSelection(list.getSelectedIndex()); 
    } 
} 

Usage:

CheckListManager checkListManager;
checkListManager = new CheckListManager(list);
ListSelectionModel selectionModel = checkListManager.getSelectionModel();

if(selectionModel.isSelectedIndex(i) == true)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s