Results 1 to 3 of 3
  1. #1
    futarokun is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Adding Jpanel to A JList or JTable

    Hello

    I want to add a Jpanel to a Jframe or to a JTable (if possible). Can I do that with a Renderer and which class should I implement?

    Thanks in advance.

    For more Information, here are my codes, and it only displays text at the moment:

    Java Code:
    public class MainView extends JFrame {
    	
    	private JList<JPanel> resultList;
    	private DefaultListModel<JPanel> resultListModel;
    
    	public MainView() {	
    		
    		JScrollPane leftPane = new JScrollPane();
    		leftPane.setBounds(10, 10, 400, 430);
    		bottomPanel.add(leftPane);
    		
    		resultList = new JList<JPanel>();
    		resultListModel = new DefaultListModel<JPanel>();
    		resultList.setModel(resultListModel);
    		resultList.setBackground(Theme.getColor(3));
    		leftPane.setViewportView(resultList);
    
    	}
    	
    	private JPanel getResultPanel(ObjectToTest objectToTest){
    		JPanel resultPanel = new JPanel();
    		resultPanel.setPreferredSize(new Dimension(380, 100));
    		resultPanel.setBackground(Theme.getColor(2));
    		resultPanel.setLayout(null);
    		
    		ImageLabel avatarLabel = new ImageLabel(objectToTest.getAvatar());
    		avatarLabel.setBounds(10, 10, 80, 80);
    		resultPanel.add(avatarLabel);
    		
    		JLabel name1Label = new JLabel(objectToTest.getName1());
    		name1Label.setBounds(100, 10, 270, 35);
    		name1Label.setBackground(Theme.getColor(0));
    		name1Label.setForeground(Theme.getColor(3));
    		resultPanel.add(name1Label);
    		
    		JLabel name2Label = new JLabel(objectToTest.getName2());
    		name2Label.setBounds(100, 55, 270, 35);
    		name2Label.setBackground(Theme.getColor(0));
    		name2Label.setForeground(Theme.getColor(3));
    		resultPanel.add(name2Label);
    		
    		return resultPanel;
    	}
    	
    	public void setListOfResult(List<ObjectToTest> objectsToTests){
    		resultListModel.clear();
    //		List<JPanel> resultPanels = new ArrayList<JPanel>();
    		for(int i =0;i<objectsToTests.size();i++){
    //			resultPanels.add(getResultPanel(objectsToTests.get(i)));
    //			resultListModel.addElement(resultPanels.get(i));
    			resultListModel.addElement(getResultPanel(objectsToTests.get(i)));
    		}
    //		resultList.firePropertyChange();
    		
    	}
    	
    	public static void main(String[] args) {
    		MainView view = new MainView();
    		view.setVisible(true);
    		List<ObjectToTest> objectList = new ArrayList<ObjectToTest>();
    		objectList.add(new ObjectToTest("img/test1.jpg", "Name1 Of Obj1", "Name2 Of Obj1"));
    		objectList.add(new ObjectToTest("img/test2.jpg", "Name1 Of Obj2", "Name2 Of Obj2"));
    		objectList.add(new ObjectToTest("img/test3.jpg", "Name1 Of Obj3", "Name2 Of Obj3"));
    		view.setListOfResult(objectList);
    		
    	}
    	
    }
    Last edited by futarokun; 11-08-2014 at 05:40 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Adding Jpanel to A JList or JTable

    Adding a JPanel to a JFrame is trivial; if you want to add it to a JTable, you should be able to render it, so you have to implement the TableCellRenderer (and it should return your JPanel).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    futarokun is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Re: Adding Jpanel to A JList or JTable

    Thanks, I did it after trying :D

Similar Threads

  1. JList with JScrollPane in a JPanel
    By Fingar in forum New To Java
    Replies: 1
    Last Post: 06-16-2012, 09:26 PM
  2. Adding to JList with Netbeans
    By kewlgeye in forum NetBeans
    Replies: 7
    Last Post: 03-28-2012, 07:39 PM
  3. Problem while adding component to JPanel or JList??
    By jspexception in forum AWT / Swing
    Replies: 1
    Last Post: 01-27-2012, 12:20 PM
  4. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 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
  •