Results 1 to 4 of 4
- 02-23-2012, 05:24 PM #1
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
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
-
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.
- 02-24-2012, 04:01 AM #3
Re: Using a Custom JList CellRenderer that extends JPanel
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-24-2012, 05:29 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
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.
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.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); } }
Similar Threads
-
Custom JList
By daghost in forum New To JavaReplies: 2Last Post: 11-17-2011, 11:57 PM -
Update Table GUI that extends custom AbstractTableModel
By nidhirastogi in forum AWT / SwingReplies: 0Last Post: 02-09-2011, 03:27 PM -
Print not working on my JTable with custom CellRenderer
By flizbert in forum AWT / SwingReplies: 2Last Post: 02-07-2011, 03:01 PM -
JList with custom Array
By sony144 in forum Advanced JavaReplies: 1Last Post: 10-09-2010, 06:35 PM -
Showing JList in a JPanel
By nico.hvi in forum AWT / SwingReplies: 0Last Post: 03-10-2010, 02:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks