Guest User

JanelaPesquisar

a guest
May 13th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. public class JanelaPesquisar extends JDialog {
  2.  
  3.     private String valorRetornado = "";
  4.     private List<String> lista;
  5.     private int x=0;
  6.     private int y=0;
  7.  
  8.     public JanelaPesquisar(List<String> lista, int x,int y) {//esse construtor é importante para receber dados
  9.         this.lista = lista;
  10.         this.x = x;
  11.         this.y = y;
  12.     }
  13.  
  14.     public String getValorRetornado() {
  15.         inicialize();
  16.  
  17.         setVisible(true);
  18.         return valorRetornado;
  19.     }
  20.  
  21.     public void finalizeJanela() {
  22.         dispose();
  23.     }
  24.  
  25.     public void inicialize() {
  26.  
  27.         setTitle("Pesquisar");
  28.         setSize(350, 400);
  29.         setModal(true);
  30.         Container containerLista = new JPanel();
  31.         Container cp;
  32.         cp = getContentPane();
  33.         cp.setLayout(new BorderLayout());
  34.         // populate list
  35.         final FilteredJList list = new FilteredJList(containerLista);
  36.         for (int i = 0; i < lista.size(); i++) {
  37.             list.addItem(lista.get(i));
  38.         }
  39.         containerLista.setLayout(new GridLayout(1, 1));
  40.         containerLista.add(list);
  41.         // add to gui
  42.         JPanel painelDireita = new JPanel();
  43.         painelDireita.setLayout(new GridLayout(3, 1));
  44.  
  45.         painelDireita.add(new JLabel());
  46.         JScrollPane pane = new JScrollPane(containerLista, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  47.         JPanel painelCentro = new JPanel();
  48.         painelCentro.setLayout(new BorderLayout());
  49.         painelCentro.add(list.getFilterField(), BorderLayout.NORTH);
  50.         painelCentro.add(pane, BorderLayout.CENTER);
  51.         cp.add(painelCentro, BorderLayout.CENTER);
  52.         cp.add(painelDireita, BorderLayout.EAST);
  53.         cp.add(new JLabel("<Clic duplo = seleciona>"), BorderLayout.SOUTH);
  54.        
  55.         //setLocationRelativeTo(null);
  56.         setLocation(x- getWidth() / 2, y+50);
  57.        
  58.         list.addMouseListener(new MouseAdapter() {
  59.             public void mouseClicked(MouseEvent e) {
  60.                 if (e.getClickCount() == 2) { //clic duplo
  61.                     valorRetornado = (String) list.getSelectedValue();
  62.                     dispose();
  63.                 }
  64.             }
  65.         });
  66.         addWindowListener(new WindowListener() {
  67.             @Override
  68.             public void windowOpened(WindowEvent we) {
  69.             }
  70.  
  71.             @Override
  72.             public void windowClosing(WindowEvent we) {
  73.             }
  74.  
  75.             @Override
  76.             public void windowClosed(WindowEvent we) {
  77.                 valorRetornado = null;
  78.                 dispose();
  79.             }
  80.  
  81.             @Override
  82.             public void windowIconified(WindowEvent we) {
  83.             }
  84.  
  85.             @Override
  86.             public void windowDeiconified(WindowEvent we) {
  87.             }
  88.  
  89.             @Override
  90.             public void windowActivated(WindowEvent we) {
  91.                
  92.             }
  93.  
  94.             @Override
  95.             public void windowDeactivated(WindowEvent we) {
  96.             }
  97.         });
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment