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

    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.


  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    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 Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Using a Custom JList CellRenderer that extends JPanel

    Moved from New to Java

    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
    Rep Power

    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++){
                    JList<int> Jset = new JList<File>(set);
    		Jset.setCellRenderer(new CustomCellRenderer());
    //List construction
    class CustomCellRenderer extends JPanel implements ListCellRenderer<File>{
    //In the same file...
    	private HashMap<int, JPanel> map = new HashMap<int,JPanel>();
    	public CustomCellRenderer(){
    	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); 
    			map.put(f, panel);
    		JPanel temp = map.get(f);
    		if (isSelected)temp.setBackground(Color.LIGHT_GRAY);
    		else temp.setBackground(Color.WHITE);
    		else temp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		add(temp);  //Not sure about this add()
    		return temp;
    class box extends JPanel{
    	private int i=0;//A bit more than just this of course
    	public box(int i){
    		setSize(90, 10*i);
    	protected void paintComponent(Graphics g){
    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