Results 1 to 6 of 6
  1. #1
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default Adhoc selection & removal of JLabels

    I have a JPanel that has thumbnails added to it. I would like to:

    1. Have the thumbnail's border set to red when the thumbnail is clicked on
    2. Get the index of the thumbnail as per it's position in the JPanel
    3. When the "Remove" button is pressed, I'd like the selected thumbnail ( one with red border ) to be removed from the JPanel

    How can I do the aforementioned?

    Below is the code for adding the thumbnails to the JPanel:

    Java Code:
    class ButtonHandler implements ActionListener{
    		
    		public void actionPerformed(ActionEvent event){
    			
    			File[] selectedFiles;
    			
    			FileBrowser browser = new FileBrowser();
    			// Get selected files from JFileChooser
    			selectedFiles = browser.browse();
    			
    			filesManager.setSelectedFiles(selectedFiles);
    			
    			Thumbnailer thumbnails = new Thumbnailer();
    			// Create thumbnails from selectedFiles
    			thumbnails.createThumbnails(selectedFiles);
    			
    			for (Image thumbnailImage : thumbnails.getThumbnails()){
    				thumbnailPanel.add(new JLabel(new ImageIcon(thumbnailImage)));
    			}
    			thumbnailScroll.revalidate();
    			thumbnailScroll.repaint();
    			
    		} // end actionPerformed method
    		
    	} // end inner class ButtonHandler
    Last edited by dan0; 03-09-2009 at 06:33 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could use a grid of JToggleButtons all held in a collection as well, add an action listener to each toggle button such that if it has been pressed, it will check to see if it is selected and if so, change its border to red, or if not to lightgrey. Toggle button's know their state: selected or not, and this can be used to determine if the button should be removed from the collection and the grid.

  3. #3
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    I switched to JToggleButtons and so far, they've been easy to use and perfect for what I wanted to do.

    However, removing the JToggleButton(s) from a collection and grid has been a bit difficult.

    The problem is: the thumbnails in the panel are not created directly from the ArrayList, but rather adhoc selections that are later stored in the ArrayList. This means that the thumbnails indices as per their container match the indices in the ArrayList - i.e. [0] index in the panel matches the [0] index in the ArrayList. I thought I could get the indices of the selected JToggleButtons, remove them from the panel and then using the same indices remove the elements from the ArrayList. But I can't figure out how to get the indices of the selected JToggleButtons from the JPanel containing the JToggleButtons

    Does my problem statement make any sense?
    To clarify, I've add the pertinent 3 classes' code below. If further clarification is needed, please let me know. Thanks.

    FileBrowser:
    Java Code:
    package projects.web;
    
    import javax.swing.JFileChooser;
    import java.io.File;
    import java.util.List;
    import java.util.ArrayList;
    
    
    public class FileBrowser{
    	
    	private final JFileChooser fileChooser = new JFileChooser();
    	private List<File> selectedFilesList = new ArrayList<File>();
    	
    	public FileBrowser(){
    		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    		fileChooser.setMultiSelectionEnabled(true);
    	} // end constructor
    	
    	public File[] browse(){
    		if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
    			for (File selectedFile : fileChooser.getSelectedFiles()){
    				selectedFilesList.add(selectedFile);
    			} // end enhanced for loop
    		} // end if
    		return selectedFilesList.toArray(new File[selectedFilesList.size()]);
    	}// end browse method
    	
    } // end class FileBrowser
    Thumbnailer:
    Java Code:
    package projects.web;
    
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageReader;
    import javax.imageio.ImageReadParam;
    import java.util.List;
    import java.util.ArrayList;
    
    public class Thumbnailer{
    	
    	private ImageReader reader;
    	private ImageReadParam param;
    	private List<Image> thumbnailsList = new ArrayList<Image>();
    	
    	public Thumbnailer(){
    		
    		reader = ImageIO.getImageReadersBySuffix("jpg").next();
    		param = reader.getDefaultReadParam();
    		param.setSourceSubsampling(6, 6, 0 ,0);
    	} 
    	
    	public void createThumbnails(File[] selectedFiles){
    		for (File imageFile : selectedFiles){
    			try{
    				reader.setInput(ImageIO.createImageInputStream(imageFile));
    				thumbnailsList.add(reader.read(0, param));
    			} catch (IOException e){
    				e.printStackTrace();
    			}
    		}
    	} 
    	
    	public List<Image> getThumbnails(){
    		return thumbnailsList;
    	}
    	
    }
    FilesManager:
    Java Code:
    package projects.web;
    
    import java.io.File;
    import java.util.List;
    import java.util.ArrayList;
    
    public class FilesManager {
    	
    	private List<File> selectedFiles;
    	
    	public FilesManager(){
    		selectedFiles = new ArrayList<File>();
    	}
    	
    	public void setSelectedFiles(File[] files){
    		for (File file : files){
    			selectedFiles.add(file);
    		}
    	}
    	
    	public void removeSelectedFiles(int[] indices){
    		
    	}
    	
    	public void clearSelectedFiles(){
    		selectedFiles.clear();
    	}
    }
    Last edited by dan0; 03-09-2009 at 07:16 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Sounds like a good candidate for a JList with a DefaultListModel and a custom renderer. The list would provide the selection functionality via its selection model and the DefaultListModel would make it easy to add and remove elements, and would also function as the data structure for the thumbnails.

    db

  5. #5
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Sounds like a good candidate for a JList with a DefaultListModel and a custom renderer. The list would provide the selection functionality via its selection model and the DefaultListModel would make it easy to add and remove elements, and would also function as the data structure for the thumbnails.

    db
    I'm having a bit of trouble with the custom renderer. I've looked at the Java Tutorials, but they weren't that clear/helpful on how to create and implement the renderer. Can someone help me understand and complete the renderer?

    My goal is to display the thumbnails side-by-side (like it would be in FlowLayout) and have selected thumbnails highlighted with a red border.

    The following is what I've put so far (which unfortunately, is not much):

    Java Code:
    class ListRenderer implements ListCellRenderer{
    		
    		public ListRenderer(){
    			
    		}
    		
    		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
    			
    			if (isSelected) {
                   //setBorder(new LineBorder(Color.RED, 3));                
                } 
    			return null;
    		}
    	}

  6. #6
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    I've made a lot of progress with the custom renderer. But there is one thing that I haven't been able to figure out. Whenever, I click in the JList area one of the cells is always selected - how can I have selections toggle like JToggleButtons? For example, lets say I clicked on a cell in the JList, but then wanted to "de-select" that cell and no cells selected, how can I do that?

    Below is the code for the custom renderer:

    Java Code:
    class ListRenderer extends JLabel implements ListCellRenderer{
    		
    		private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
    		private JLabel renderer;
    		
    		public ListRenderer(){
    			
    		}
    		
    		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
    			renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    			
    			renderer.setHorizontalAlignment(CENTER);
    			renderer.setVerticalAlignment(CENTER);
    			
    			if (isSelected){
    				renderer.setBorder(new LineBorder(Color.RED, 3));
    				renderer.setBackground(null);
    			}
    			if (!isSelected){
    				renderer.setBorder(null);
    				renderer.setBackground(null);
    			}
    			
    			return renderer;
    		}
    	}

Similar Threads

  1. Key selection for SSL/TLS
    By OrangeDog in forum Advanced Java
    Replies: 3
    Last Post: 04-27-2009, 10:38 PM
  2. JAVA: String char removal with nested loop
    By igniteflow in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 02:09 AM
  3. Removal of Homework Requests
    By CaptainMorgan in forum Suggestions & Feedback
    Replies: 14
    Last Post: 08-03-2008, 09:21 PM
  4. problem with JLabels
    By geork in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 02:30 PM
  5. Having Trouble Aligning JLabels
    By Mark_Petrov in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2008, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •