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


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

JList and selection problem

oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
need help in finding solution this problem:want a my programe to be able to add food item and remove whenever i click on any of the button created,also give me an option of whether am sure of delecting and item.
I used a sample code from sun tutorials of JList(ListDemo.java) but couldn't my problem and also was alot of that am find difficult debuge.somebody tell what to do.thanks

the code is thus:

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

import javax.swing.*;
import javax.swing.border.*;

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(
list.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 = list.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--;
}

list.setSelectedIndex(index);
list.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 = list.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}

listModel.insertElementAt(employeeName.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.
list.setSelectedIndex(index);
list.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 (list.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);
}
}
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
28 errors:
fi.java:7: cannot find symbol
symbol: class ListSelectionListener
public class FI extends JPanel implements ListSelectionListener

add this import statement

so the compiler can look up the ListSelectionListener and related classes.
Recompile:

19 errors:
fi.java:39: cannot find symbol
symbol : variable addstring
location: class FI
JButton addButton = new JButton(addstring);
^
Your variable is declared as

You are trying to refer to it as "addstring". Change this to "addString".
Same with substring and subString. The compiler error gives the line numbers where changes are required. Next errors from the console:
fi.java:53: cannot find symbol
symbol : variable list
location: class FI
list.getSelectedIndex()).toString();
^
fi.java:52: cannot find symbol
symbol : variable listModel
location: class FI
String name = listModel.getElementAt(
^
Your declarations are:

In SubListener you are trying to refer to these with different names: list and listModel. The compiler cannot find these. Change the SubListener names to match your declarations.
When you get all the variables spelled correctly and recompile:

C:\jexp>javac fi.java
fi.java:120: cannot find symbol
symbol : variable employeeName
location: class FI.AddListener
listmodel.insertElementAt(employeeName.getText(), index);
^
1 error

Looks like there is a missing JTextField named employeeName. And the comment above the AddListener class mentions a hire button.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JList and selection problem
 
Similar Threads
JList deselection problem
JButton enabling and disabling
Ref:JList and Selection problem
JList null pointer exception
altering a GUI object without triggering the listener