aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Ref:JList and Selection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Ref:JList and Selection problem" Watch "Ref:JList and Selection problem" New topic
Author

Ref:JList and Selection problem

oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
i was able to debuge the program code i sent but the problem is i want it in such away that whenever i click on any of the food item,it will display a dialog telling me it's description(e.g if click on chicken it will tell that it belongs to protein class).please, tell me or give and hint in achieveing my goal.

the corrected code is thus:
/**@author:kingsley Efekodo
*@created:31/8/2006 Time:12:02pm
*@copy right:koe networks
*/
import java.awt.*;
import java.awt.event.*;

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

public class FoodItem extends JPanel implements ListSelectionListener
{
//String proteinFood[]=new String[]{"chicken","turkey","beef","fish","egg whites"};
private JList foodlist;
private DefaultListModel listModel;

private static final String addstring="Add Food Item";
private static final String substring="Remove Food Item";
private JButton subButton;
private JTextField foodName;

public FoodItem()
{
super(new BorderLayout());

listModel=new DefaultListModel();
listModel.addElement("chicken");
listModel.addElement("fish");
listModel.addElement("turkey");
listModel.addElement("beef");
listModel.addElement("egg whites");

//create the list and put it in a scroll pane
foodlist=new JList(listmodel);
foodlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
foodlist.setSelectedIndex(0);
foodlist.addListSelectionListener(this);
foodlist.setVisibleRowCount(5);
JScrollPane listScrollPane = new JScrollPane(foodlist);


JButton addButton = new JButton(addstring);
AddListener addListener = new AddListener(addButton);
addButton.setActionCommand(addstring);
addButton.addActionListener(addListener);
addButton.setEnabled(false);

subButton = new JButton(substring);
subButton.setActionCommand(substring);
subButton.addActionListener(new SubListener());

foodName = new JTextField(10);
foodName.addActionListener(addListener);
foodName.getDocument().addDocumentListener(addListener);
String name = listModel.getElementAt(
foodlist.getSelectedIndex()).toString();


//Create a panel that uses BoxLayout.
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane,
BoxLayout.LINE_AXIS));
buttonPane.add(subButton);
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(foodName);
buttonPane.add(addButton);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

add(listScrollPane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);

}
class SubListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//This method can be called only if
//there's a valid selection
//so go ahead and remove whatever's selected.
int index = foodlist.getSelectedIndex();
listModel.remove(index);

int size = listModel.getSize();

if (size == 0) { //Nobody's left, disable firing.
subButton.setEnabled(false);

} else { //Select an index.
if (index == listModel.getSize()) {
//removed item in last position
index--;
}

foodlist.setSelectedIndex(index);
foodlist.ensureIndexIsVisible(index);
}
}
}

//This listener is shared by the text field and the hire button.
class AddListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;

public AddListener(JButton button) {
this.button = button;
}

//Required by ActionListener.
public void actionPerformed(ActionEvent e) {
String name = foodName.getText();

//User didn't type in a unique name...
if (name.equals("") || alreadyInList(name)) {
Toolkit.getDefaultToolkit().beep();
foodName.requestFocusInWindow();
foodName.selectAll();
return;
}

int index = foodlist.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}

listModel.insertElementAt(foodName.getText(), index);
//If we just wanted to add to the end, we'd do this:
//listModel.addElement(foodName.getText());

//Reset the text field.
foodName.requestFocusInWindow();
foodName.setText("");

//Select the new item and make it visible.
foodlist.setSelectedIndex(index);
foodlist.ensureIndexIsVisible(index);
}

//This method tests for string equality. You could certainly
//get more sophisticated about the algorithm. For example,
//you might want to ignore white space and capitalization.
protected boolean alreadyInList(String name) {
return listModel.contains(name);
}

//Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
enableButton();
}

//Required by DocumentListener.
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}

//Required by DocumentListener.
public void changedUpdate(DocumentEvent e) {
if (!handleEmptyTextField(e)) {
enableButton();
}
}

private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}

private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}

//This method is required by ListSelectionListener.
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {

if (foodlist.getSelectedIndex() == -1) {
//No selection, disable fire button.
subButton.setEnabled(false);

} else {
//Selection, enable the fire button.
subButton.setEnabled(true);
}
}
}


//main method
public static void main()
{
//set frame for food item
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,400);
Container c=frame.getContentPane();
FoodItem foodItem=new FoodItem();
c.add(foodItem);
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
1)
change the name of the class to something else (see #2)

2)
FoodItem should be the name of the class for the individual item


3)
add FoodItems to the list
listModel.addElement(new FoodItem("chicken","protein blah..blah.."));
the toString() method will display "chicken" in the JList

4)
add a listener to the list, when an item is selected, getSelectedValue()
will return the (eg chicken) fooditem - you then display your message box
showing [returnedObject].description
oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
i did what you asked me to do and gave an error lease can explain more

