Results 1 to 4 of 4
  1. #1
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Using a Custom JList CellRenderer that extends JPanel

    I'm trying to create a scrollable list that supports Drag n Drop from a list of graphics (the JList) to the main area (another JPanel). I'd like to use JList because it has the slection/DnD things baked in. If it's untenable, I can live with trying something else.

    Anyway, the issue I'm running into is inside the CellRenderer I'm trying to write. Inside getListCellComponent I would like to put together a graphic based on the data received from the list. However, I seem to be unable to access any graphics from within this class (getGraphics() returns null). Am I overlooking something obvious here? Should I write something that the renderer can call that will put together the component and pass it up through? Should I be looking into designing a list from scratch using JPanels, layout managers, and a JScrollPane?

    Guess it's more of a design question than a code question, but I'd like to get more experienced opinions before I go any further.

    Thanks

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

    Default Re: Using a Custom JList CellRenderer that extends JPanel

    You should almost never be calling getGraphics any way. Consider creating and posting a small compilable program that runs, that uses internet-available resources (such as for images) and that demonstrates you problem -- an SSCCE, and I'll bet we'll be better able to help you.

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

    Default Re: Using a Custom JList CellRenderer that extends JPanel

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Using a Custom JList CellRenderer that extends JPanel

    I ended up going with a method that passes another component to the renderer. Hopefully this makes sense.

    Java Code:
    public class listbar extends JPanel{
    public listbar{
             DefaultListModel<int> set = new DefaultListModel<int>();
    		for(int i=0;i<10;i++){
    			set.addElement(i);
    		}
                    JList<int> Jset = new JList<File>(set);
    		Jset.setCellRenderer(new CustomCellRenderer());
                    add(Jset);
                    setVisible(true);
             }
    }
    //List construction
    
    class CustomCellRenderer extends JPanel implements ListCellRenderer<File>{
    //In the same file...
    	private HashMap<int, JPanel> map = new HashMap<int,JPanel>();
    	
    	public CustomCellRenderer(){
    		setOpaque(true);
    	}
    	
    	@Override
    	public Component getListCellRendererComponent(JList<? extends int> list,
    			int f, int index, boolean isSelected, boolean hasFocus) {
    		
    		if(!map.containsKey(f)){  //Memoized
    			JPanel panel = new JPanel();
    			box temp = new box(f); 
    			panel.add(temp);
    			map.put(f, panel);
    		}
    		JPanel temp = map.get(f);
    		
    		if (isSelected)temp.setBackground(Color.LIGHT_GRAY);
    		else temp.setBackground(Color.WHITE);
    		if(hasFocus)temp.setBorder(BorderFactory.createDashedBorder(Color.BLACK,1,2));
    		else temp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		
    		add(temp);  //Not sure about this add()
    		setVisible(true);
    		return temp;
    	}
    	
    }
    
    class box extends JPanel{
    	private int i=0;//A bit more than just this of course
    	
    	public box(int i){
    		this.i=i;
    		setSize(90, 10*i);
    		setPreferredSize(getSize());
    		setBorder(BorderFactory.createLineBorder(Color.BLACK));
    			
    	}
    	
    	protected void paintComponent(Graphics g){
    		g.drawString(""+i,2,12);
    	}
    }
    Any suggestions as to a better implementation than this? I didn't include the scrolling, and I still have to implement the DnD that would essentially transfer the int represented by the image to somewhere else to be converted into another image.

Similar Threads

  1. Custom JList
    By daghost in forum New To Java
    Replies: 2
    Last Post: 11-17-2011, 11:57 PM
  2. Update Table GUI that extends custom AbstractTableModel
    By nidhirastogi in forum AWT / Swing
    Replies: 0
    Last Post: 02-09-2011, 03:27 PM
  3. Replies: 2
    Last Post: 02-07-2011, 03:01 PM
  4. JList with custom Array
    By sony144 in forum Advanced Java
    Replies: 1
    Last Post: 10-09-2010, 06:35 PM
  5. Showing JList in a JPanel
    By nico.hvi in forum AWT / Swing
    Replies: 0
    Last Post: 03-10-2010, 02:26 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
  •