Remove an item from autocomplete combobox after add that item to jlist
here i add a jar file named glazedlists_java15-1.9.0.jar
here is the code for add fields to jpanel
DefaultComboBoxModel dt=new DefaultComboBoxModel();
comboBook = new JComboBox(dt);
comboBook.addItemListener(this);
List<Book>books=ServiceFactory.getBookServiceImpl().findAllBook();
Object[] elementBook = new Object[books.size()];
int i=0;
for(Book b:books){
elementBook[i]=b.getCallNo();
// dt.addElement(elementBook[i]);
i++;
}
AutoCompleteSupport.install(comboBook, GlazedLists.eventListOf(elementBook));
comboBook.setBounds(232, 151, 184, 22);
issuePanel.add(comboBook);
btnAdd = new JButton("+");
btnAdd.addActionListener(this);
btnAdd.setBounds(427, 151, 56, 23);
issuePanel.add(btnAdd);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(232, 184, 184, 107);
issuePanel.add(scrollPane);
v=new Vector<String>();
listBooks = new JList(v);
scrollPane.setViewportView(listBooks);
btnRemove = new JButton("-");
btnRemove.addActionListener(this);
btnRemove.setBounds(427, 185, 56, 23);
issuePanel.add(btnRemove);
Action performed code here..
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnAdd){
DefaultComboBoxModel dcm = (DefaultComboBoxModel) comboBook.getModel();
dcm.removeElementAt(index);
// Add what the user types in JTextField jf, to the vector
v.add(selectedBook);
// Now set the updated vector to JList jl
listBooks.setListData(v);
// Make the button disabled
jb.setEnabled(false);
}
else if(e.getSource()==btnRemove){
// Remove the selected item
v.remove(listBooks.getSelectedValue());
// Now set the updated vector (updated items)
listBooks.setListData(v);
}
here the image shows add an item from combobox to jlist then the item hide or remove from combobox.
if u guys know about this please share answers here.. & thankyou !!!
From what I can see from your description and code you're simply using the GlazedLists convenience methods to set up the initial auto-complete component, yet you're not using the fundamental part of GlazedLists to stitch the various elements together: EventLists.
Sure - you have derived a one off EventList to populate the autocomplete installation, but GlazedLists really expects you to be utilising EventLists to hold your objects throughout and not during a quick interchange. Pure Java Swing components like JComboBox and JList force you down the route of arrays and vectors, but GlazedLists provides a lot of helper classes if you stick to the various EventList implementations as your fundamental collection class. It has classes for combobox and jlist models, and selection model classes and these conveniently link up the EventLists and Swing components and you can really simplify your code once you go down this route.
Here's a very crude example of what I think you're trying to achieve. I think the code speaks for itself rather than me rambling on any longer. Note: I used GlazedLists 1.8.
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.swing.AutoCompleteSupport;
import ca.odell.glazedlists.swing.EventComboBoxModel;
import ca.odell.glazedlists.swing.EventListModel;
import ca.odell.glazedlists.swing.EventSelectionModel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
/**
*
* @author Andrew Roberts
*/
public class GlazedListsAutocompleteTest {
private JFrame mainFrame;
private JComboBox availableItems;
private EventList<Book> books = new BasicEventList<Book>();
private EventList<Book> selectedBooks;
public GlazedListsAutocompleteTest() {
populateAvailableBooks();
createGui();
mainFrame.setVisible(true);
}
private void populateAvailableBooks() {
books.add(new Book("A Tale of Two Cities"));
books.add(new Book("The Lord of the Rings"));
books.add(new Book("The Hobbit"));
books.add(new Book("And Then There Were None"));
}
private void createGui() {
mainFrame = new JFrame("GlazedLists Autocomplete Example");
mainFrame.setSize(600, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//EventComboBoxModel<Book> comboModel = new EventComboBoxModel<Book>(books);
availableItems = new JComboBox();
final SortedList<Book> availableBooks = new SortedList<Book>((BasicEventList<Book>) GlazedLists.eventList(books), new BookComparitor());
selectedBooks = new SortedList<Book>(new BasicEventList<Book>(), new BookComparitor());
AutoCompleteSupport autocomplete = AutoCompleteSupport.install(availableItems, availableBooks);
JButton addButton = new JButton("+");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
EventComboBoxModel<Book> comboModel = (EventComboBoxModel<Book>) availableItems.getModel();
try {
Book book = (Book) comboModel.getSelectedItem();
selectedBooks.add(book);
availableBooks.remove(book);
} catch (ClassCastException ex) {
System.err.println("Invalid item: cannot be added.");
}
}
});
final EventListModel listModel = new EventListModel(selectedBooks);
final EventSelectionModel selectionModel = new EventSelectionModel(selectedBooks);
JButton removeButton = new JButton("Remove");
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
EventList<Book> selectedListItems = selectionModel.getSelected();
for (Book book : selectedListItems) {
selectedBooks.remove(book);
availableBooks.add(book);
}
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(availableItems, BorderLayout.CENTER);
panel.add(addButton, BorderLayout.EAST);
JList selectedItemsList = new JList(listModel);
selectedItemsList.setSelectionModel(selectionModel);
mainFrame.setLayout(new BorderLayout());
mainFrame.getContentPane().add(panel, BorderLayout.NORTH);
mainFrame.getContentPane().add(selectedItemsList, BorderLayout.CENTER);
mainFrame.getContentPane().add(removeButton, BorderLayout.SOUTH);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GlazedListsAutocompleteTest();
}
});
}
class Book {
private String title;
public Book() {
}
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return getTitle();
}
}
class BookComparitor implements Comparator<Book> {
@Override
public int compare(Book b1, Book b2) {
return b1.getTitle().compareToIgnoreCase(b2.getTitle());
}
}
}