this is what i did:

/**@author:kingsley Efekodo
*@created:31/8/2006 Time:12:02pm
*@copy right:koe networks
*/
import java.awt.*;
import java.awt.event.*;

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

public class FoodItem extends JPanel implements ListSelectionListener
{
//String proteinFood[]=new String[]{"chicken","turkey","beef","fish","egg whites"};
private JList foodlist;
private DefaultListModel listModel;

private static final String addstring="Add Food Item";
private static final String substring="Remove Food Item";
private JButton subButton;
private JTextField foodName;

public FoodItem()
{
super(new BorderLayout());

listModel=new DefaultListModel();
/*listModel.addElement("chicken");
listModel.addElement("fish");
listModel.addElement("turkey");
listModel.addElement("beef");
listModel.addElement("egg whites");
*/

listModel.addElement(new Food1("chicken","fish","turky","beef","egg whites"));



//create the list and put it in a scroll pane
foodlist=new JList(listModel);
foodlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
foodlist.setSelectedIndex(0);
foodlist.addListSelectionListener(this);
foodlist.setVisibleRowCount(5);
JScrollPane listScrollPane = new JScrollPane(foodlist);


JButton addButton = new JButton(addstring);
AddListener addListener = new AddListener(addButton);
addButton.setActionCommand(addstring);
addButton.addActionListener(addListener);
addButton.setEnabled(false);

subButton = new JButton(substring);
subButton.setActionCommand(substring);
subButton.addActionListener(new SubListener());

foodName = new JTextField(10);
foodName.addActionListener(addListener);
foodName.getDocument().addDocumentListener(addListener);
String name = listModel.getElementAt(
foodlist.getSelectedIndex()).toString();


//Create a panel that uses BoxLayout.
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane,
BoxLayout.LINE_AXIS));
buttonPane.add(subButton);
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(foodName);
buttonPane.add(addButton);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

add(listScrollPane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);

}
class SubListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//This method can be called only if
//there's a valid selection
//so go ahead and remove whatever's selected.
int index = foodlist.getSelectedIndex();
listModel.remove(index);

int size = listModel.getSize();

if (size == 0) { //Nobody's left, disable firing.
subButton.setEnabled(false);

} else { //Select an index.
if (index == listModel.getSize()) {
//removed item in last position
index--;
}

foodlist.setSelectedIndex(index);
foodlist.ensureIndexIsVisible(index);
}
}
}

//This listener is shared by the text field and the hire button.
class AddListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;

public AddListener(JButton button) {
this.button = button;
}

//Required by ActionListener.
public void actionPerformed(ActionEvent e) {
String name = foodName.getText();

//User didn't type in a unique name...
if (name.equals("") || alreadyInList(name)) {
Toolkit.getDefaultToolkit().beep();
foodName.requestFocusInWindow();
foodName.selectAll();
return;
}

int index = foodlist.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}

listModel.insertElementAt(foodName.getText(), index);
//If we just wanted to add to the end, we'd do this:
//listModel.addElement(foodName.getText());

//Reset the text field.
foodName.requestFocusInWindow();
foodName.setText("");

//Select the new item and make it visible.
foodlist.setSelectedIndex(index);
foodlist.ensureIndexIsVisible(index);
}

//This method tests for string equality. You could certainly
//get more sophisticated about the algorithm. For example,
//you might want to ignore white space and capitalization.
protected boolean alreadyInList(String name) {
return listModel.contains(name);
}

//Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
enableButton();
}

//Required by DocumentListener.
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}

//Required by DocumentListener.
public void changedUpdate(DocumentEvent e) {
if (!handleEmptyTextField(e)) {
enableButton();
}
}

private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}

private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}

//This method is required by ListSelectionListener.
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {

if (foodlist.getSelectedIndex() == -1) {
//No selection, disable fire button.
subButton.setEnabled(false);

} else {
//Selection, enable the fire button.
subButton.setEnabled(true);
}
}
}


//main method
public static void main(String a[])
{
//set frame for food item
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,400);
Container c=frame.getContentPane();
FoodItem foodItem=new FoodItem();
c.add(foodItem);
}
}
class Food1
{
String name;
String description;
public Food1(String n, String d)
{
name = n; description = d;
}
public String toString(){return name;}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i did what you asked me to do

3)
add FoodItems to the list
listModel.addElement(new FoodItem("chicken","protein blah..blah.."));
the toString() method will display "chicken" in the JList

this part is wrong
listModel.addElement(new Food1("chicken","fish","turky","beef","egg whites"));

it should be
listModel.addElement(new Food1("chicken","belongs to protein class"));
listModel.addElement(new Food1("fish",.............));
listModel.addElement(new Food1("turky",.............));
listModel.addElement(new Food1("beef",.............));
listModel.addElement(new Food1("egg whites",.............));
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ref:JList and Selection problem