RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5726 | Replies: 2 | Solved
Reply
Join Date: Jul 2006
Posts: 112
Reputation: ryy705 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

How to use JList

  #1  
Aug 18th, 2006
Hi,
I need to learn how I can use a custom data structure in a JList. In my program when I click the Add button next to the JList a JOptionPane opens and takes a String input. The input is then used to create an object which is placed in a data structure. The purpose of JList is to display the data structure and allow the selection of the objects in the data structure.

What is the idiom for using dynamic data structures with JLists?
currently I can not select anything from the list(nothing is highlighted). And the Frame has be clicked(focused) for the JList to display the updated list.

I am currently using the following methods for the gui:

 
    /**
     * 
     */
    public void populateList(ListInterface grList)
    {        
        ls_group = new JList(getListModel(grList));
        LabelListCellRenderer renderer = new LabelListCellRenderer();
        ls_group.setCellRenderer(renderer);
                             ls_group.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane grScrPane = new JScrollPane(ls_group,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = ins;
        gbc.fill = GridBagConstraints.BOTH;
        add(grScrPane, gbc);
    }

public DefaultListModel getListModel(ListInterface labels)
    {
        DefaultListModel model = new DefaultListModel();
        for(int i = 1; i <= labels.size(); i++) {
            model.addElement(labels.get(i));
        }
        return model;
    }

populateList method is called every time something is added to the data structure. Is there a simpler way?
The following is the code for LabelListCellRenderer class if you need it because I am not sure why I need it.

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

class CheckListCellRenderer extends JCheckBox
    implements ListCellRenderer
{
    protected static Border m_noFocusBorder = 
        new EmptyBorder(1, 1, 1, 1);
        
    public CheckListCellRenderer()
    {
        super();
        setOpaque(true);
        setBorder(m_noFocusBorder);
    }
    
    public Component getListCellRendererComponent(JList list, 
        Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        setText(value.toString());
        
        setBackground(isSelected ? list.getSelectionBackground() :
            list.getBackground());
        
        setForeground(isSelected ? list.getSelectionForeground() :
            list.getForeground());
            
        CheckListItem data = (CheckListItem)value;
            setSelected(data.isSelected());
            
        setFont(list.getFont());
        setBorder((cellHasFocus) ?
        UIManager.getBorder("List.focusCellHighlightBorder")
            : m_noFocusBorder);
            
        return this;
    }
}
Please kindly provide some help.
Last edited by ryy705 : Aug 18th, 2006 at 2:36 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Posts: 732
Reputation: Phaelax is on a distinguished road 
Rep Power: 6
Solved Threads: 31
Phaelax Phaelax is offline Offline
Master Poster

Re: How to use JList

  #2  
Aug 20th, 2006
This should help out. For dynamic data changes look at the list model.
http://java.sun.com/docs/books/tutor...ents/list.html
Reply With Quote  
Join Date: Jul 2006
Posts: 112
Reputation: ryy705 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: How to use JList

  #3  
Aug 21st, 2006
Thank you. Thank you. This just might work.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:49 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC