"How to create product filter"
Bootstrap 3.0.0 Snippet by ajeetaryan

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="row"> <h2>Create your snippet's HTML, CSS and Javascript in the editor tabs</h2> <code> /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.erp.cui; /** * * @author ajeet.singh */ import com.erp.cui.Demo.MyOptionPane; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; public class Demo { public List<Item> getItemList(int count) { char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray(); List<Item> list = new ArrayList<Item>(); for (int j = 1; j < count; j++) { StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < 20; i++) { char c = chars[random.nextInt(chars.length)]; sb.append(c); } String output = sb.toString(); Item item = new Item(); item.setId(j); item.setName(output); list.add(item); } return list; } private void createAndShowGUI() { final JButton button = new JButton("Show dialog"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MyOptionPane optionPane = new MyOptionPane(); Object option = optionPane.showYesNoMessage("Close frame", "Do you really want to close the frame?", getItemList(1000)); if (option != null) { Item item = (Item) option; button.setText(item.getId() + "\n" + item.getName()); } } }); JPanel content = new JPanel(); content.add(new JLabel("Test:")); content.add(button); JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.getContentPane().add(content); frame.setSize(500, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } public class MyOptionPane { private List<Item> defaultValues; private Object choice = null; private JList<Item> lstItems; public Object showYesNoMessage(String title, String message, List<Item> defaultValues) { this.defaultValues = defaultValues; final JDialog dialog = new JDialog(); JLabel label = new JLabel(message); final JTextField tftSearch = new JTextField(); lstItems = new JList<Item>(); fillModel(); final JScrollPane theScrollPane = new JScrollPane(); theScrollPane.revalidate(); theScrollPane.repaint(); theScrollPane.setViewportView(lstItems); tftSearch.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { filter(); } @Override public void removeUpdate(DocumentEvent e) { filter(); } @Override public void changedUpdate(DocumentEvent e) { } private void filter() { String filter = tftSearch.getText(); filterModel((DefaultListModel<Item>) lstItems.getModel(), filter); if (lstItems.getModel().getSize() > 0) { lstItems.setSelectedIndex(0); } } }); lstItems.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() > 1) { if (lstItems.getSelectedIndex() >= 0) { choice = lstItems.getSelectedValue(); dialog.dispose(); } } } }); tftSearch.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP) { int index = lstItems.getSelectedIndex(); if (index > 0) { index--; } lstItems.setSelectedIndex(index); lstItems.ensureIndexIsVisible(lstItems.getSelectedIndex()); // tftSearch.setText(lstItems.getSelectedValue().toString()); } if (e.getKeyCode() == KeyEvent.VK_DOWN) { int index = lstItems.getSelectedIndex(); if (index < lstItems.getModel().getSize() - 1) { index++; } lstItems.setSelectedIndex(index); lstItems.ensureIndexIsVisible(lstItems.getSelectedIndex()); // tftSearch.setText(lstItems.getSelectedValue().toString()); } if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (lstItems.getSelectedIndex() >= 0) { choice = lstItems.getSelectedValue(); dialog.dispose(); } else { JOptionPane.showMessageDialog(dialog, "Please select a product first!", "Eroor Message", JOptionPane.ERROR_MESSAGE); tftSearch.setText(""); fillModel(); } } if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { dialog.dispose(); } } }); JPanel content = new JPanel(new BorderLayout(8, 8)); content.add(tftSearch, BorderLayout.NORTH); content.add(theScrollPane, BorderLayout.CENTER); content.add(label, BorderLayout.SOUTH); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setModal(true); dialog.setTitle(title); dialog.getContentPane().add(content); dialog.pack(); dialog.setLocationRelativeTo(null); dialog.setVisible(true); return choice; } public void fillModel() { DefaultListModel<Item> model = new DefaultListModel<Item>(); for (Item s : defaultValues) { model.addElement(s); } lstItems.setModel(model); } public void filterModel(DefaultListModel<Item> model, String filter) { for (Item s : defaultValues) { if (!s.toString().startsWith(filter)) { if (model.contains(s)) { model.removeElement(s); } } else if (!model.contains(s)) { model.addElement(s); } } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Demo().createAndShowGUI(); } }); } } </code> </div> </div>

Related: See More


Questions / Comments: