Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5726 | Replies: 2 | Solved
![]() |
•
•
Join Date: Jul 2006
Posts: 112
Reputation:
Rep Power: 3
Solved Threads: 0
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:
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.
Please kindly provide some help.
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;
}
} Last edited by ryy705 : Aug 18th, 2006 at 2:36 pm.
•
•
Join Date: Mar 2004
Posts: 732
Reputation:
Rep Power: 6
Solved Threads: 31
This should help out. For dynamic data changes look at the list model.
http://java.sun.com/docs/books/tutor...ents/list.html
http://java.sun.com/docs/books/tutor...ents/list.html
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode